?
Solved

sendkeys in java?

Posted on 1999-01-28
5
Medium Priority
?
1,638 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

765 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