Java converting keycode to string or char

19,335

Solution 1

Cast it to Char.

Char c=(Char)keycode;

Solution 2

If e is the variable parameter of the KeyEvent, use

 e.getKeyText(e.getKeyCode())

But preferably use it in a static context of KeyEvent

KeyEvent.getKeyText(e.getKeyCode())

It returns a string but you can cast to or get value of string returned in other parsable data types

Solution 3

One way could be to load all the values in a Map and just get the value for each key pressed. Like if you press a SPACE-KEY and get input as 20, just get the value associated with that key from the map and use it.

Share:
19,335
user2098268
Author by

user2098268

Updated on July 28, 2022

Comments

  • user2098268
    user2098268 almost 2 years

    I want to turn keyevent keycode value into a string or a char value. Either one would do.

    For example, when I press 'SPACE', which 'lets say' has a keycode 20, and I want to convert that value to a char or a string. Is there any standard way of doing so, or I have to write a bunch of 'if' statements for every key on the keyboard?

  • user2098268
    user2098268 about 11 years
    that was simple O_O Thanks!