keyCode on android is always 229

后端 未结 7 1429
长情又很酷
长情又很酷 2020-11-28 08:23

On my Samsung Galaxy tab 4 (Android 4.4.2, Chrome: 49.0.2623.105) I ran into a situation where the keyCode is always 229.

I\'ve setup a simple test for two situatio

相关标签:
7条回答
  • 2020-11-28 09:16

    Solution to fix it for WebView, note it handles only space character , but you can extend mapping KeyEvent.KEYCODE_SPACE => keyCode.

    class MyWebview: WebView {
        override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection? {
            return BaseInputConnection(this, true)
        }
    
        override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
            val dispatchFirst = super.dispatchKeyEvent(event)
    
            // Android sends keycode 229 when space button is pressed(and other characters, except new line for example)
            // So we send SPACE CHARACTER(here we handles only this case) with keyCode = 32 to browser explicitly
    
            if (event?.keyCode == KeyEvent.KEYCODE_SPACE) {
                if (event.action == KeyEvent.ACTION_DOWN) {
                    evaluateJavascript("javascript:keyDown(32)", null)
                } else if (event.action == KeyEvent.ACTION_UP) {
                    evaluateJavascript("javascript:keyUp(32)", null)
                }
            }
    
            return dispatchFirst
        }
    }
    
    0 讨论(0)
提交回复
热议问题