Typing text automatically using Robot class

S

Sameer

Hello,
How to map character from a string to the corresponding
KeyEvent.VK_character?
I want to generate events to type a sentence.
Do I need to enter the corresponding integer constants in an array and
use it? Can it be simplified?


import java.awt.event.*;
import java.awt.*;

public class RobotDemo {
String string2Print;

RobotDemo(String str) {
string2Print=str;
char[] charArray=string2Print.toCharArray();
try {
Robot rob = new Robot();
for (int i = 0; i < charArray.length; i++) {
// CODE NEEDED HERE

rob.delay(1000);
}
rob.keyPress(KeyEvent.VK_ENTER);

}catch (AWTException awte) {
}

}

public static void main(String args[]) {
new RobotDemo("TESTING");
}

}


I even tried to put the KeyEvent.VK_... constant in a Hashtable as

ht.put("A", KeyEvent.VK_A)

but I was unable to retrieve the constant and use it for event.

I need generelised code which maps a character to corresponding
constant if possible.

Please help.

-Sameer
 
T

Thomas Weidenfeller

Sameer said:
How to map character from a string to the corresponding
KeyEvent.VK_character?

E.g. with a HashMap. Please note that you will have to generate multiple
keyPress()/keyRelease() calls for many characters, e.g. for uppercase
letters you need the sequence

keyPress(KeyEvent.VK_SHIFT);
keyPress(KeyEvent.VK_<some letter>);
keyRelease(KeyEvent.VK_SHIFT);

So the HashMap alone is not enough. You either need to test for
upper-case/lower-case separately, or you need to encode complete
press/release sequences in the map somehow.

But that's not all. The big problem is that all this depends on the
actual keyboard layout. E.g. on an English keyboard you would need a
but on a German keyboard you said:
I want to generate events to type a sentence.
Do I need to enter the corresponding integer constants in an array and
use it? Can it be simplified?

You could try your luck with KeyStrokes and try to make sense out of the
KeyStroke fields to figure out which sequence of virtual key codes you need.
I even tried to put the KeyEvent.VK_... constant in a Hashtable as

ht.put("A", KeyEvent.VK_A)

but I was unable to retrieve the constant and use it for event.

Then you have a bug in your code.

/Thomas
 
S

Sameer

I coded it this way but this throws an exception.

import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class RobotDemo {
String string2Print;

RobotDemo(String str) {
Hashtable ht = new Hashtable();
ht.put("A", new Integer(KeyEvent.VK_A));
ht.put("B", new Integer(KeyEvent.VK_B));
.................
.................
ht.put("Z", new Integer(KeyEvent.VK_Z));

string2Print = str;
try {
Robot rob = new Robot();
for (int i = 0; i < string2Print.length(); i++) {
String charStr = string2Print.substring(i, 1);
Integer ic = (Integer)ht.get(charStr);
rob.keyPress(ic.intValue());
rob.delay(1000);
}
rob.keyPress(KeyEvent.VK_ENTER);
} catch (AWTException awte) {
}
}

public static void main(String args[]) {
new RobotDemo("THEQUICKBROWNFOXJUMPSOVERALAZYDOG");
}
}


The output is given below.

C:\jdk1.5.0u1\bin>JAVA RobotDemo
TException in thread "main" java.lang.NullPointerException
at RobotDemo.<init>(RobotDemo.java:45)
at RobotDemo.main(RobotDemo.java:56)

The version of the jdk i am using is

java version "1.5.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing)

What may be the problem?

-Sameer
 
A

Andrew Thompson

I coded it this way but this throws an exception.

You posted 33 lines of code, but..
C:\jdk1.5.0u1\bin>JAVA RobotDemo
TException in thread "main" java.lang.NullPointerException
at RobotDemo.<init>(RobotDemo.java:45)
at RobotDemo.main(RobotDemo.java:56)

...lines 45, and 56? Not in your example!

Please prepare examples for usenet as SSCCE's.
<http://www.physci.org/codes/sscce.jsp>

Some other points.

You need to figure out how to sort these exceptions
yourself. Start here.
<http://www.physci.org/codes/javafaq.jsp#exact>, then read..
<http://www.physci.org/codes/javafaq.jsp#stacktrace>

And finally, a better group for beginners is.
<http://www.physci.org/codes/javafaq.jsp#cljh>
 
R

Roland

I coded it this way but this throws an exception.

import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class RobotDemo {
String string2Print;

RobotDemo(String str) {
Hashtable ht = new Hashtable();
ht.put("A", new Integer(KeyEvent.VK_A));
ht.put("B", new Integer(KeyEvent.VK_B));
.................
.................
ht.put("Z", new Integer(KeyEvent.VK_Z));

string2Print = str;
try {
Robot rob = new Robot();
for (int i = 0; i < string2Print.length(); i++) {
String charStr = string2Print.substring(i, 1);
Integer ic = (Integer)ht.get(charStr);
rob.keyPress(ic.intValue());
rob.delay(1000);
}
rob.keyPress(KeyEvent.VK_ENTER);
} catch (AWTException awte) {
}
}

public static void main(String args[]) {
new RobotDemo("THEQUICKBROWNFOXJUMPSOVERALAZYDOG");
}
}


The output is given below.

C:\jdk1.5.0u1\bin>JAVA RobotDemo
TException in thread "main" java.lang.NullPointerException
at RobotDemo.<init>(RobotDemo.java:45)
at RobotDemo.main(RobotDemo.java:56)

The version of the jdk i am using is

java version "1.5.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing)

What may be the problem?

-Sameer


String.substring takes two arguments, the begin index and the end index,
i.e. not the length. So try and replace
String charStr = string2Print.substring(i, 1);
by
String charStr = string2Print.substring(i, i+1);
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
 
S

Sameer

Dear Sir,
Thank you for giving link to your article about 'Short, Self Contained,
Correct (Compilable), Example'.
But I think the example does not contain any unwanted details and is
ready for compilation if copied from here and pasted in notepad.
Another links about Java FAQ are not working.
I also welcome suggestion from ronald.
But this is not going to solve my problem.
Please help in correcting the code.
-Sameer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top