Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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

Threaded View

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 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.

  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)


Similar Threads

  1. Replies: 24
    Last Post: August 4th, 2014, 12:49 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, 12:25 PM
  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