Linux终端中是否有用于按键输入的提前缓冲? [英] Is there a typeahead buffer for key presses in Linux terminal?

查看:79
本文介绍了Linux终端中是否有用于按键输入的提前缓冲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux缓冲区是否在终端中键入,以便您以后可以一次一个键读取它们?

Does Linux buffer keys typed in terminal so that you can read them later one key at a time?

我之所以问是因为我想捕捉ESC和箭头键,却找不到可靠读取代码的方法.我将终端置于非规范模式,并希望在没有输入的情况下阻止程序,但如果有输入,我只想获取一个按键进行处理.

I am asking because I want to catch ESC and arrow key presses and can't find a way to reliably read the codes. I put terminal in non-canonical mode and want program to block if there is no input, but if there is, I want to fetch only one key press for processing.

更新2 :箭头键只是一个示例.即使我的程序的转义序列未知,我也需要识别按键.

Update 2: Arrow keys is just an example. I need to identify key presses even for the keys with unknown escape sequences for my program.

有两种冲突的情况:

  • read(1)返回一个字符.对于功能键和ESC键,此字符均为0x1b.要检查它是否是箭头键,您需要读取(1),如果仅按一个ESC键,它将阻塞.
    解决方案:阻止读取(1),非阻止读取(1)
    问题:如果第二次读取与任何功能键均不匹配,则可能表示它已被ESC缓冲,后跟某些序列或未知的功能键.如何检测未知的功能键?

  • read(1) returns one character. For both function keys and ESC key this character will be 0x1b. To check if it was an arrow key, you need to read(1), which will block if only single ESC is pressed.
    Solution: blocked read(1), non-blocked read(1)
    Problem: if second read didn't match any function key, it may mean that it was buffered ESC followed by some sequence, or an unknown function key. How to detect unknown function key press?

read(4)最多返回4个字符,但是如果按ESC键四次以使其缓冲,则将得到四个0x1b的字符串.找出是否存在上述未知功能键的相同问题.

read(4) returns at most 4 characters, but if you press ESC four times to let it buffer, you'll get a string of four 0x1b. The same problem to find out if there is an unknown function key press as above.

有人可以解释如何在Linux终端中处理这些问题,或者至少发布一个证明Linux只是没有用于密钥的输入缓冲区的证据吗?

Can anybody explain how to deal with these problems in Linux terminal, or at least post a proof that Linux just doesn't have input buffer for keys?

推荐答案

您应该阅读 VT100转义序列.

您已经发现,转义按钮的字符代码(作为真实字符发送,但通常几乎只用于表示转义序列的开始),是0x1b.

You've discovered that the character code for the escape button (which is sent as a real character, but tends to be used almost exclusively for signalling the beginning of an escape sequence) is 0x1b.

To move the cursor UP:    <ESC>[{COUNT}A
To move the cursor DOWN:  <ESC>[{COUNT}B
To move the cursor RIGHT: <ESC>[{COUNT}C
To move the cursor LEFT:  <ESC>[{COUNT}D

您可以通过在终端中键入它们来对它们进行测试.只需一个接一个地键入按键.我的终端无法识别count参数,但是如果键入<ESC>[X(对于A,B,C,D中的X),则可以成功运行.

You can test these yourself by typing them into the terminal. Just type the keys one after another. My terminal does not recognize the count argument, but will work successfully if I type <ESC>[X (for X in A,B,C,D).

如果您的终端不在VT100模式下,请查找其所在模式的转义序列.您可能会意识到,过多地依赖于终端的特定转义码会将您的程序限制为一种特定的终端类型.

If your terminal isn't in VT100 mode, look up the escape sequences for whichever mode it's in. You might realize that depending too much on terminal specific escape codes restricts your program to one specific terminal type.

这篇关于Linux终端中是否有用于按键输入的提前缓冲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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