Question

I'm writing a program that will run continuously and I was wondering if there was a Java equivalent to the Autoit SetHotKey(Key, Action()). I saw an answer on here that related to a GUI interface, but my program does not have a GUI. I just want the program to exit whenever I press a certain key, preferably ESC.

I'd have the program running in an infinite loop using the awt.Robot's keyevents, I'd like to be able to quit the program by pressing a certain key.

Était-ce utile?

La solution

There are no core Java solutions since Java was built to be as operating system agnostic as possible, and to achieve your goal, you need a program that can integrate closer to the OS. The main solutions that I know of are to integrate your program to the OS via JNA, JNI, or (my favorite), AutoIt. Of done something similar by simply having my Java program and AutoIt communicate through standard IO and sockets.

A simple example:

Java program, TrialAutoIt3a.java:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class TrialAutoIt3a {

   // ***** of course your path to program will be different
   private static final String AUTOIT_PATH = "C:/Users/Pete/Documents/Programming/AutoIt/Experiment/";
   private static final String AUTOIT_EXEC = "TestWithJava.exe";
   protected static final CharSequence EXIT = "exit";
   private static Process proc = null;

   public static void main(String[] args) {
      Runtime rt = Runtime.getRuntime();
      System.out.println("Type \"exit\" to exit program");

      try {
         proc = rt.exec(AUTOIT_PATH + AUTOIT_EXEC);
      } catch (IOException e1) {
         e1.printStackTrace();
         System.exit(-1);
      }
      InputStream iStream = proc.getInputStream();
      InputStreamReader isr = new InputStreamReader(iStream);
      final BufferedReader bufReader = new BufferedReader(isr);

      OutputStream oStream = proc.getOutputStream();
      final PrintWriter pw = new PrintWriter(oStream, true);

      Runnable bufReaderRunnable = new Runnable() {
         public void run() {
            String output;
            try {
               while ((output = bufReader.readLine()) != null) {
                  System.out.println(output);
                  if (output.toLowerCase().contains(EXIT)) {
                     proc.destroy();
                     System.exit(0);
                  }
               }
            } catch (IOException e) {
               e.printStackTrace();
            } finally {
               if (bufReader != null) {
                  try {
                     bufReader.close();
                  } catch (IOException e) {
                     e.printStackTrace();
                  }
               }
            }
         }
      };
      new Thread(bufReaderRunnable).start();

      Runnable myRun = new Runnable() {
         public void run() {
            Scanner scan = new Scanner(System.in);
            while (scan.hasNextLine()) {
               String line = scan.nextLine();
               pw.println(line);
            }
            scan.close();
         }
      };
      new Thread(myRun).start();

   }
}

AutoIt program, TestWithJava.au3:

Local $line = ""

While (True)

    $line = $line & ConsoleRead()

    If StringInStr($line, @CR) Or StringInStr($line, @LF) Then
        ConsoleWrite($line & "to java" & @CRLF)
        $line = ""
    EndIf

    Sleep(25)

WEnd

The AutoIt program will be compiled to an exe file prior to running this program

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top