Detect which soft key was pressed Android?

2k Views Asked by At

I was trying to detect which key was pressed, so far I was able to detect SHIFT,BACKSPACE and ENTER with dispatchKeyEvent() method but it seems that it won't detect the letters and numbers. And iv'e also tried the onKeyDown and onKeyUpand they don't even detect the SHIFT,BACKSPACE and ENTER let alone the alphabet and letters. My goal is to type a letter in my EditText and detect the KeyStroke and display it with Toast

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
   int keyaction = event.getAction();
   if(keyaction == KeyEvent.ACTION_DOWN )
    {
        String keycode = event.getCharacters();
        Toast toast = Toast.makeText(this,String.valueOf(event.getKeyCode()), Toast.LENGTH_LONG);
        toast.show();
    }
    return super.dispatchKeyEvent(event);
}

This is detecting the Keycode for SHIFT,BACKSPACE and ENTER but not the alphabets

3

There are 3 best solutions below

1
On

use something like this :

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_D:
            //do something
            return true;
        case KeyEvent.KEYCODE_F:
            //do something
            return true;
        case KeyEvent.KEYCODE_d:
            //do something
            return true;
        case KeyEvent.KEYCODE_f:
            //do something
            return true;
        default:
            return super.onKeyUp(keyCode, event);
    }
}
0
On

The exact answer to what you're asking is this :-

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) 
    {
         // Do Code here
    }
else if(keyCode == KeyEvent.KEYCODE_0) 
   {

   }
//And so on
return super.onKeyDown(keyCode, event); }

You can read about it in the Constants section of this page.

However, from the last line of your question, I am guessing that you're approaching your requirement the wrong way. You really don't need the above code. Instead, just add a Text Changed Listener to your EditText and make a Toast when the text changes. Something like the following :-

 editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
           String text = s.toString();
           Toast.makeText(context,s.charAt(s.length()-1).toString(), Toast.LENGTH_LONG).show();
        }
    });
0
On

If you want to achieving to show toast on every charector press so you can use Edittext.addonTexchange listerner and in this onTextChange method you can show text what you pressed you can just get last enter charector and show to toast.

textView.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // here right logic for get last char and show it on toast

    }

});

And for action key like done and Backpress use setOnKeyListener and use different actions are there. t

extView.setOnKeyListener(new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
            if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                enterPressed();
                return true;
            }
            return false;
        }
});