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?

3 Answers 3

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.

3
  • 22
    Basically that means: private boolean isHardwareKeyboardAvailable() { return getResources().getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS; }
    – Marcus
    Commented Mar 10, 2010 at 10:47
  • 4
    Yes, if for your purposes you are counting a "12-key keyboard" as a "hardware keyboard". Commented Mar 10, 2010 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 stackoverflow.com/q/12161989/11683 for that.
    – GSerg
    Commented Jun 13, 2017 at 8:25
5

To detect common qwerty keyboard connected use this:

private boolean isKeyboardConnected() {
    return getResources().getConfiguration().keyboard == KEYBOARD_QWERTY;
}
1
  • For those wondering, it's Configuration.KEYBOARD_QWERTY Commented Jan 22 at 11:51
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()'   
}
1
  • 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. Commented Nov 23, 2018 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.