Typing text automatically using Robot class

Discussion in 'Java' started by Sameer, Aug 1, 2005.

  1. Sameer

    Sameer Guest

    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
     
    Sameer, Aug 1, 2005
    #1
    1. Advertising

  2. 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/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Aug 1, 2005
    #2
    1. Advertising

  3. Sameer

    Sameer Guest

    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
     
    Sameer, Aug 2, 2005
    #3
  4. 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'
     
    Andrew Thompson, Aug 2, 2005
    #4
  5. Sameer

    Roland Guest

    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-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_/ /__/
    / \ /_/ / \
     
    Roland, Aug 2, 2005
    #5
  6. Sameer

    Sameer Guest

    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
     
    Sameer, Aug 2, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prak
    Replies:
    4
    Views:
    741
    Andrew Thompson
    Jun 3, 2004
  2. larry
    Replies:
    1
    Views:
    441
    Andrew Thompson
    Jul 20, 2004
  3. Rafal Majda
    Replies:
    2
    Views:
    971
    Thomas Weidenfeller
    Apr 12, 2005
  4. Rafal Majda
    Replies:
    5
    Views:
    2,467
    Rafal Majda
    Apr 18, 2005
  5. JimLad
    Replies:
    0
    Views:
    526
    JimLad
    Jan 26, 2010
Loading...

Share This Page