1

I have something similar to the following: An object with the value of KeyEvent.VK_G. How can I get the key letter from that object (as a String)?

1
  • KeyEvent.VK_G is an int. How can an Object hold this value? Is it an Integer? Commented Sep 26, 2011 at 21:47

2 Answers 2

7

By using KeyEvent.getKeyChar() (see http://download.oracle.com/javase/6/docs/api/java/awt/event/KeyEvent.html#getKeyChar()).

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but remember, it is represented by an Object variable, not a KeyEvent variable..
So cast it? ((KeyEvent) event).getKeyChar()
Where are you getting the integer from? Presumably a KeyEvent object. Use that object instead of the integer.
The integer comes from the VK_G part, i presume
What I’m saying is you probably have the event object somewhere.
3

Is this what you want to do?

int someKeyCode = KeyEvent.VK_G;
Object someKeyCodeObject = new Integer(someKeyCode);
String keyString = KeyEvent.getKeyText((Integer)someKeyCodeObject);

Which would give "G" in this case.

3 Comments

Its can idea, but it says that I cannot convert my Object variable into an int, help?
So what is your object? Is it an Integer? Is it an Integer cast down to an Object? If so, do KeyEvent.getKeyText((Integer)yourObject)
Thanks! LOL I was using (int) myObject instead of (Integer) myObject, heh heh ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.