4

What can I import to simulate a keyboard press in java?

So for example I can use it to make a programme to automatically press the "a" key when an event occurs.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Charles
  • 493
  • 7
  • 14

1 Answers1

10

Everything you need is in java.awt.Robot

Example:

Robot robot = new Robot(); 
robot.keyPress(KeyEvent.VK_A); 
Samuel Rossille
  • 18,940
  • 18
  • 62
  • 90
  • Can I use this to for example, make a character fire continuously in a internet game unrelated to the java program, by using it rapidly tap a key when another key is pressed? – Charles May 27 '12 at 10:58
  • Yes you can. The robot simulates what happen if you really type the keyboard. The window with the focus will receive the keyboard event. – Samuel Rossille May 27 '12 at 13:46