Solved

sendkeys in java?

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 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