2

I created EditText and applied it to the showSoftInput of InputMethodManager. When I enter a physical key, the keyboard is automatically hidden.

I want to prevent the keyboard from automatically being hidden.

I've tried the method below. 1. use showSoftInput Flag ImputMethodManager.SHOW_IMPLICIT, SHOW_FORCED 2. use InputConnectionWrapper in EditText, The string and the number work normally. but ctrl, Tab, Alt, F1,F2. Entering a key hides the keyboard.

I expect the keyboard to not be hidden when I enter the physical key. Thank you for reading.

Molly
  • 1,887
  • 3
  • 17
  • 34
Joon
  • 23
  • 4
  • see this https://stackoverflow.com/questions/1845285/how-to-block-virtual-keyboard-while-clicking-on-edittext-in-android/1845307#1845307 – nyulan Jan 24 '19 at 09:23

1 Answers1

0

a good option is just to close the softkeyboard when there is an input from hardware keyboard

Android classes usually provide event handlers, you can implement when subclassing them. The Activity class has the following event handlers:

  • onKeyDown(int keyCode, KeyEvent event)
  • onKeyLongPress(int keyCode, KeyEvent event)
  • onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
  • onKeyShortcut(int keyCode, KeyEvent event)
  • onKeyUp(int keyCode, KeyEvent event)

In addition all views have the following event handlers:

  • onKeyDown(int, KeyEvent)
  • onKeyUp(int, KeyEvent)

I guess there are many other classes that have similar event handlers for key events, but this should be enough for your situation. The KeyEvent then contains information about the pressed key, i.e. the key code.

in you case you might want to do something like this :

in you activity or view class override on of onKeyDown or onKeyUp methods and hide the softkeyboard in there like:

override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        hideSoftKeyboard()
        return super.onKeyUp(keyCode, event)
}

or you can add a keyListener for your edittext

mEditText.setOnKeyListener { v, keyCode, event ->
       hideSoftKeyboard()
       return@setOnKeyListener when (keyCode) {
           KeyEvent.ACTION_UP -> {
               hideSoftKeyboard()
               true
           }
           else -> false
       }
}

how to close softKeyword:

fun hideSoftKeyboard() {
   try {
        val inputMethodManager = getSystemService(
            Activity.INPUT_METHOD_SERVICE
        ) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(
            currentFocus!!.windowToken, 0)
    } catch (e: Exception) {}
}    

alireza easazade
  • 3,324
  • 4
  • 27
  • 35
  • I've also tried to block key events. However, to change the language, the language setting must be sent as an event. In that case, the virtual keyboard is hidden. – Joon Jan 24 '19 at 09:47
  • If enter a key on a physical keyboard, I want to prevent the virtual keyboard from automatically being hidden. Instead, I think only prevent virtual keyboards from being hidden because keys such as changing language can't be run if block events in the middle. – Joon Jan 25 '19 at 07:13