如何导致左键单击然后关闭线程?

How to cause a left click then turn off thread?

    private void runInBackground() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            while (running) {
                try {
                    checkPixel();
                } catch (AWTException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

public void stop() {
    this.running = false;
}

public void start() {
    this.running = true;
}

所以有人给我提供了一些代码来监视屏幕中间颜色的变化。我基本上想在按下 F9 之类的按钮后转 on/off checkPixel() 但是如果没有 GUI 我怎么能这样做因为我似乎找不到任何允许这样做的东西。我相信 KeyListeners 只能与 GUI 一起使用?

编辑:好的,所以当我检查线程中的像素变化时。一旦检测到像素变化,我想在 checkPixel() 中创建一个 "left click action",然后关闭线程。有什么帮助吗?

我猜您需要 JNativeHook 库提供的功能。 该库允许从后台获取密钥。