Last active
December 6, 2021 06:02
-
-
Save anddam/7276579 to your computer and use it in GitHub Desktop.
Programmatically dispatch KeyEvent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KeyEvent powerKeyUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_POWER); | |
KeyEvent powerKeyDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER); | |
powerKeyDown = powerKeyDown.changeTimeRepeat(powerKeyDown, SystemClock.uptimeMillis() - 100, 1); | |
dispatchKeyEvent(powerKeyDown); | |
if (dispatchKeyEvent(powerKeyUp)) | |
Log.d("PML", "The event was consumed in dispatchKeyEvent() ACTION_UP"); | |
else | |
Log.d("PML", "The event was NOT consumed in dispatchKeyEvent() ACTION_UP"); | |
//// this is an alternate way of dispatching the long press key event using KeyEvent.DispatcherState | |
// KeyEvent.DispatcherState powerKeyState = new KeyEvent.DispatcherState(); | |
// powerKeyState.performedLongPress(powerKeyDown); | |
// powerKeyDown.dispatch(this, powerKeyState, this); | |
// *** actual output: | |
// The event was NOT consumed in dispatchKeyEvent() ACTION_UP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment