Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate Keyboard press in java

I want to simulate entering a string (can contain any character) in JTextField. I am using new KeyEventData() for that. But I am not able to determine how to handle characters like {, }, ) etc and also how to append a new character to already entered characters.

like image 326
user1247412 Avatar asked Aug 31 '12 20:08

user1247412


2 Answers

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

keyPress(KeyEvent.VK_SHIFT);
keyPress(KeyEvent.VK_OPEN_BRACKET);
keyRelease(KeyEvent.VK_OPEN_BRACKET);
keyRelease(KeyEvent.VK_SHIFT);
like image 172
trashgod Avatar answered Sep 28 '22 09:09

trashgod


This might be helpful for emulating keyevents: How to simulate keyboard presses in java?

For left and right brace, KeyEvent.VK_BRACELEFT and KeyEvent.VK_BRACERIGHT http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html#VK_BRACELEFT

Hope this helps =]

like image 40
corgichu Avatar answered Sep 28 '22 09:09

corgichu