Tuesday, March 12, 2019

Android开发笔记-ch3.4.18 Key processing


3.4.18 Key processing

Refer to KeyEvent: KEYCODE_DPAD_UP/DOWN/LEFT/RIGHT/CENTER: 19/20/21/22/23, 82/KEYCODE_MENU, 7/KEYCODE_0
183/KEYCODE_PROG_RED, 184/KEYCODE_PROG_GREEN, 185/KEYCODE_PROG_YELLOW, acts as a contextual/programmable key
17/*: KEYCODE_STAR, 18/#: KEYCODE_POUND. 7~16 are number key 0~9.
Each key press is described by a sequence of key events. A key press starts with a key event with ACTION_DOWN. If the key is held sufficiently long that it repeats, then the initial down is followed additional key events with ACTION_DOWN and a non-zero value for getRepeatCount(). The last key event is a ACTION_UP for the key up. If the key press is canceled, the key up event will have the FLAG_CANCELED flag set. Key events are generally accompanied by a key code (getKeyCode()), scan code (getScanCode()) and meta state (getMetaState()). Key code constants are defined in this class. Scan code constants are raw device-specific codes obtained from the OS and so are not generally meaningful to applications unless interpreted using the KeyCharacterMap. Meta states describe the pressed state of key modifiers such as META_SHIFT_ON or META_ALT_ON.
As mentioned here, Usually, you should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times, or use code like this: if(keyCode==20&&event.getRepeatCount()==0)
For catching single event of onKeyDown and onKeyLongPress, refer to this SO.

0 Comments:

Post a Comment