0

我正在编写一个将连续运行的程序,我想知道是否有与 Autoit SetHotKey(Key, Action()) 等效的 Java。我在这里看到了与 GUI 界面相关的答案,但我的程序没有 GUI。我只想让程序在我按下某个键时退出,最好是 ESC。

我想让程序使用 awt.Robot 的 keyevents 在无限循环中运行,我希望能够通过按下某个键来退出程序。

4

1 回答 1

2

没有核心 Java 解决方案,因为 Java 被构建为尽可能与操作系统无关,为了实现您的目标,您需要一个可以更接近操作系统集成的程序。我知道的主要解决方案是通过 JNA、JNI 或(我最喜欢的)AutoIt 将您的程序集成到操作系统中。通过简单地让我的 Java 程序和 AutoIt 通过标准 IO 和套接字进行通信来完成类似的操作。

一个简单的例子:

Java 程序,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 程序,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

AutoIt 程序将在运行该程序之前编译为 exe 文件

于 2013-11-06T01:49:32.357 回答