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>的最佳方法是什么?击键?
这篇关于在控制台中捕获按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在控制台中捕获按键
基础教程推荐
- 将 Windows 证书导入 Java 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- JPA惰性列表上的流 2022-01-01
