0

I am trying to simulate keyboard input by programmatically generating KeyEvent objects and pumping them to the event queue. This works fine except that when characters are being entered into a JTextField, for example, the cursor (caret?) does not move to always be at the end of the entered value. For example, if we denote the caret as the pipe | then this is what I get:

An 'A' keypress is simulated by sending a KEY_PRESSED, KEY_TYPED, KEY_RELEASED event, and the JTextField value is:

|A

that is, the cursor/caret is back at the beginning of the field after the A is entered.

How do I get the cursor/caret to automatically move as it would when actual physical keys are pressed?

mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

2

Have you tried using the Robot Class in the JDK?

http://docs.oracle.com/javase/6/docs/api/java/awt/Robot.html

theon
  • 14,170
  • 5
  • 51
  • 74
0

After you issue each command, call a method that uses setCaretPosition() to the end of the text in the JTextField. This will be much easier if you use a J*Pane so you can call getDocument() and you will have much more control.

Mitch Connor
  • 766
  • 10
  • 19