Get string of the keyboard key - java

Is there any way to get the String text representation of a keyboard key?
Examples:
Input: User presses Shift
Output: "Shift"
Input: User presses F1
Output: "F1"
Input: User presses Left arrow key
Output: "Left Arrow"
Im using a keylistener in order to get the key strokes by the way

Closest to this is KeyEvent.getKeyText: http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html#getKeyText(int)
I say closest to because there are some platform-specific differences in implementation on certain keys

Related

Java getInputMap() for the enter key on numpad

So in my calculator program, I would like to set up the equals operation where it the mathematical problem could be computed by pressing the enter key (on the main keyboard) or by pressing the enter key (on the numpad). I know the numbers are VK_NUMPAD0 - VK_NUMPAD9, VK_ADD, VK_SUBTRACT, VK_MULTIPLY, VK_DIVIDE. However I can't seem to find one for the enter key. VK_ENTER is for the keyboards enter key. I also looked at KeyEvent.KEY_LOCATION_NUMPAD, but not sure how to add it to my code to get it to work. I have a method called SetupKeyInput() that takes some parameters that adds the key bindings for the inputmap and actionmap.
private void SetupKeyInput(JButton component, int keyStroke, int mask) {
component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(keyStroke, mask), "hmm");
component.getActionMap().put("hmm", new MyButtonEvents(component));
}
So, I can call the function like this
SetupKeyInput(buttonEquals, KeyEvent.VK_ENTER, 0); // Only works for keyboard enter
What am i missing for the numpad enter? I also tried this
SetupKeyInput(buttonEquals, KeyEvent.VK_ENTER, KeyEvent.KEY_LOCATION_NUMPAD);

Checking JTextField Characters

I have a program I've written for my kids to practice basic arithmetic. There is a JTextField on a JFrame where the student keys in the answer to a given math problem (like 8+8, they key in 16, Enter). This field accepts only integer values as entries by way of a DocumentFilter.
That part works just fine. What I want to do with this is to avoid having the user have to hit the enter key. Ideally, when the user keys in the first number (1), a check is done to see if 1 == 16. If not, nothing happens. When they subsequently type the 6, the text field displays the number 16. I want to run a check to verify if 16 == 16, and then handle the entry as if the user had also hit the Enter key.
txtAnswer.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
respondToAnswer(isTimed);
} else {
/* Check if the correct answer has been entered */
System.out.println(txtAnswer.getText());
//int userAnswer = Integer.parseInt(txtAnswer.getText());
//if (userAnswer == correctAnswer {
// respondToAnswer(isTimed);
//}
}
};
});
This code isn't quite working. It's as if it is one character behind. What I mean by that is when the user hits the '1' key (in my 8+8=16 example), the console output is an empty string. When they hit the '6' key, the output is '1'. If they then hit another integer key, the output is '16'. It's always one digit behind. Because of this, I cannot capture the entire contents of the text field to see if it matches the correct answer.
Anyone see what I am missing?
Thanks!
Use a DocumentListener for that instead of a KeyListener. In all three events, you will have access to the actual text content.
To listen for the Enter on JTextField, us an ActionListener
Side note: you should almost never need a KeyListener. On JTextComponent, always rely on a DocumentListener. For all the others, use appropriate Swing Key bindings. KeyListener is really a low level API.
You should use DocumentListener for this purpose. keyPressed is probably fired before the text in the field is updated.
See How to Write a Document Listener for details and examples.

KeyCode of backtick key

I'm trying to type some text into a text field using the Robot class in Java.
The problem I have is, that I can not discover the integer value of the backtick key (to the left of 1 on the keyboard).
I have no idea which one of the VK_ constants in the KeyEvent class it is.
At first I assumed it was 96, but pressing it using the robot gives me 0 (maybe 96 is the numpad zero).
What is the integer keycode for backtick?
VK_BACK_QUOTE is the code you want. It has integer value 192.
If microsoft layout editor is right, VK_OEM_3 . Does it make sense?

Key code for <> signs for java robot class?

can you tell me the keycodes for Java Robot class.
For example, to press Enter we use:
a.keyPress(KeyEvent.VK_ENTER);
a.keyRelease(KeyEvent.VK_ENTER);
I want to press these sysmbols > and < sign. What keyword or code should i use?
Thank you.
Assuming you mean java.awt.event.KeyInput, then a quick glance at the documentation suggests that KeyInput.VT_LESS and KeyInput.VT_GREATER would correspond to the < and > keys.
Did you try VK_LESS, and VK_LESS in combination with SHIFT for >?
You'll need to do a VK_SHIFT press event, followed by a VK_COMMA press, then a VK_COMMA release and a VK_SHIFT release (assuming QWERTY keyboard). The system will reject a VK_LESS press because that's not a valid key without a shift event.

Java/AWT/Swing: how to distinguish the pressed enter or return key

It looks like native applications behave differently if the user presses the Return key (right to the characters) or the Enter key (number key pad) - one time a new line character is inserted, the other time the default button is activated.
How I can distinguish both key presses from Java/AWT/Swing?
keyEvent.getKeyLocation() == KeyEvent.KEY_LOCATION_NUMPAD

Categories

Resources