Android - Get keyboard key press

后端 未结 4 1783
独厮守ぢ
独厮守ぢ 2020-12-08 05:00

I want to catch the press of any key of the softkeyboard. I don\'t want a EditView or TextView in my Activity, the event must be handled from a extended View inside my Activ

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 05:20

    With the hint of vasart i can get the KeyPress event. To make the keycode printable i have used the function getUnicodeChar passing it the meta button state then just a char cast solve the problem.

    This is the working code:

    @Override
    public boolean dispatchKeyEvent(KeyEvent KEvent) 
    {
        int keyaction = KEvent.getAction();
    
        if(keyaction == KeyEvent.ACTION_DOWN)
        {
            int keycode = KEvent.getKeyCode();
            int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState() );
            char character = (char) keyunicode;
    
            System.out.println("DEBUG MESSAGE KEY=" + character + " KEYCODE=" +  keycode);
        }
    
    
        return super.dispatchKeyEvent(KEvent);
    }
    

    Of course this work only with ASCII character.

提交回复
热议问题