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

Thread: Simulate keystroke

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Simulate keystroke

    So I'm trying to simulate a keystroke I used the following code (see below). When I open notepad it works but when I open the game in which I want to use it, it doesn't do anything. So keystrokes donít seem to work. I tried to simulate mouse movement and click those action do work. Does anyone knows how to fix this problem??

    package MyProject;
     
    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.KeyEvent;
     
    public class KeyStroke {
     
    	public static void main(String[] args) throws AWTException {
     
    		Robot robot = new Robot();
     
    	    robot.delay(3000);
     
    	    robot.keyPress(KeyEvent.VK_Q);
    	    robot.keyPress(KeyEvent.VK_W);
    	    robot.keyPress(KeyEvent.VK_E);
    	    robot.keyPress(KeyEvent.VK_R);
    	    robot.keyPress(KeyEvent.VK_T);
    	    robot.keyPress(KeyEvent.VK_Y);
     
    	}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Simulate keystroke

    How would you know if the key presses were occurring? Are you expecting to see the keys move on your keyboard?

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simulate keystroke

    I expected to see it on my screen of course, for example it types "qwerty" in notepad. But I just managed to fix my problem. I already tried KeyPress/Release before but now I keep it pressed for a small amount of time. That did the trick.
    package MyProject;
     
    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.KeyEvent;
     
    public class KeyStroke {
     
    	public static void main(String[] args) throws AWTException {
     
                Robot robot = new Robot();
     
    	    robot.delay(3000);
     
    	    robot.keyPress(KeyEvent.VK_1);
    	    robot.delay(100);
    	    robot.keyRelease(KeyEvent.VK_1);
    	    robot.delay(100);
    	    robot.keyPress(KeyEvent.VK_2);
    	    robot.delay(100);
    	    robot.keyRelease(KeyEvent.VK_2);
     
    	}
     
    }

Similar Threads

  1. Java & Keystroke Dynamics
    By shrushtha212 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 3rd, 2013, 02:15 AM
  2. simulate BufferedReader's readLine()
    By Rexshine in forum What's Wrong With My Code?
    Replies: 24
    Last Post: February 25th, 2013, 08:56 PM
  3. simulate calculation ?
    By meryqat in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2012, 01:03 PM
  4. Simulate Latency
    By pixelDepth in forum Java Networking
    Replies: 6
    Last Post: November 30th, 2011, 07:26 AM
  5. Simple program that detects my keystroke
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: December 27th, 2009, 10:44 PM