Solved

sendkeys in java?

Posted on 1999-01-28
5
1,606 Views
Last Modified: 2012-06-27
I usually work with vb and occasionally use sendkeys even though I understand that its bad programming practice. Is there a way to sendkeys with java?
0
Comment
Question by:GoofyJoe99
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 1234870
What is send keys? Post key events in the System queue? I can give you an example of that, if this is what you need.
0
 

Author Comment

by:GoofyJoe99
ID: 1234871
Sendkeys is a small method that is equivalent to pressing a key on the keyboard.
Ex.- In VB        
SendKeys "{Tab}"      - is equivalent to hitting the tab key on the keyboard
SendKeys "{Enter}"   - hits the enter key

Can any of this be done in java?
0
 
LVL 8

Accepted Solution

by:
diakov earned 50 total points
ID: 1234872
Yes. You have to construct a KeyEvent and push it in the system queue. Here is the code fragment:

//get the system event queue
EventQueue q = this.getToolkit().getSystemEventQueue();

//play some events
long time =  System.currentTimeMillis();

//ttt is the text component to which you send the keystrokes.
//note, it must be visible.
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);

//the key events go in triads. key pres, key released and then key typed. This will trigger the desired behaviour.

Cheer,
  Nik


0
 
LVL 8

Expert Comment

by:diakov
ID: 1234873
Btw, this is not an 'easy' question :-)
0
 

Author Comment

by:GoofyJoe99
ID: 1234874
ha, your right diakov, this is not an easy question. The only reason I thought it was easy is because its so easy to do in VB. I realize that its a complicated process in Java and I better start diggin my brain.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question