简体   繁体   English

java.awt.Robot里面的游戏?

[英]java.awt.Robot inside games?

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? 我发现了这个问题, 如何在游戏中使用java.awt.Robot? 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);

    }

}

You probably want to press and release the keys to simulate a keystroke, ie your current code will hold down Q, W, E, R, T and Y until a release is triggered. 您可能希望按下并释放键以模拟击键,即您的当前代码将按住Q,W,E,R,T和Y直到触发释放。 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);
    }
}

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. 任何时候人们遇到没有在程序上注册的java.awt.Robot方法的问题,很可能是因为这些方法没有释放键/鼠标笔划或者在按下/释放之间没有延迟。 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. 如果你正在使用robot.keyPress(key)确保在某些时候有一个robot.keyRelease(key)

  2. Make sure to have a big enough delay delay between the Press and Release . 确保PressRelease之间有足够大的延迟延迟。 Rule of thumb 100 ms 经验法则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);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM