CodeGuy
CodeGuy

Reputation: 28905

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

Upvotes: 1

Views: 3555

Answers (2)

michael_s
michael_s

Reputation: 2575

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;

Upvotes: 2

mustaphahawi
mustaphahawi

Reputation: 297

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) + ".");
}   

Upvotes: 0

Related Questions