0
    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;
}

So I have some code someone provided me to monitor the change in color in the middle of the screen. I want to essentially turn on/off checkPixel() after I press something like F9 but how can I do this without a GUI because I can't seem to find anything that allows this. I believe KeyListeners only work with GUIs?

EDIT: Ok so instead while I'm checking for pixel changes in the thread. Once a pixel change has been detected I want to cause create a "left click action" in checkPixel() then turn off the thread. Any help with this?

Huang Chen
  • 1,177
  • 9
  • 24
  • possible duplicate of [Get key press without pressing enter in console](http://stackoverflow.com/questions/4531560/get-key-press-without-pressing-enter-in-console) –  Jul 29 '15 at 15:26
  • How do I add a KeyListener without a GUI? unless I'm wrong – Huang Chen Jul 29 '15 at 15:26
  • http://stackoverflow.com/questions/901224/listening-for-input-without-focus-in-java ...unfortunately... or you could use a full screen transparent window and then implement a KeyListener. I would probably opt for the full screen transparent window as it will save you a lot of time – GregH Jul 29 '15 at 15:27
  • @peggy I have made an edit, could you possibly help with this? maybe this is a more smarter solution. We don't have to start the thread again. – Huang Chen Jul 29 '15 at 15:29
  • I'd still use a transparent window/KeyListener but I'm sure there are other ways to accomplish your goal also – GregH Jul 29 '15 at 15:31
  • @peggy my new request doesn't require listeners – Huang Chen Jul 29 '15 at 15:32
  • are you not referring to using a MouseListener by "create a left click action"? – GregH Jul 29 '15 at 15:33
  • @peggy I want to make the program click the mouse, so I'm not listening for a mouse click – Huang Chen Jul 29 '15 at 15:35
  • can you be more specific on what you are wanting to do here? Do you care where the click takes place? what is this click supposed to accomplish? the Robot class can create mouse clicks but I'm not sure if this is what you actually want. How do you plan to use a left click to turn off a thread? See this post on creating mouse clicks via Robot: http://stackoverflow.com/questions/19185162/how-to-simulate-a-real-mouse-click-using-java – GregH Jul 29 '15 at 15:38

1 Answers1

1

I guess you want functionality provided by JNativeHook library. The library allows for grabbing a key from the backgrond.

krzydyn
  • 1,012
  • 9
  • 19