在 Java 中检测和操作键盘方向键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/572001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:54:19  来源:igfitidea点击:

Detecting and acting on keyboard direction keys in Java

javakeyboardkeydirection

提问by elwynn

G'day all,

大家好,

I have a console project where it is intended the user presses the keyboard direction keys (non-numeric keypad) to move an avatar. I am having difficulty coding to check for the press of these keys. In Pascal it was easy enough to use "readkey" and code, for example, for #80 for the down keypress. However, I am stumped how to implement the same functionality in Java, though I think I understand the use of System.in and BufferedInputStream.

我有一个控制台项目,旨在让用户按下键盘方向键(非数字键盘)来移动头像。我很难通过编码来检查是否按下了这些键。在 Pascal 中,使用“readkey”和代码很容易,例如,#80 用于向下按键。但是,我对如何在 Java 中实现相同的功能感到困惑,尽管我认为我了解 System.in 和 BufferedInputStream 的使用。

Could anyone help me out? Your thoughts or hints are much appreciated.

有人可以帮我吗?非常感谢您的想法或提示。

采纳答案by Uri

The Console support issue in Java is well known, I am not sure that this is doable.

Java 中的控制台支持问题是众所周知的,我不确定这是否可行。

This was not initially possible with System.in since it used to work line-based.

这在 System.in 最初是不可能的,因为它曾经基于行工作。

Sun eventually added a java.io.Console class.

Sun 最终添加了一个 java.io.Console 类。

Here are its JavaDocs: http://java.sun.com/javase/6/docs/api/java/io/Console.html

这是它的 JavaDocs:http: //java.sun.com/javase/6/docs/api/java/io/Console.html

Once you get the console (I think from System.console), you can get a reader and perhaps read characters from it, but I'm not sure if it includes keys.

一旦您获得控制台(我认为来自 System.console),您就可以获得一个阅读器并从中读取字符,但我不确定它是否包含键。

Generally, you're supposed to use Swing or AWT if you want access to the keyboard, which is silly.

通常,如果您想访问键盘,您应该使用 Swing 或 AWT,这很愚蠢。

As of 2007, there was a feature request about it: here

截至 2007 年,有一个关于它的功能请求:here

回答by Jason S

If java.io.console doesn't work for you (I haven't tried that), try JLine. I used it to solve a vaguely similar problem.

如果 java.io.console 对您不起作用(我还没有尝试过),请尝试JLine。我用它来解决一个模糊相似的问题

回答by vladr

Unfortunately this is not possible in a portable way:

不幸的是,这是不可能的:

http://forums.sun.com/thread.jspa?threadID=5351637&messageID=10526512

http://forums.sun.com/thread.jspa?threadID=5351637&messageID=10526512

On Windows, reading from System.in will block until enteris pressed, even when you do not use a BufferedReader. Arrows will cycle through the command history. Try it yourself:

在 Windows 上,从 System.in 读取将阻塞直到enter被按下,即使您不使用BufferedReader. 箭头将在命令历史记录中循环。自己试试:

import java.io.*;
public class KeyTest {
  public static void main(String[] argv) {
    try {
      InputStreamReader unbuffered = new InputStreamReader(System.in);
      for (int i = 0; i < 10; ++i) {
        int x = unbuffered.read();
        System.out.println(String.format("%08x", x));
      }
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}

Same issue using the Consoleclass (input buffered under Windows, arrow keys intepreted by Windows):

使用Console该类的相同问题(在 Windows 下缓冲的输入,Windows 解释的箭头键):

import java.io.*;
public class KeyTest2 {
  public static void main(String[] argv) {
    try {
      Console cons = System.console();
      if (cons != null) {
        Reader unbuffered = cons.reader();
        for (int i = 0; i < 10; ++i ) {
          int x = unbuffered.read();
          System.out.println(String.format("%08x", x));
        }
      }
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}

回答by Barett

Not with built-in Java code. Check out java curses libraries or JLine as mentioned above, if you want to continue.

不适用于内置 Java 代码。如果您想继续,请查看上面提到的 java curses libraries 或 JLine。