1

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=?.
4

2 に答える 2

2

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;
于 2013-02-12T16:43:23.990 に答える
0
public static String convert(int keyEvent){
    if(keyEvent == 32){
        return " ";
    }
    return null;
}
public static void main(String [] arg){
    System.out.println("Space=" + convert(KeyEvent.VK_SPACE) + ".");
}   
于 2013-02-12T16:37:34.387 に答える