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)?
-
KeyEvent.VK_G is an int. How can an Object hold this value? Is it an Integer?Hovercraft Full Of Eels– Hovercraft Full Of Eels2011-09-26 21:47:02 +00:00Commented Sep 26, 2011 at 21:47
Add a comment
|
2 Answers
By using KeyEvent.getKeyChar() (see http://download.oracle.com/javase/6/docs/api/java/awt/event/KeyEvent.html#getKeyChar()).
5 Comments
Dylan Wheeler
Thanks, but remember, it is represented by an Object variable, not a KeyEvent variable..
Daniel Brockman
So cast it?
((KeyEvent) event).getKeyChar()Daniel Brockman
Where are you getting the integer from? Presumably a
KeyEvent object. Use that object instead of the integer.Dylan Wheeler
The integer comes from the VK_G part, i presume
Daniel Brockman
What I’m saying is you probably have the event object somewhere.
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
Dylan Wheeler
Its can idea, but it says that I cannot convert my Object variable into an int, help?
jornb87
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)Dylan Wheeler
Thanks! LOL I was using (int) myObject instead of (Integer) myObject, heh heh ...