Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get keyCode in android

Tags:

android

I have the following code:

tv.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
         switch(keyCode) 
            {
                case KeyEvent.KEYCODE_0:
                    Log.v(TAG, "Keycode_0");
                    return true;
                    //break;

                case KeyEvent.KEYCODE_1:
                    Log.v(TAG, "Keycode_1");
                    return true;
                    //break;
                case KeyEvent.KEYCODE_D:
                    Log.v(TAG, "Keycode_D");
                    return true;
                    //break;
                case KeyEvent.KEYCODE_2:
                    Log.v(TAG, "Keycode_2");
                    return true;
                    //break;
                case KeyEvent.KEYCODE_TAB:
                    Log.v(TAG, "Keycode_TAB");
                    return true;
                    //break;
                case KeyEvent.KEYCODE_DEL:
                    Log.v(TAG, "Keycode_DEL");
                    return true;
                    //break;
                case KeyEvent.KEYCODE_COMMA:
                    Log.v(TAG, "Keycode_COMMA");
                    return true;
                    //break;
                case KeyEvent.KEYCODE_ENTER:
                    Log.v(TAG, "KEYCODE_ENTER");
                    i=KeyEvent.KEYCODE_ENTER;
                    Log.v(TAG, "KEYCODE_ENTER"+i);
                    return true;
                    //break;
               case KeyEvent.KEYCODE_SHIFT_LEFT:
                    Log.v(TAG, "KEYCODE_SHIFT_LEFT"+KeyEvent.KEYCODE_SHIFT_LEFT);
                    return true;
                    //break;
                case KeyEvent.KEYCODE_SHIFT_RIGHT:
                    Log.v(TAG, "KEYCODE_SHIFT_RIGHT"+KeyEvent.KEYCODE_SHIFT_RIGHT);
                    return true;
                    //break;
                case KeyEvent.KEYCODE_SPACE:
                    Log.v(TAG, "KEYCODE_SPACE"+KeyEvent.KEYCODE_SPACE);
                    return true;
                    //break;
                default:
                    break;
            }
            return false;
        }
    });

but it is working only for enter and delete keys. For the rest of the keys, it is not working. I wanted it for shift and caps lock keys.

like image 582
Namratha Avatar asked Dec 27 '11 11:12

Namratha


People also ask

What is a key in Android?

The Android Keystore system lets you store cryptographic keys in a container to make it more difficult to extract from the device. Once keys are in the keystore, they can be used for cryptographic operations with the key material remaining non-exportable.

What is KeyCode in keyboard?

Key codes are numeric values that correspond to physical keys on the keyboard but do not necessarily correspond to a particular character.


1 Answers

Uset this listener on your editText:

editText.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {

return false;
}
});
like image 105
Mahesh Avatar answered Sep 23 '22 04:09

Mahesh