16

i want to get the key value when user pressed any key and also perform action based on the key pressed in android.

e.g. if user pressed 'A' key then i want to get that value,compare,do something.

James Oravec
  • 19,579
  • 27
  • 94
  • 160
Hiren Dabhi
  • 3,693
  • 5
  • 37
  • 59

3 Answers3

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) 
    {
    }
);
Zelimir
  • 11,008
  • 6
  • 50
  • 45
  • This only captures hard key events like back and menu but not soft input key events. – ozmank Nov 17 '11 at 14:44
  • Are you sure in that? Works correctly for me for letters as well. On which phone you tested? – Zelimir Nov 17 '11 at 15:08
  • @Zelimir please this is my question if u ans it, it would be very kind of you thanks.. http://stackoverflow.com/questions/21746279/android-how-to-display-welcome-message-when-touch-mouse-or-keyboard?noredirect=1#comment32892054_21746279 – Farhan Shah Feb 13 '14 at 06:41
  • @Farhan Shah - could you please explain in a more detail what are you doing, and what is the problem you observe. – Zelimir Feb 14 '14 at 11:43
  • @Zelimir my issue is that if no one touch the keyboard or mouse for 3 minutes then it start message(at this point i am stuck),how i can implement this point? – Farhan Shah Feb 14 '14 at 11:55
  • or for example if the user dont touch the device screen for 3minutes then start message..please any help will be much much appreciated.. – Farhan Shah Feb 14 '14 at 11:57
  • @Farhan Shah - If you do not know how to start Action after some time, please investigate: http://stackoverflow.com/questions/4177409/java-android-how-to-start-an-asynctask-after-3-seconds-of-delay – Zelimir Feb 14 '14 at 12:18
  • @Farhan Shah - So, you want to display some Popup (Dialog) screen after some time? – Zelimir Feb 14 '14 at 12:19
  • @Zelimir i implemented that..but the issue is that the dialoge show regularly after 3mintues,and i just want when user not touch the device,i mean no one can touch the mouse or keyboard for 3minutes then the custom popup will display..can u get my point? – Farhan Shah Feb 14 '14 at 12:29
  • @Farhan Shah - OK, understood. Then you need to disable triggering when user press the screen. To do that, please use handler.removeCallbacks(yourRunnable). – Zelimir Feb 14 '14 at 12:35
  • Note: To filter out modifier keys add if (unicodeChar != '\u0000') in your handling of events. – Igor Čordaš Apr 05 '16 at 10:16
  • could you kindly explain how do you "use" text change listener? i cant see how onKeyDown and text change listener connected? how do i trigger an action if the user pressed space?? – jakson Oct 01 '17 at 03:50
  • `onKeyDown` is not called if the input language is other than English. – Violet Giraffe Jun 18 '20 at 15:14
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.

jfcogato
  • 3,359
  • 3
  • 19
  • 19
2

Try this out, this works perfectly well.

@Override
public boolean onKeyUp (int keyCode, KeyEvent event){
char c = (char) event.getUnicodeChar();
//Do something....
return super.onKeyUp(keyCode, event);
}