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
> 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
You can use KeyEvent.getDisplayLabel() to get the primary character for the key. In other words, the label that is physically printed on it.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With