Search code examples
javaswingkeylistenerkeyevent

Convert KeyEvent.VK_SPACE to a String space


How can I convert KeyEvent.VK_SPACE (which is an integer) to an actual space?

String space = convertKeyEvent(KeyEvent.VK_SPACE);
System.out.println("Space=" + space + ".");

This should output

Space= .

And although this example uses Space, it should be a general converter for other characters as well, such as VK_0.


WHAT I HAVE TRIED

Note that this DOES NOT work:

String space = KeyEvent.getKeyText(KeyEvent.VK_SPACE);
System.out.println("Space=" + space + ".");

In Eclipse, this outputs:

Space=?.

Solution

  • well - uh - did you once look at the actual value of VK_SPACE? It is 0x20 - that is the actual ASCII-value of space - that should give you some hint ;)

    try:

    char space = (char)VK_SPACE;