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?
Related
I've been trying to learn keybinds by rewriting problems from my book that I've previously solved using KeyListener. The problem that I'm struggling to solve using keybinds requires me to record a message that's been typed and to display it on the panel.
The way it was solved using KeyListener is simply by recording characters with unicodes using the keyTyped() method and reading modifier/non-Unicode keys with keyPressed. If KeyEvent.VK_ENTER matches the keycode from the keyevent, then it displays the string on the panel.
~~~~~~~~
I thought that it can be solved in a similar way with KeyBinds. It says in the KeyEvent docs that KeyEvent.KEY_TYPED is fired every time a character is entered. I assumed that it meant every character with a corresponding Unicode being typed like how it works in KeyListener.
Later on, I realized that I have no idea how to retrieve the character since the Oracle tutorial on KeyBinds says that the KeyEvent is consumed when actionPerformed() is invoked.
This is the code that I THOUGHT would enable me to record typed keys to a StringBuilder using KeyBindings:
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.KEY_TYPED, 0), "recordTypedKey");
getActionMap().put("recordTypedKey", addCharToString);
Is there a way to obtain the characters that would invoke KeyListener's keyTyped() method besides adding a key to every one of them and using a separate Action event to record them?
Is there a way to obtain the characters that would invoke KeyListener's keyTyped() method besides adding a key to every one of them and using a separate Action event to record them?
I do not believe there is a global KeyStroke you can pass to the InputMap that will work similar to a KeyListener, as KeyBindings work on an individual key basis. You can however create a single Action and bind keys to it by looping over the char values you wish to process - in the ActionListener implementation you can obtain the value of the key via getActionCommand. For example to deal with a-z:
AbstractAction action = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
};
//loop over the ascii char values
for ( char a = 'A'; a <= 'Z'; a++ ){
panel.getInputMap().put(KeyStroke.getKeyStroke(Character.toString(a)), "recordTypedKey");
}
panel.getActionMap().put("recordTypedKey", action);
You can add modifiers if needed...for example to deal with the shift key (eg upper case),
panel.getInputMap().put(KeyStroke.getKeyStroke("shift " + Character.toString(a)), "recordTypedKey");
I want to create an array of all keys on the keyboard.
How many KeyCode are they ?
KeyEvent.getKeyCode() return ASCII or Virtual Key?
From the docs:
The key being pressed or released is indicated by the getKeyCode and getExtendedKeyCode methods, which return a virtual key code.
You can count the number of keycodes yourself from the doc of java.lang.Enum.KeyCode:
If you're doing this in order to detect the keyboard layout, forget the Keycodes. Instead do:
InputContext context = InputContext.getInstance();
System.out.println(context.getLocale().toString());
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.
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.
i m using the keyboard event on Robot Objects....
but each time i have to specify the keys individually....like
Robot r=new Robot();
r.KeyPress(KeyEvent.VK_A);
r.KeyPress(KeyEvent.VK_B);
r.KeyPress(KeyEvent.VK_C);
r.KeyPress(KeyEvent.VK_D);
is there any technique to get/recognize eachand every keys....not by specifying them individually....?
i m recieving the keycode from server side in keyCode variable....
so can i use this variable directly inplace of "KeyEvent.VK_D" like r.keyPress(keyCode);
The Key identifier is just an Int value. When the server value matches the java value, than you an directly put the value in. If not you have to create a Map where the server value references to the java key value.
I had the same issue during converstion between C++ Qt key events and Java Key events. The value also does not match. I had to create a mapping for this.
Take a look at the class KeyEvent. Every Key is listed their with a int value. You have to check if the value you get from the server matches with this int value. When the server value is not matching you have to create a Map. The map key is the server value and the map value is the corresponding KeyEvent valuke for the pressed key.
When the values matching, you dont have to create a map. You directly can use the server value for the Robot command.
Um... yes? The constants in KeyEvent.VK_D are merely there for your convenience. There is absolutely nothing wrong with using numerical values from somewhere else, as long as the same values are used for the same keys.
If I understand your question correctly, you're trying to get this action to take place whenever any key is pushed. Although I doubt I'm reading your question correctly, the solution to this would be to use KeyEvent.KEY_PRESSED and if you want an action or to set a variable when a key is released it would be KeyEvent.KEY_RELEASED. Although, I am a bit confused. Do you really want the same action to occur when every key is pressed or do you want a unique event for a set of keys?