Detect Which Hot Keys Is Being Pressed
How can I detect which key combination is being pressed? For example I want to recognize Back and Menu button being pressed simultaneously or any key combination. I would like to o
Solution 1:
I did a simple research and found this solution and it may work. You can detect Menu key pressed by the following code.
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == KeyEvent.ACTION_DOWN) {
//Start a new thread here and run a while loop for listening to "back pressed" and trigger the event you want if the back button is pressed
} else {
//stop the started thread abovereturnfalse;
}
}
Hope this might help you. Thnks.
Solution 2:
As Daniel Lew said (Prompt user to save changes when Back button is pressed):
You're not quite on the right track; what you should be doing is overriding onKeyDown() and listening for the back key, then overriding the default behavior:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.returntrue;
}
return super.onKeyDown(keyCode, event);
}
If you're only supporting Android 2.0 and higher, they've added an onBackPressed() you can use instead:
@OverridepublicvoidonBackPressed() {
// do something on back.return;
}
This answer is essentially ripped from this blog post: http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
Post a Comment for "Detect Which Hot Keys Is Being Pressed"