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

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

问题描述

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



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

解决方案

自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;

公共类TrialAutoIt3a {

// *****当然你的程序路径会有所不同
private static final String AUTOIT_PATH =C:/用户/皮特/文件/程序/ AutoIt的/实验/;
private static final String AUTOIT_EXEC =TestWithJava.exe;
protected static final CharSequence EXIT =exit;
private static进程proc = null;

public static void main(String [] args){
Runtime rt = Runtime.getRuntime();
System.out.println(键入\exit \退出程序);

尝试{
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:

 本地$ line =

而(真)

$ line = $ line& ConsoleRead()

如果StringInStr($ line,@ CR)或StringInStr($ line,@ LT)那么
ConsoleWrite($ line&to java& @CRLF)
$ line =
EndIf

睡眠(25)

WEnd

$ p

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.

解决方案

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

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

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