有没有办法等待,并从(远程)终端会话按键?

编程入门 行业动态 更新时间:2024-10-28 01:15:00
本文介绍了有没有办法等待,并从(远程)终端会话按键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在玩一些ANSI东西(如颜色等)在java和php(从头开始),我试图找到一种方式基本上等待一个按键。我想在我的主要事件循环结束时有类似下面的伪代码:

If(KeyPressed) Begin var event = new KeyboardEvent(); event.Key = ReadKey(); this.BubbleEvent(event); 结束

但是在过去几天我一直在尝试的一切失败,

只要在STDIN上可以使用。 java

code>, php ,纯旧 c 或 c#会是最好的,我不能使用任何真正的spiffy图书馆的东西,因为我需要移植到所有这四种语言...我需要这个工作在 telnet 或 ssh 连接,但是我的研究表明,除非你在本地机器上工作,否则这是不可能的。

请证明我错了。

解决方案

c $ c> cbreak(3)将禁用线缓冲和擦除/杀死处理。你可以用 stty(1)自己做。

当你的程序死了,终端在 cbreak 模式下,通常可以使用 stty sane 或 reset

在Perl中,您可以使用 Term :: ReadKey 或 Curses 模块来操作终端。有关详情,请参阅 Term :: ReadKey(3pm)或 Curses(3pm)联机帮助页。

在C中,您可以使用 ioctl(2)在终端设备上调用打开cbreak模式,或者您可以使用curses 。有关详细信息,请参阅 ncurses(3)联机帮助页。

I've been playing around with some ANSI stuff (like colors etc.) in java and php (from scratch) and I'm trying to find a way to basically wait for a key press. I'd like to have something like the following pseudo code at the end of my main event loop:

If (KeyPressed) Begin var event = new KeyboardEvent(); event.Key = ReadKey(); this.BubbleEvent(event); End

But everything I've been trying over the last couple days fails because the key presses only become available on STDIN after the user has pressed enter.

It doesn't matter much what language you answer in, but java, php, plain old c or c# would be nicest, and I cannot use any really spiffy library stuff because I need to port it to all four of those languages... I need this to work over a telnet or ssh connection, but my research so far suggests it is impossible unless you're working on the local machine.

Please prove me wrong.

解决方案

The curses function cbreak(3) will disable line-buffering and erase/kill handling. You can do this yourself with stty(1) if you really want.

When your program dies and leaves the terminal in cbreak mode, you can usually use either stty sane or reset to bring the terminal back to a reasonable state.

From within Perl, you can use either the Term::ReadKey or the Curses module to manipulate the terminal. See the Term::ReadKey(3pm) or Curses(3pm) manpage for details.

From within C, you can use either ioctl(2) calls on the terminal device to turn on cbreak mode, or you can use curses. See the ncurses(3) manpage for details.

更多推荐

有没有办法等待,并从(远程)终端会话按键?

本文发布于:2023-10-31 01:26:43,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:终端   没有办法   并从   按键

发布评论

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

>www.elefans.com

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