Android WebView: Handle arrow keys in JavaScript

后端 未结 1 1578
没有蜡笔的小新
没有蜡笔的小新 2021-01-14 02:52

I have a simple WebView application which I want to control with the keyboard. Is it possible to catch arrow keys in Javascript?

I have tried the following code with

相关标签:
1条回答
  • 2021-01-14 03:16

    You should overwrite the onKeyDown method of WebView. See: http://blog.csdn.net/focusxi/article/details/6780965

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event){
        int valKey = 0;
        System.out.println("Web KEY:"); 
        System.out.println(keyCode);    
    
        switch(keyCode){
            //UP
            case 50:
            case 19:
                valKey = 19;
                break;
            //DOWN
            case 83:
            case 20:
                valKey = 20;        
                break;
            //LEFT
            case 81:
            case 21:
                valKey = 21;
                break;
            //RIGHT
            case 69:
            case 22:
                valKey = 22;    
                break;
        }
    
        if (valKey!=0)
        {
            //new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT);
            KeyEvent event1 = new KeyEvent(KeyEvent.ACTION_DOWN, valKey);
    
            System.out.println(event1.getKeyCode());    
    
            return super.onKeyDown(38, event1);
        }
        else
        {
            return super.onKeyDown(keyCode, event);
        }
    
    }
    }
    
    0 讨论(0)
提交回复
热议问题