Last active
July 14, 2023 23:28
-
-
Save sonique6784/ec56174d0e09a82e157fe9dd5d136ff7 to your computer and use it in GitHub Desktop.
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
/* Copyright 2023 Cedric Ferry. | |
SPDX-License-Identifier: Apache-2.0 */ | |
class MainActivity : ComponentActivity() { | |
// activity top level interception of the keyboard events | |
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean { | |
val ctrl_w = KeyShortcut(KeyEvent.KEYCODE_W, ctrl = true) | |
val ctrl_r = KeyShortcut(KeyEvent.KEYCODE_R, ctrl = true) | |
event?.let { | |
if(ctrl_w.match(event)) { | |
// action on CTRL-W | |
return true | |
} else if(ctrl_r.match(event)) { | |
// action on CTRL-R | |
return true | |
} | |
} | |
return super.onKeyUp(keyCode, event) | |
} | |
} | |
// Optional utility class to create keyboard shortcuts | |
class KeyShortcut( | |
val key: Int, | |
val ctrl: Boolean = false, | |
val shift: Boolean = false, | |
val alt: Boolean = false, | |
val meta: Boolean = false, | |
) { | |
// check if the KeyEvent match the shortcut | |
fun match(keyEvent: KeyEvent): Boolean { | |
return keyEvent.keyCode == key | |
&& keyEvent.isAltPressed == alt | |
&& keyEvent.isCtrlPressed == ctrl | |
&& keyEvent.isMetaPressed == meta | |
&& keyEvent.isShiftPressed == shift | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment