Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to Sendkeys to an application in Java using the Robot Class

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Post How to Sendkeys to an application in Java using the Robot Class

    For any of you that have used Visual Basic, you will probably know about SendKeys.
    SendKeys is basically a way of sending keystrokes to an application.

    Java now has this ability thanks to the Robot Class.

    This code will show you how to send keystrokes to an application. In this case, Notepad.exe with a delay between each key.

    The Robot class can do a lot more so its worth looking into!

    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
     
    public class Robot1{
     
        static int keyInput[] = 
        {
          KeyEvent.VK_J, KeyEvent.VK_A, KeyEvent.VK_V, KeyEvent.VK_A, KeyEvent.VK_SPACE,
          KeyEvent.VK_P, KeyEvent.VK_R, KeyEvent.VK_O, KeyEvent.VK_G, KeyEvent.VK_R,
          KeyEvent.VK_A, KeyEvent.VK_M, KeyEvent.VK_M, KeyEvent.VK_I, KeyEvent.VK_N,
          KeyEvent.VK_G, KeyEvent.VK_SPACE, KeyEvent.VK_F, KeyEvent.VK_O, KeyEvent.VK_R,
          KeyEvent.VK_U, KeyEvent.VK_M, KeyEvent.VK_S, KeyEvent.VK_SPACE, KeyEvent.VK_PERIOD,
          KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_M 
        };
     
     
        public static void main(String[] args) throws AWTException,IOException {
     
        Runtime.getRuntime().exec("notepad");
     
        Robot robot = new Robot();
     
        for (int i = 0; i < keyInput.length; i++)
        {
     
          robot.keyPress(keyInput[i]);
          robot.delay(100);
     
        }
    }
    }

    Output:



    Try it for yourself. Its cool...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  2. The Following 3 Users Say Thank You to JavaPF For This Useful Post:

    bocahTuaNakalzz (October 24th, 2009), JFujy (September 22nd, 2009), ptl161 (January 4th, 2011)


  3. #2
    Junior Member
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Sendkeys to an application in Java using the Robot Class

    wow, does the Robot do everything short of key logging and injecting DLL's into executables?
    hahaha

  4. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Sendkeys to an application in Java using the Robot Class

    ei this is one is cool ,

    anyway sir java , what other kind of executable file can i use with this method..?

    in a console output.. how should i code it?

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: How to Sendkeys to an application in Java using the Robot Class

    SendKeys passes the keys to the currently active window and then they are proccessed as if the user typed them. So you can send them to any application, and you can change focuses too.

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  6. #5
    Junior Member
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: How to Sendkeys to an application in Java using the Robot Class

    Wow... Robot Class.....

  7. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: How to Sendkeys to an application in Java using the Robot Class

    wow, its superb dude
    i am searching in Google for a JAVA code to implement this feature only and finally i got it

  8. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to Sendkeys to an application in Java using the Robot Class

    I am using MAC OS. So, how will i do to open in BBEdit or TextEdit as there is no notepad. Also I tried by replacing "notepad" with "BBedit" but displayed error as:
    Exception in thread "main" java.io.IOException: Cannot run program "BBEdit": error=2, No such file or directory

Similar Threads

  1. Replies: 23
    Last Post: July 4th, 2014, 06:53 PM
  2. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 2
    Last Post: May 17th, 2014, 01:16 AM
  3. Replies: 8
    Last Post: April 21st, 2013, 08:20 AM
  4. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Swing Tutorials
    Replies: 6
    Last Post: March 6th, 2012, 11:25 AM
  5. [SOLVED] How to call string in another class in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2009, 09:31 AM

Tags for this Thread