How to detect hardware keyboard presence?

后端 未结 3 959
梦谈多话
梦谈多话 2020-12-24 03:30

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条回答
  • 2020-12-24 03:43

    "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

    0 讨论(0)
  • 2020-12-24 04:05

    To detect common qwerty keyboard connected use this:

    private boolean isKeyboardConnected() {
        return getResources().getConfiguration().keyboard == KEYBOARD_QWERTY;
    }
    
    0 讨论(0)
  • 2020-12-24 04:05

    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()'   
    }
    
    0 讨论(0)
提交回复
热议问题