我怎么知道用户在带有ncurses的控制台(Linux)中按了ESC键? [英] How can I know that the user hit the ESC key in a console with ncurses (Linux)?
问题描述
在检测我是否只是得到一个普通的 ESC 密钥(仅代码27)还是它是另一个 special 密钥(例如,发送的Arrow Up)时出现问题我三个字节: ESC [ A (27 91 65).
I have a problem in detecting whether I just got a plain ESC key (just code 27) or whether it was another special key such as an Arrow Up which sends me three bytes: ESC [ A (27 91 65).
现在,我了解了转义序列,我不了解的是,我怎么可能知道用户实际上键入了 ESC 而不是特殊键,因为它们都以27和 ESC开头只有27岁?
Now, I understand escape sequences, what I don't understand is how can I possibly know that the user actually typed ESC instead of a special key since both start with 27 and ESC is only 27?
请注意,我使用ncurses的wgetch()
函数,如下所示:
Note that I use the wgetch()
function from ncurses as in:
// initialization not shown initscr() should be enough for this test
while(!f_should_exit)
{
int c(wgetch(f_win_input));
// show codes of what the user types
//
printf("got [%d] ", c);
// prints 27 when I hit ESC
// prints 27 91 65 when I hit Arrow Up
}
我在vim中一直使用 ESC 和箭头键,因此我可以想象有一种简单的方法可以专门检测出按下了哪个键?!
I use the ESC and arrow keys all the time in vim so I would imagine that there is an easy way to specifically detect which key was pressed?!
推荐答案
这是X/Open Curses的标准功能. wgetch
的手册页在
This is a standard feature of X/Open Curses. The manual page for wgetch
discusses it in keypad
mode:
当接收到可能是功能键开头的字符(在现代终端上,表示转义字符),诅咒 设置一个计时器.如果序列的其余部分不在其中 在指定的时间,字符通过;否则, 返回功能键值.因此,许多终端在用户按下转义键与转义返回到程序之间会遇到延迟.
When a character that could be the beginning of a function key is received (which, on modern terminals, means an escape character), curses sets a timer. If the remainder of the sequence does not come in within the designated time, the character is passed through; otherwise, the function key value is returned. For this reason, many terminals experience a delay between the time a user presses the escape key and the escape is returned to the program.
默认情况下,给定窗口的keypad
未设置为true,即库不执行此操作(如果需要功能键,则您的程序必须这样做):
By default, keypad
is not set to true for a given window, i.e., the library does not do this (your program must, if you want function-keys):
keypad(win, TRUE);
在ncurses的输入选项手册页中描述了超时.要将转义符与功能(或光标或键盘键)区分开,可以使用notimeout
,如
The timeouts are described in ncurses' input-options manual page. To distinguish an escape character from a function (or cursor, or keypad key), you could use notimeout
, as mentioned in the discussion of nodelay
:
在解释输入转义序列时,wgetch(3x)设置一个计时器 在等待下一个字符时.如果
notimeout(win, TRUE)
是 调用,则wgetch不会设置计时器. 超时的目的 是要区分从功能键接收到的序列,并且 用户键入的内容.
While interpreting an input escape sequence, wgetch(3x) sets a timer while waiting for the next character. If
notimeout(win, TRUE)
is called, then wgetch does not set a timer. The purpose of the timeout is to differentiate between sequences received from a function key and those typed by a user.
这篇关于我怎么知道用户在带有ncurses的控制台(Linux)中按了ESC键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!