3

I'm trying to simulate a keystroke with the code below. When I open notepad it works fine 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 clicks, those action do work. Does anyone know how to fix this problem?

I found this question, How can I use java.awt.Robot inside games? but I can't add a comment or anything.

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

    }

}
Community
  • 1
  • 1
PHPeter
  • 567
  • 6
  • 19

3 Answers3

4

You probably want to press and release the keys to simulate a keystroke, i.e. your current code will hold down Q, W, E, R, T and Y until a release is triggered. Also, you may want to hold them down for a small amount of time, because that caused some problems for me when I did something like this.

Code:

package MyProject;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class KeyStroke {
    private static Robot robot;

    public static void main(String[] args) throws AWTException {
        robot = new Robot();
        robot.delay(3000);
        keystroke(KeyEvent.VK_Q);
        keystroke(KeyEvent.VK_W);
        keystroke(KeyEvent.VK_E);
        keystroke(KeyEvent.VK_R);
        keystroke(KeyEvent.VK_T);
        keystroke(KeyEvent.VK_Y);
    }

    private static void keystroke(int key) {
        robot.keyPress(key);
        robot.delay(100); // hold for a tenth of a second, adjustable
        robot.keyRelease(key);
    }
}
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
  • Ow sorry this code is just a thing I tried also tried the KeyPress/Release but that didn't matter. Your second comment about holding it down a small amount of time fixed my problem. Thank you very much!! – PHPeter Sep 18 '14 at 10:02
  • @PHPeter You may want to accept my answer if it solved your problem, by clicking on the outline of a tick. – bcsb1001 Sep 18 '14 at 10:07
0

Any time people experience problems with the java.awt.Robot methods not registering on a program, most likely it is because the methods are not releasing the key/mouse stroke or do not have a delay between press/release. This goes for mouse clicks and keystrokes alike.

There are two things to check-

  1. If you are using robot.keyPress(key) make sure to have a robot.keyRelease(key) following at some point.

  2. Make sure to have a big enough delay delay between the Press and Release. Rule of thumb 100 ms

Examples

Incorrect

robot.keyPress(key); // without a keyRelease

robot.keyPress(key); 
robot.keyRelease(key); // no delay

Correct

robot.keyPress(key);
Thread.sleep(100) // or robot.delay(100);
robot.keyRelease(key);
Community
  • 1
  • 1
Zach
  • 16
-1

As opposed above - where do you add your key listener???

public class BetaTest {

    public static void main (String[] args){
        new BetaTest().startUp();
    }


    private void startUp() {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

        final KeyAdapter ka = new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                System.out.println("key pressed");
            }

        };

        frame.addKeyListener(ka);

        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    KeyEvent ke = new KeyEvent(frame, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_A, 'a');
                    ka.keyPressed(ke);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread t = new Thread(r);
        t.setDaemon(true);
        t.start();

    }

}

and output is suprise

key pressed
Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • OP doesn't want anything to do with listening or swing - simply `java.awt.Robot`. – bcsb1001 Sep 18 '14 at 11:39
  • well... you might think i'm wrong but first sentence: "I'm trying to simulate a keystroke with the code below." and later "Does anyone know how to fix this problem" ... i don't see how this is wrong concerned the question... even when asking for clarification, no response was given... – Martin Frank Sep 18 '14 at 11:52
  • I don't know about you, but to me it seems quite obvious that **simulating** a keystroke is not the same as **listening for** a keystroke. Also, the code specifically showed an attempt to use `java.awt.Robot` - completely unrelated to your 'solution'. Besides, I am clearly right because I have the accepted answer. So yes, I do think you're wrong. – bcsb1001 Sep 18 '14 at 11:57
  • well reading http://cr.openjdk.java.net/~alanb/6854954/webrev.00/src/share/classes/java/awt/Robot.java-.html would would surely **KNOW** that java.awt.robot uses KeyEvents for **simulating** keyevents... so, i let's say **you** are right ..... – Martin Frank Sep 18 '14 at 12:08