Setting a hotkey in a Java program that exits, without a GUI

前端 未结 1 938
旧巷少年郎
旧巷少年郎 2021-01-28 03:01

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 relate

相关标签:
1条回答
  • 2021-01-28 03:41

    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

    0 讨论(0)
提交回复
热议问题