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
  • 166,747
  • 40
  • 210
  • 420
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
  • 17,907
  • 18
  • 59
  • 87
  • 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