How to disable physical keyboard in code(use virtual keyboard all the time)

后端 未结 6 2086
迷失自我
迷失自我 2020-12-14 08:27

You might ask why do I want that. Here is the reason:

I used a barcode scanner for the login screen of my application. However connecting the barcode scanner will fo

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 09:04

    Yes, the barcode scanner is detected as a Physical Keyboard. When a keyboard is connected to the device, by default the soft keyboard is disabled. To enable it, we need to turn OFF hardware keyboard via:

    Settings > Language & Input > Select Input Method

    The option name may differ from device to device. We will be able to use the scanner along with the soft keyboard even though we turn it OFF.

    And NO, there is no way currently to programmatically accomplish this. The most we can do is detect when a scanner/keyboard is connected and redirect the user to the Input Method selection window, by overriding the onConfigurationChanged method like this:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      if(newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
    
        ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
                                      .showInputMethodPicker();
        Toast.makeText(this, "Barcode Scanner detected. Please turn OFF Hardware/Physical keyboard to enable softkeyboard to function.", Toast.LENGTH_LONG).show();
      }
    }
    

提交回复
热议问题