Search code examples
javakeyevent

Convert KeyEvent.VK_[value] to String


Given an integer which comes from KeyEvent.VK_[value], such as KeyEvent.VK_SPACE or KeyEVENT.VK_F3, I'd like to convert it to the String of the key.

For example keyIntToString(KeyEvent.VK_SPACE) would return "Space"

How can I do this?


I've tried

KeyEvent.getKeyText(KeyEvent.VK_SPACE);

In the terminal, if I print that, I get "Space". In Eclipse if I print that, I get "?". In a JLabel if I display that, I get a square.

What I really want is to get it into a JLabel. How do I do this?


Solution

  • Something like this

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

    Also you can use getChar method in KeyEvent