无需在控制台中按 Enter 即可获得按键 [英] Get key press without pressing enter in console

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

问题描述

我有一个用 Java 编写的控制台程序,它应该响应单键按下,但用户没有按 Enter.

I have a console program written in Java that should respond to single key presses, but the user does not press enter.

我正在打乒乓球.所以需要上下键来移动球棒.

I'm making pong.. so need the up and down keys to move the bat thing.

欢迎使用替代方法!(除了制作一个 GUI)

Alternative approaches welcome! (apart from making a GUI instead)

--

我只打算在 UNIX 系统(OSX 和 Linux)上运行我的程序,所以我想我可以使用以下命令将终端置于原始"模式:stty raw

I'm only going to run my program on UNIX systems (OSX and Linux), so I think I can put the terminal into "raw" mode with this: stty raw

当我在运行程序之前将其输入控制台时,它可以工作!但是我需要Java自动完成,所以我尝试了这个:

When I type that into the console before running the program it works! But I need Java to do it automatically, so I tried this:

Runtime.getRuntime().exec("stty raw");

它什么都不做......可能是因为 JVM 只是将它作为一个单独的进程运行,而不是作为这个终端中的一个进程.

and it does nothing... probably because the JVM is just running it as a separate process and not as a process within this terminal.

推荐答案

我认为你不能没有本机代码和 JNI.看一下 Java Curses 库:http://sourceforge.net/projects/javacurses/

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

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

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