Key code for <> signs for java robot class? - java

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.

Related

Java Swing: KeyBinding for KEY_TYPED event

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");

Creating a Java Combination Lock

I'm working on a computer science assignment, and I can't for the life of me figure out why this isn't working.
We're supposed to create a combination lock (via a constructor) that prompts the user for letters that spell out the password.
The kicker is that it has to allow the user to make a few mistakes at first. For example, if the password were
D
O
G
the user could type in
A
B
C
D
O
G
And it would unlock the combination lock.
The attached code is probably my... Fifth attempt at this, and i'm not quite sure why it's not working. Any ideas?
EDIT:
Code on ideone:
http://ideone.com/D3yFYt
http://ideone.com/jzMNjJ
Thanks in advance!!
Maybe I am not getting the explanation correctly, so early apologies... But why don't you merge all input and test if the input .endsWith("DOG"). If that's not the point can you list a few more scenarios?
You may want to take a look at what exactly is in your first String variable. I guess you have a line feed ('\n') at the end of it.
So replace first.equals(num1) by first.startsWith(num1) and it should work better (untested, might not be the only bug).

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?

How to recognize key press events in Java

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?

Categories

Resources