kryzb
asked on
Text field key press...
Can I simulate the press of a key in a JTextField programatically?
When my screen first draws, I want my program to press the 'cursor back' button in a certain field. What code do I need to do this?
Cheers.
Kryz.
When my screen first draws, I want my program to press the 'cursor back' button in a certain field. What code do I need to do this?
Cheers.
Kryz.
I have a more specific suggestion:
EventQueue q = this.getToolkit().getSyste mEventQueu e();
//play some events
long time = System.currentTimeMillis() ;
KeyEvent E1 = new KeyEvent((Component)ttt, KeyEvent.KEY_PRESSED, time - 7 , 0, KeyEvent.VK_A, 'a'); KeyEvent E2 = new KeyEvent((Component)ttt, KeyEvent.KEY_RELEASED, time - 6, 0, KeyEvent.VK_A, 'a'); KeyEvent E3 = new KeyEvent((Component)ttt, KeyEvent.KEY_TYPED, time - 5, 0, KeyEvent.VK_UNDEFINED, 'a');
q.postEvent(E1);
q.postEvent(E2);
q.postEvent(E3);
-------
Each key event consists of 3 simple events. you have to post sequences in order to mimic a normal typing behaviour. The typing will occur whereever the focus is, e.g. your textfield.
Cheers,
Nik
EventQueue q = this.getToolkit().getSyste
//play some events
long time = System.currentTimeMillis()
KeyEvent E1 = new KeyEvent((Component)ttt, KeyEvent.KEY_PRESSED, time - 7 , 0, KeyEvent.VK_A, 'a'); KeyEvent E2 = new KeyEvent((Component)ttt, KeyEvent.KEY_RELEASED, time - 6, 0, KeyEvent.VK_A, 'a'); KeyEvent E3 = new KeyEvent((Component)ttt, KeyEvent.KEY_TYPED, time - 5, 0, KeyEvent.VK_UNDEFINED, 'a');
q.postEvent(E1);
q.postEvent(E2);
q.postEvent(E3);
-------
Each key event consists of 3 simple events. you have to post sequences in order to mimic a normal typing behaviour. The typing will occur whereever the focus is, e.g. your textfield.
Cheers,
Nik
This is jdk 1.1 solution, it may have to be adjusted for the Java 2.
ASKER
As you mentioned, this is the 1.1 solution. Do you know what the JDK 1.2 version of 'postEvent' is, as I keep getting depricated warnings?
Cheers.
Kryz.
Cheers.
Kryz.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
textField.postEvent(new Event(textField, 8, "BACKSPACE"));
but I don't think that it wil work ...