I’m trying to simulate text input into a JTextField. I’ve got a 1 char long string containing the letter I want to add and I run:
receiver.dispatchEvent(new KeyEvent(this,
KeyEvent.KEY_TYPED, 0,
this.shifted?KeyEvent.SHIFT_DOWN_MASK:0,
KeyEvent.VK_UNDEFINED, text.charAt(0)));
But this doesn’t seem to change the contents at all. What am I missing here?
Looks like a virtual keyboard to me 🙂
Almost the exact same code does work for me. I would suggest the following:
Pass the target
JTextField(in your case,receiver) as thesourceparameter to theKeyEventconstructor, i.e.:Ensure your target
JTextFieldhas the focus.Edit:
Just to verify the above suggestion, I tested this snippet of code:
This does not work, however if the last line is modified as follows (target component as the
sourceparameter of theKeyEventconstructor), it works fine: