How to simulate keystrokes in java

I want to simulate a string input (can contain any character) in a JTextField. I use the new KeyEventData () for this. But I can’t determine how to handle characters like {,},), etc., And also how to add a new character to already entered characters.

+5
source share
3 answers

You can use Robotfor this, as shown in the example. To get {, for example, you need to do something like this:

keyPress(KeyEvent.VK_SHIFT);
keyPress(KeyEvent.VK_OPEN_BRACKET);
keyRelease(KeyEvent.VK_OPEN_BRACKET);
keyRelease(KeyEvent.VK_SHIFT);
+4
source

This can be useful for emulating key events: How to simulate keystrokes in java?

, KeyEvent.VK_BRACELEFT KeyEvent.VK_BRACERIGHT http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html#VK_BRACELEFT

, =]

+2

check VK_BRACELEFT, VK_BRACERIGHT, VK_RIGHT_PARENTHESIS and VK_LEFT_PARENTHESIS to process these characters.

To add a character, you can create a list of characters or create a string.

edit: to get the value of a character from keyevent try KeyEvent.getKeyChar ()

+1
source

All Articles