Android KeyCode Reference Table - Android Key Code Reference and Mapping
In Android development, understanding the KeyCode corresponding to each key is fundamental for handling key events. This article provides a complete Android KeyCode reference table to help developers quickly find the KeyCode values for common keys. Whether physical keys, virtual keys, or function keys, the corresponding KeyCode can be found in this table.
What is Android KeyCode?
Android KeyCode is an integer value representing different keys on the device. Each key (such as "back key," "volume up key") has a unique KeyCode used to process user input events. KeyCodes are very important when writing key event listeners.
Common Android KeyCode Reference Table
Below are the common keys in Android and their corresponding KeyCode values:
| Key Name |
KeyCode Value |
Description |
| Back Key |
4 |
Physical back key, used to return to the previous level |
| Volume Up Key |
24 |
Volume increase key |
| Volume Down Key |
25 |
Volume decrease key |
| Power Key |
26 |
Device power key |
| HOME Key |
3 |
Return to the home screen |
| MENU Key |
82 |
Pop-up menu |
| Camera Key |
27 |
Launch camera |
| Search Key |
84 |
Launch search functionality |
| Forward Key |
92 |
Browser forward key |
| Backward Key |
93 |
Browser backward key |
| Up and Down Arrow Keys |
19, 20 |
Navigation keys, up and down arrows |
| Left and Right Arrow Keys |
21, 22 |
Navigation keys, left and right arrows |
How to Use KeyCode in Android?
In an Android application, you can obtain the KeyCode by listening for key events. For example, when the user presses the back key, you can determine the specific action by checking the KeyCode value:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Handle back key down event
return true;
}
return super.onKeyDown(keyCode, event);
}
Conclusion
Understanding and utilizing Android KeyCode is foundational knowledge in Android development, especially when handling key events. By mastering Android key codes and KeyCode mapping, you can precisely control user interactions and optimize the user experience of your applications.