Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Typing text automatically using Robot class

Reply
Thread Tools

Typing text automatically using Robot class

 
 
Sameer
Guest
Posts: n/a
 
      08-01-2005
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

 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      08-01-2005
Sameer wrote:
> 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
shift event for both the letters < and >, but on a German keyboard you
would just need one for the later letter

> 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


--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
 
 
 
Sameer
Guest
Posts: n/a
 
      08-02-2005
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-b0
Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing)

What may be the problem?

-Sameer

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      08-02-2005
On 2 Aug 2005 03:37:28 -0700, Sameer wrote:

> 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>

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Bender's Humor By 'Microsoft Joke'
 
Reply With Quote
 
Roland
Guest
Posts: n/a
 
      08-02-2005
On 2-8-2005 12:37, Sameer wrote:

> 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-b0
> 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_/ /__/
/ \ /_/ / \
 
Reply With Quote
 
Sameer
Guest
Posts: n/a
 
      08-02-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Typing to Nullable (Of Date) vs typing to Date JimLad ASP .Net 0 01-26-2010 07:54 PM
typing location changes when I am typing. Ed Computer Support 5 11-11-2006 12:51 AM
Static Typing Where Possible and Dynamic Typing When Needed vladare Ruby 0 07-11-2005 11:54 AM
Visual Age for Java - java.awt.robot class absence? Rafal Majda Java 2 04-12-2005 10:03 AM
Drive an Applet without using robot class ? prak Java 4 06-03-2004 03:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57