8

I want to get the char value of the KeyCode Event when pressing an Android keyboard.

public void KeyCodeEventAsChar(int keyCode, KeyEvent event) {

  char pressedKey;

  // TODO: convert key Code Event into char

  syslog.d ("TEST", "The pressed key in Android keyboard was char: " + pressedKey);

}

Does anyone has a clue how to do this?!

UPDATE:

I don't want hardcoded text! I want to convert it to correspondent char!

UPDATE 2:

I need also to dead chars such as: à, á, ò, ó, etc...

ANSWER FOUND:

// TODO: convert key Code Event into char
char pressedKey = (char) event.getUnicodeChar();

IMPORTANT NOTE: char has only 1 byte length, so it will not support several chars

Jorge
  • 695
  • 2
  • 9
  • 21
  • in Java `char` is UTF-16, so it can keep all the [Basic Multilingual Plane](http://en.wikipedia.org/wiki/Plane_%28Unicode%29#Basic_Multilingual_Plane) – bigstones Aug 12 '12 at 18:17

5 Answers5

12

> I had already answered updating the question

// TODO: convert key Code Event into char
char pressedKey = (char) event.getUnicodeChar();

IMPORTANT NOTE: char has only 1 byte length, so it will not support several chars

Hope it help you somehow

Jorge
  • 695
  • 2
  • 9
  • 21
4

In order to test the Answer Found you can do this:

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

    char pressedKey = (char) event.getUnicodeChar();

    Log.d("onKeyUp is: ", Character.toString(pressedKey));


    return super.onKeyUp(keyCode, event);

}

And the letter that you pressed should appear in the logcat

Maduro
  • 713
  • 5
  • 23
  • 44
3

You can use KeyEvent.getDisplayLabel() to get the primary character for the key. In other words, the label that is physically printed on it.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
1

You have to check which key is getting pressed like this

if(keyCode  == KeyEvent.KEYCODE_ENTER){

syslog.d ("TEST", "The pressed key in Android keyboard was Enter" );
}

Here is the link where you find all the KeyEvents

http://developer.android.com/reference/android/view/KeyEvent.html

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
Sachin Gurnani
  • 2,444
  • 7
  • 36
  • 45
0
KEYCODE_XX is an int

You can have a method that has keycode as parameter and use switch case in it to do specifc task. like below,

void getEvent(KeyEvent keyEvent){

    char pressedKey = (char) keyEvent.getUnicodeChar();

    switch(pressedKey){
        case A://
        syslog.d ("TEST", "The pressed key in Android keyboard was Enter" );
        //DO what you wnat with this event
        break;
        case B:
        syslog.d ("TEST", "The pressed key in Android keyboard was B" );
        //DO what you wnat with this event
        break;
        // so on to check other keys
    }
}

This way you can use Unicode to get corresponding values and you can use this with key listener to get events continuously

Its not blank
  • 3,055
  • 22
  • 37
  • @Vincent... I don't want that hardcoded. For int event=KeyEvent.KEYCODE_G, i can translate that to "G"! – Jorge May 16 '12 at 13:52