🌐
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
github.com › AndroidSDKSources › android-sdk-sources-for-api-level-24 › blob › master › android › view › KeyEvent.java
android-sdk-sources-for-api-level-24/android/view/KeyEvent.java at master · AndroidSDKSources/android-sdk-sources-for-api-level-24
This is only a backup for Android SDK Sources for API Level 24 [Android 7.0]. - android-sdk-sources-for-api-level-24/android/view/KeyEvent.java at master · AndroidSDKSources/android-sdk-sources-for-api-level-24
Author   AndroidSDKSources
🌐
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)] public class KeyEvent : Android.Views.InputEvent, IDisposable, Java.Interop.IJavaPeerable
🌐
Tabnine
tabnine.com › home page › code › java › android.view.keyevent
android.view.KeyEvent java code examples | Tabnine
@Override public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) if (mEditorListener != null && mEditorListener.onEditorAction(this, android.R.id.closeButton, event)) return true; return super.onKeyPreIme(keyCode, event); } }
🌐
Appium
appium.github.io › java-client › io › appium › java_client › android › nativekey › KeyEvent.html
KeyEvent (java-client 6.1.0 API)
io.appium.java_client.android.nativekey.KeyEvent · public class KeyEvent extends java.lang.Object · clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait · public KeyEvent() public KeyEvent(AndroidKey key) public KeyEvent withKey(AndroidKey key) Sets the key code.
🌐
Android Developers
developer.android.com › core areas › ui › views › handle keyboard actions
Handle keyboard actions | Views | Android Developers
@Override public boolean onKeyUp(int keyCode, KeyEvent event) { switch (keyCode) { ... case KeyEvent.KEYCODE_J: if (event.isShiftPressed()) { fireLaser(); } else { fireMachineGun(); } return true; case KeyEvent.KEYCODE_K: if (event.isShiftPressed()) { fireSeekingMissle(); } else { fireMissile(); } return true; default: return super.onKeyUp(keyCode, event); } } Keyboard Shortcuts Helper: System screen that enables users to search the keyboard shortcuts your app offers. Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Find elsewhere
🌐
Luciad
dev.luciad.com › portal › productDocumentation › LuciadCPillar › docs › reference › android › com › luciad › input › KeyEvent.html
KeyEvent (LuciadCPillar Java Android API documentation)
java.lang.Object · com.luciad.input.KeyEvent · All Implemented Interfaces: IInputEvent, AutoCloseable · public final class KeyEvent extends Object implements IInputEvent, AutoCloseable · Implementation of IInputEvent that represents keyboard input. This class contains the button state, ...
🌐
Headspin
headspin.io › home › blogs › simulating hardware keys and key events on android
Simulating Hardware Keys And Key Events On Android
September 12, 2018 - But how do we construct the keyEvent parameter? It must be of type KeyEvent, and at a minimum we have to provide a keycode, which is an integer (represented as the AndroidKey enum for the Java client). So, let's say we wanted to simulate a click of the hardware "Back" button on some Android devices.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › android.views.keyevent.-ctor
KeyEvent Constructor (Android.Views) | Microsoft Learn
The new event time (in android.os.SystemClock#uptimeMillis) of the event. ... The new repeat count of the event. ... Copy an existing key event, modifying its time and repeat count. This member is deprecated. Use #changeTimeRepeat(KeyEvent, long, int) instead. Java documentation for android.view.KeyEvent.KeyEvent(android.view.KeyEvent, long, int).
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.