无需在控制台中按 Enter 即可获得按键

编程入门 行业动态 更新时间:2024-10-23 20:30:42
本文介绍了无需在控制台中按 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 库:sourceforge/projects/javacurses/

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

更多推荐

无需在控制台中按 Enter 即可获得按键

本文发布于:2023-11-30 03:26:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   按键   Enter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!