29

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
  • 176,216
  • 111
  • 293
  • 365
Marcus
  • 8,321
  • 4
  • 19
  • 23

3 Answers3

37

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

http://d.android.com/reference/android/content/res/Configuration.html#keyboard

  • 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
  • 3
    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
1

To detect common qwerty keyboard connected use this:

private boolean isKeyboardConnected() {
    return getResources().getConfiguration().keyboard == KEYBOARD_QWERTY;
}
dreinoso
  • 905
  • 10
  • 21
0

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
  • 2,435
  • 1
  • 16
  • 29
  • 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