35

Is there a way to detect if the device I'm currently running on has a hardware keyboard installed?

How do I query device capabilities anyway?

Janusz
  • 187,060
  • 113
  • 301
  • 369
Marcus
  • 8,601
  • 4
  • 24
  • 24

3 Answers3

40

[android-developers] Re: Detect Physical Keyboard Layout (ex: QWERTY vs QWERTZ)

The flags provided by getResources().getConfiguration().keyboard are a good way of checking which keyboard (if any) is available.

hata
  • 11,633
  • 6
  • 46
  • 69
  • 21
    Basically that means: private boolean isHardwareKeyboardAvailable() { return getResources().getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS; } – Marcus Mar 10 '10 at 10:47
  • 4
    Yes, if for your purposes you are counting a "12-key keyboard" as a "hardware keyboard". – HostileFork says dont trust SE Mar 10 '10 at 10:56
  • 4
    Note it will detect a keyboard that is an integral part of the device, but it will not detect if a USB or Bluetooth keyboard is currently attached. See https://stackoverflow.com/q/12161989/11683 for that. – GSerg Jun 13 '17 at 08:25
4

Use the following method to ascertain presence of hard keyboard at any time:
(To my knowledge, soft keyboards all lack the features tested below )

public static boolean isHardKB(Context ctx) {
    Configuration cf = ctx.getResources().getConfiguration();
    return cf.navigation==Configuration.NAVIGATION_DPAD
        || cf.navigation==Configuration.NAVIGATION_TRACKBALL
        || cf.navigation==Configuration.NAVIGATION_WHEEL;
}

Optionally trap all run-time keyboard changes for each affected Activity via AndroidManifest:

android:configChanges="keyboard|keyboardHidden|navigation"

But be sure to support the above manifest change with (at least) a dummy onConfigurationChanged()

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Optionally employ 'isHardKB()'   
}
Bad Loser
  • 3,065
  • 1
  • 19
  • 31
  • What I can see cf.navigation relates to the virtual keyboard and not the physical keyboard, when I try a physical BT-keybord with keypad? But it can be a keyboard driver mistake? (The HAXM emulators says no keypad but keypad input works?) SO far I can only see as the article HostileForks answer relates to that Config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO detects if HW keyboard is present. – Jan Bergström Nov 23 '18 at 07:11
4

To detect common qwerty keyboard connected use this:

private boolean isKeyboardConnected() {
    return getResources().getConfiguration().keyboard == KEYBOARD_QWERTY;
}
dreinoso
  • 1,479
  • 17
  • 26