在控制台中捕获按键

时间:2023-03-17
本文介绍了在控制台中捕获按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在尝试制作简单的控制台游戏以开始使用,并且我想要渲染控制台井字游戏板.我遇到的问题是我希望能够捕获箭头键以在板内移动选择或捕获空格键.这将是一个不断运行的循环.

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>的最佳方法是什么?击键?

这篇关于在控制台中捕获按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何在 Java 控制台(无 UI)中显示下载文件的百分比 下一篇:Eclipse 控制台:检测警告和错误模式并使其可点击

相关文章

最新文章