在控制台中捕获KeyPress [英] Capture KeyPress in Console

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

问题描述

好吧,我想让简单的控制台游戏开始,我想渲染一个控制台Tic-Tac-Toe板。我遇到的问题是,我想要能够捕获箭头键来移动选择板内或捕获空格键。这将是一个不断运行的循环。



无论我有多么看,我不知道我该怎么做,而不实现一些GUI应用程序附加到。我希望它是可能的。有人可以给我一些指针吗?这里是我试图捕获键盘输入只是为了看到什么价值Java认为箭头键是:

  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/ p>

复制自: -

更多参考: - 在Java中,捕获< tab>键击?


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.

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;
}
}

解决方案

I think you can't without native code and JNI. Take a look at Java Curses library: http://sourceforge.net/projects/javacurses/

copied from :- Get key press without pressing enter in console

More reference :- In Java, what is the best way to capture a <tab> keystroke?

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆