🌐
Android Developers
developer.android.com › api reference › keyevent
KeyEvent | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Android Developers
stuff.mit.edu › afs › sipb › project › android › docs › reference › android › view › KeyEvent.html
KeyEvent - Android SDK | Android Developers
Call this during onKeyDown(int, KeyEvent) to have the system track the key through its final up (possibly including a long press).
🌐
GitHub
gist.github.com › arjunv › 2bbcca9a1a1c127749f8dcb6d36fb0bc
All Android Key Events for usage with adb shell · GitHub
def adb(command): os.system(f"adb shell {command}") class KeyMap: zero = "input keyevent 0" soft_left = "input keyevent 1" soft_right = "input keyevent 2" home = "input keyevent 3" back = "input keyevent 4" call = "input keyevent 5" endcall = "input keyevent 6" zer0 = "input keyevent 7" one = "input keyevent 8" two = "input keyevent 9" three = "input keyevent 10" four = "input keyevent 11" five = "input keyevent 12" six = "input keyevent 13" seven = "input keyevent 14" eight = "input keyevent 15" nine = "input keyevent 16" star = "input keyevent 17" pound = "input keyevent 18" dpad_up = "input
🌐
Android Developers
developer.android.com › core areas › ui › views › handle keyboard actions
Handle keyboard actions | Views | Android Developers
February 10, 2025 - override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean { return when (keyCode) { KeyEvent.KEYCODE_D -> { moveShip(MOVE_LEFT) true } KeyEvent.KEYCODE_F -> { moveShip(MOVE_RIGHT) true } KeyEvent.KEYCODE_J -> { fireMachineGun() true } KeyEvent.KEYCODE_K -> { fireMissile() true } else -> super.onKeyUp(keyCode, event) } }
🌐
Blogger
thecodeartist.blogspot.com › 2011 › 03 › simulating-keyevents-on-android-device.html
Simulating keypress events on Android | The Code Artist
If a keylayout file is not specified, Android will default to /system/usr/keylayout/qwerty.kl · Now to generate a event of a specific keycode, we simply execute this on the terminal/serial console: ... The value of keycode can be any of the integer values from the above table. In case a serial-console is NOT available on your device, you can always run the command via adb as follows: adb shell input keyevent <keycode> That's how a hardware-button-press is simulated in software!!
🌐
Headspin
headspin.io › home › blogs › simulating hardware keys and key events on android
Simulating Hardware Keys And Key Events On Android
September 12, 2018 - Check out: Capturing App Launch Metrics on Android · Appium enables us to actually generate KeyEvents, making the system (and thus any apps under test) believe that a user actually triggered the events. We can construct KeyEvents with each of the three components above (keycode, metastate, and flag).
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › android.views.keyevent
KeyEvent Class (Android.Views) | Microsoft Learn
[<Android.Runtime.Register("android/view/KeyEvent", DoNotGenerateAcw=true)>] type KeyEvent = class inherit InputEvent interface IParcelable interface IJavaObject interface IDisposable interface IJavaPeerable
🌐
Google Play
play.google.com › store › apps › details
KeyEvent Display - Apps on Google Play
I've had some problems getting ... out the following: KeyEvents: The KeyEvents as Android understands them (KeyUp, KeyDown, KeyLongPress, KeyMultiple) LogCat: Any relevant messages ......
Rating: 4.2 ​ - ​ 167 votes
Find elsewhere
🌐
GitHub
gist.github.com › ctrl-freak › 9abb5aea0d89d7bd9df6a3d0ac08b73c
Android ADB `adb shell input keyevent` Codes · GitHub
Android ADB `adb shell input keyevent` Codes. GitHub Gist: instantly share code, notes, and snippets.
🌐
Tabnine
tabnine.com › home page › code › java › android.view.keyevent
android.view.KeyEvent java code examples | Tabnine
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (Integer.parseInt(android.os.Build.VERSION.SDK) > 5 && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { Log.d("CDA", "onKeyDown Called"); onBackPressed(); ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › android.views.keyevent.-ctor
KeyEvent Constructor (Android.Views) | Microsoft Learn
KeyEventActions · Action code: either #ACTION_DOWN, #ACTION_UP, or #ACTION_MULTIPLE. code · Keycode · The key code. Attributes · RegisterAttribute · Create a new key event. Java documentation for android.view.KeyEvent.KeyEvent(int, int). Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Top answer
1 of 3
28

Answer to this question should be twofold. It is determined by the way how the key was generated. If it was press on the hardware key, then both approaches described below are valid. If it was press on the software key, then it depends on actual context.

1.) If key was result of the pressing on the soft keyboard that was obtained by long press on the Menu key:

You need to carefully override the following function:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) {

    switch (keyCode) {
        case KeyEvent.KEYCODE_A:
        {
            //your Action code
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

2.) If your activity contains EditText, and softkeyboard was obtained from it, then first approach does not work because key event was already consumed by EditText. You need to use text changed Listener:

mMyEditText.addTextChangedListener(new TextWatcher()
{
    public void afterTextChanged(Editable s) 
    {
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after) 
    {
        /*This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length after. It is an error to attempt to make changes to s from this callback.*/ 
    }
    public void onTextChanged(CharSequence s, int start, int before, int count) 
    {
    }
);
2 of 3
5

Zelimir code works if your ssoo version is up to 2.3 If you have this code running in 2.3 it will not works at all, the way to control the event keys in all ssoo version is with dispatchKeyEvent()

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.d("hello", String.valueOf(event.getKeyCode()));
        return super.dispatchKeyEvent(event);
    }

With this, you can control the key pressed in a webview or wherever you are. The only bug is that you can not control the "action button" ... don't know why.

🌐
Vskills
vskills.in › home › keyboard events handling in android
Keyboard events handling in Android - Tutorial
April 12, 2024 - In Android, keyboard events are handled using the KeyEvent class.
🌐
Medium
medium.com › @wanxiao1994 › how-android-dispatch-keyevent-and-perform-focus-navigation-8565327bd12e
How Android dispatch KeyEvent and perform focus navigation | by Wan Xiao | Medium
June 12, 2022 - If you press the arrow keys on D-pad, Android will exit TouchMode, and the system will find a focusable view on the screen to request the focus. If you open an APP in non-TouchMode, the system will also find a focusable view in the APP’s window to request the focus by default. This step is done in ViewRootImpl.performTraversals. In the image below, you can see that the focus is now on Settings. ... The biggest difference in the dispatch of KeyEvent and TouchEvent is the focus, which is why it is relatively simple.
🌐
GitHub
github.com › alt236 › KeyEvent-Display---Android
GitHub - alt236/KeyEvent-Display---Android: This application will display all Button press related messages (using Android KeyEvents, and logcat & kmsg parsing). Useful when porting Android to different hardware and buttons don’t work.
This application will display all Button press related messages (using Android KeyEvents, and logcat & kmsg parsing). Useful when porting Android to different hardware and buttons don’t work. - alt236/KeyEvent-Display---Android
Starred by 26 users
Forked by 9 users
Languages   Java 82.3% | HTML 16.4% | Shell 1.3%