在控制台中捕获按键

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)