Solved

sendkeys in java?

Posted on 1999-01-28
5
1,628 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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 …

707 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