在控制台中按Enter键不输入 [英] Get key press without pressing enter in console

查看:615
本文介绍了在控制台中按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.

我正在制作pong ..需要上下键来移动蝙蝠的东西。

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天全站免登陆