3

I spent two hours for the past three days researching and cannot figure this out.

This is a simple program that clicks the left mouse button every 2.5 Seconds 50 times and then the program ends. But sometimes I want to end the program sooner by pressing the escape key (not mouse).

How do I end the program by clicking the escape button? I don't know what I am doing wrong. If possible please provide code because I am new to Java and don't understand Java terminology well. Thank you for the help.

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class TestClass implements KeyListener, ActionListener
{
   static boolean exit = false;
   public static void main(String[] args) throws AWTException 
   {
      //Creates robot class
      Robot r = new Robot();
      for(int x = 1; x < 50; x++)
      {
         //moves mouse to a cretain point on the screen
         r.mouseMove(789, 326);

         //robot class presses the mouse then realases every 2.5 Seconds
         r.mousePress(InputEvent.BUTTON1_MASK);
         r.mouseRelease(InputEvent.BUTTON1_MASK);
         r.delay(2500);
         System.out.println("This the " + x + "th time."); 
      } 
   }

   //KeyLisener stuff
   @Override
   public void keyPressed(KeyEvent e)
   {
      // TODO Auto-generated method stub
      if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
      {
         System.out.println("The key has been pressed");
         System.exit(0);
      }
   }

   @Override
   public void keyReleased(KeyEvent e){}

   @Override
   public void keyTyped(KeyEvent e){}

   // Action Performed stuff
   @Override
   public void actionPerformed(ActionEvent arg0)
   {
      // TODO Auto-generated method stub
      if (exit = true)
      {
         System.exit(0);
      }
   }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
user3144079
  • 159
  • 3
  • 12
  • Pretty sure that key listener needs to be on a separate thread. – user1231232141214124 Jul 05 '14 at 00:27
  • @redFIVE: it has little to do with Threads. A KeyListener needs to listen to a Swing or AWT GUI component that has focus in a GUI that has focus, something that isn't true for the original poster's question. In other words, a KeyListener simply cannot be used here. – Hovercraft Full Of Eels Jul 05 '14 at 01:05

2 Answers2

2

A KeyListener needs to listen to a Swing or AWT GUI component that has focus in a GUI that has focus, something that isn't true for your situation. In other words, a KeyListener simply cannot be used here.

This is no simple task to do with Java when your program is not a GUI or is a GUI that doesn't have focus. One solution is to use JNI or JNA to get a system keyboard hook, which can be tricky if you're not familiar with system programming for your operating system.

My solutions in a Windows environment have usually involved using a utility program such as AutoIt or AutoHotKey, and then tie the small utility program via sockets with my Java program. All solutions will be platform specific.

For example code that solves a similar problem, please see my answer to a similar stackoverflow question here.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
2

Just for people who are still looking for this - the way I close my programs is very, very simple:

if(e.getKeyCode() == KeyEvent.VK_ESCAPE) { System.exit(1); }

Hope this helps.