Recently made Android TV system customization, the above arrangement I do a key board button multiplexing function: When the TV OSD menu is displayed when the ch+/ch-on the key board is the key on/off, the vol+/vol-is reused as the direction key right/left. The OSD menu is implemented using a fragment, so choose the OSD menu display (onresume) Trigger button multiplexing function, the OSD menu disappears (onStop) When the button reuse function. The idea of implementation is reasonable, and we know that the application handling key event Keyevnet is usually determined by the event's KeyCode value to determine what the program is going to do, so just handle the key function Onkey,onkeyup,onkeydown such as processing the key event before the KeyCode value of the event can be used to achieve the re-use of keys, specifically, this is to change the Keycode_channel_up/keycode_channel_down to Keycode_dpad_up/keycode_dpad _down the same keycode_volume_up/keycode_volume_down changed to Keycode_dpad_right/keycode_dpad_left. The Dispatchkeyevent (KeyEvent event) is required to modify the key value before the function mentioned above, and the official interpretation of the method is to intercept the keystroke event before it is distributed to the window. All right, gossip less. The code is as follows:
1 Public Booleandispatchkeyevent (KeyEvent event) {2 LongDowntime=event.getdowntime ();3 LongEventtime =event.geteventtime ();4 intAction =event.getaction ();5 intCode =Event.getkeycode ();6 intrepeat =Event.getrepeatcount ();7 intMetaState =event.getmetastate ();8 Switch(code) {9 Casekeyevent.keycode_channel_up:Tenevent =Newkeyevent (Downtime, eventtime, action, KEYEVENT.KEYCODE_DPAD_UP, Repeat, metaState); One Break; A CaseKeyevent.keycode_channel_down: -event =Newkeyevent (Downtime, eventtime, action, Keyevent.keycode_dpad_down, Repeat, metaState); - Break; the Casekeyevent.keycode_volume_up: -event =Newkeyevent (Downtime, eventtime, action, Keyevent.keycode_dpad_right, Repeat, metaState); - Break; - CaseKeyevent.keycode_volume_down: +event =Newkeyevent (Downtime, eventtime, action, Keyevent.keycode_dpad_left, Repeat, metaState); - Break; + } A at return Super. Dispatchkeyevent (event); -}
Android Button Reuse