Solved

sendkeys in java?

Posted on 1999-01-28
5
1,603 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 191
eclipse formatting 6 87
diffSum example 4 36
String array comparison 4 32
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
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 …

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now