如何通过按键来停止按键循环。

huangapple go评论84阅读模式
英文:

How to stop keypressing While loop with keypress

问题

以下是您要翻译的内容:

所以我这里有一个小问题。我正在制作一个程序,按下预定义的按钮后,该程序将开始连续按下“W”键(暂时只按下“W”键),直到我再次按下该按钮为止。我正在使用JNativeHook库来检测按键,但是当我的程序开始在各个地方连续按下W键时,这个库就停止检测按下的按钮。我该如何解决这个问题?以下是我的程序的一些片段:

public class Buttons implements NativeKeyListener {

    private ButtonsAction buttonsAction = new ButtonsAction();

    public void nativeKeyPressed(NativeKeyEvent e) {
        System.out.println("按下的按键:" + NativeKeyEvent.getKeyText(e.getKeyCode()));

        if (e.getKeyCode() == NativeKeyEvent.VC_K) {
            buttonsAction.startBot();
        }
    }

    public void nativeKeyReleased(NativeKeyEvent e) {

    }

    public void nativeKeyTyped(NativeKeyEvent e) {

    }

    public void start() {
        Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
        logger.setLevel(Level.OFF);
        try {
            GlobalScreen.registerNativeHook();
        }
        catch (NativeHookException ex) {
            System.err.println("注册本机钩子时出问题了。");

            System.exit(1);
        }

        GlobalScreen.addNativeKeyListener(new Buttons());
    }
}
public class ButtonsAction {

    private int state = 0;

    public void startBot() {
        try {
            Robot robot = new Robot();
            while (state == 0) {
                robot.keyPress(KeyEvent.VK_W);
                Thread.sleep(10);
            }
        } catch (AWTException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
英文:

so I'm here with a small problem. I'm making a program, which after pressing a predefined button is going to spam pressing "W" (for now) until I will press that button again. I'm using JNativeHook library to check the keypress, but when my program starts spamming W's everywhere, this library stops checking pressed buttons. How can I solve this problem? There are some fragments of my program:

public class Buttons implements NativeKeyListener {

    private ButtonsAction buttonsAction = new ButtonsAction();

    public void nativeKeyPressed(NativeKeyEvent e) {
        System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));

        if (e.getKeyCode() == NativeKeyEvent.VC_K) {
            buttonsAction.startBot();
        }
    }

    public void nativeKeyReleased(NativeKeyEvent e) {

    }

    public void nativeKeyTyped(NativeKeyEvent e) {

    }

    public void start() {
        Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
        logger.setLevel(Level.OFF);
        try {
            GlobalScreen.registerNativeHook();
        }
        catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");

            System.exit(1);
        }

        GlobalScreen.addNativeKeyListener(new Buttons());
    }
}
public class ButtonsAction {

    private int state = 0;

    public void startBot() {
        try {
            Robot robot = new Robot();
            while (state == 0) {
                robot.keyPress(KeyEvent.VK_W);
                Thread.sleep(10);
            }
        } catch (AWTException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

答案1

得分: 1

问题是您正在使用相同的线程来执行机器人。您需要使用另一个线程。

英文:

The problem is you're using the same thread to execute the bot. You'll need to use another thread.

huangapple
  • 本文由 发表于 2020年8月23日 06:42:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63541800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定