CodeGuy
CodeGuy

Reputation: 28905

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?

Upvotes: 0

Views: 1419

Answers (1)

vels4j
vels4j

Reputation: 11298

Something like this

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

Also you can use getChar method in KeyEvent

Upvotes: 1

Related Questions