- Home /
Detect physical keyboard vs touch input
Is it possible to detect which type of keyboard input is coming from? That is, physical keyboard or on-screen? I'm working on a game that requires the user to type commands (words) and I'd like to apply logic like this:
private TouchScreenKeyboard OpenKeyboard;
void Update()
{
// Show touch-screen keyboard any time they use touch input.
if (Input.touchCount > 0)
{
if (!TouchScreenKeyboard.visible)
{
OpenKeyboard = TouchScreenKeyboard.Open(string.Empty, TouchScreenKeyboardType.ASCIICapable, false, false, false, false, string.Empty);
}
}
// Hide touch-screen keyboard any time they use keyboard input.
else if (Input.anyKeyDown && !string.IsNullOrEmpty(Input.inputString))
{
if (OpenKeyboard != null)
{
OpenKeyboard.active = false;
OpenKeyboard = null;
}
}
}
Basically, I want to automatically show/hide the on-screen keyboard for them if they need it or not. If they touch the screen, they want the on-screen keyboard, if they type using some other method (external keyboard), they do not want/need the on-screen keyboard.
Although I suppose if all devices have some way for the user to manually close the on-screen keyboard, just auto-opening it for them on touch input might be enough, since they could always close it if they choose to switch to an off-screen keyboard... Sadly, I do not think all devices have such a thing.
I can't easily test on all devices, which is why I'm hoping someone here has experience or inside knowledge. If Input.anyKeyDown tracks physical keyboard only, my code above should work, if it includes on-screen keyboard input, it will not (and it will end up closing the keyboard on every keystroke, fun!).
p.s.-I'm not using GUI controls, just a simulated "console" I render myself.