在退出的 Java 程序中设置热键,没有 GUI [英] Setting a hotkey in a Java program that exits, without a GUI

查看:31
本文介绍了在退出的 Java 程序中设置热键,没有 GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

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

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.

一个简单的例子:

Java 程序,TrialAutoIt3a.java:

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 程序,TestWithJava.au3:

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

AutoIt 程序在运行该程序之前会被编译成一个 exe 文件

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

这篇关于在退出的 Java 程序中设置热键,没有 GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆