title: Dispatching key events on JTextArea doesn't move the caret
body: <p>I want to simulate key presses on a JTextArea. I would use the Robot class, but the window I want to type in doesn't have focus. So I have a scenario like this:</p>
  
  <pre><code>public class Test {
    public static void main(String[] args) {
      Frame frame = new Frame();
      JTextArea text = new JTextArea();
      frame.add(text);
      frame.pack();
      frame.setVisible(true);
  
      text.dispatchEvent(new KeyEvent(text,
            KeyEvent.KEY_TYPED, 0,
            0,
            KeyEvent.VK_UNDEFINED, 'H'));
      text.dispatchEvent(new KeyEvent(text,
            KeyEvent.KEY_TYPED, 0,
            0,
            KeyEvent.VK_UNDEFINED, 'L'));
    }
  }
  </code></pre>
  
  <p>But after the H is typed, the caret is not moved right, which causes L to be typed before H: the final text in the area is LH, but I want it to be HL.</p>
  
  <p>I can dispatch a new key event in between H and L that would move the caret right (the right arrow) or call setCaretPosition and that would work, but I am searching for a solution that doesn't move the caret manually and behaves just like a person typing (I am making a tester for testing assignments from students).</p>
  
  <p>Any ideas?</p>