Capture KeyPress in Console(在控制台中捕获按键)
问题描述
好的,我正在尝试制作简单的控制台游戏以开始使用,并且我想要渲染控制台井字游戏板.我遇到的问题是我希望能够捕获箭头键以在板内移动选择或捕获空格键.这将是一个不断运行的循环.
Alright, I'm trying to make simple console games to get started and I'm wanting to render a console Tic-Tac-Toe board. The problem I'm having is that I'd like to be able to capture either the arrow keys to move the selection inside the board or capture the space key. This will be a constantly running loop.
无论我环顾四周,如果不实现一些要附加的 GUI 应用程序,我不知道该怎么做.我希望它是可能的.有人可以给我一些指示吗?这是我试图捕捉键盘输入的内容,只是为了看看 Java 认为箭头键是什么值:
No matter how much I have looked around, I can't figure out how I'd go about doing this without implementing some GUI application to attach to. I'd like to hope it's possible at all. Can someone give me some pointers? Here is what I was trying to just capture keyboard input just to see what value Java thinks the arrow key is:
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
public class KeyListener extends KeyAdapter
{
public static void main(String[] args)
{
String choice = keyboard.nextLine();
System.out.println("Running listener...");
}
public static char getKeyPressed(KeyEvent e)
{
char key = e.getKeyChar();
System.out.println("You pressed: "+key);
return key;
}
}
推荐答案
我认为你不能没有本机代码和 JNI.看看 Java Curses 库:http://sourceforge.net/projects/javacurses/
复制自:- 获取按键而不在控制台中按回车
更多参考:- 在Java,捕获<tab>的最佳方法是什么?击键?
这篇关于在控制台中捕获按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在控制台中捕获按键


基础教程推荐
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01