Android - How to convert KeyEvent KeyCode to Char?

I want to get the char value of the KeyCode event when I click on the 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 know how to do this?

UPDATE:

I do not need hard code! I want to convert it to the corresponding char!

UPDATE 2:

I also need dead characters, such as: ร , รก, รฒ, รณ, etc.

ANSWER TO FIND:

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

IMPORTANT NOTE: char is only 1 byte long, therefore does not support multiple characters

+4
source share
5 answers

I already answered the question update

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

IMPORTANT NOTE: char is only 1 byte long, so it does not support multiple characters

Hope this helps you somehow

+6
source

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

+2
source

You have to check which key is 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 will find all KeyEvents

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

+1
source

To check the Found answer , 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 email you clicked should appear in logcat

+1
source
 KEYCODE_XX is an int 

You can have a method with key code as a parameter and use a switch case in it to complete the task. as shown 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 } } 

That way you can use Unicode to get the appropriate values, and you can use it with a key listener to continuously receive events.

0
source

Source: https://habr.com/ru/post/1412773/


All Articles