简体   繁体   English

如何读取 Ctrl+C 作为输入

[英]How to read Ctrl+C as input

(in Linux ) (在Linux中)

The methods I found all use signal .我发现的方法都使用signal

Is there no other way?难道没有别的办法了吗? Is there anything I can do to make the terminal put it into the input buffer?有什么办法可以让终端将其放入输入缓冲区?

In order to "read CTL+C as input" instead of having it generate a SIGINT it is necessary to use tcsetattr () to either clear cc_c[VINTR] or clear the ISIG flag as described in the manual page that I linked to, here.为了“读取 CTL+C 作为输入”而不是让它生成SIGINT ,有必要使用tcsetattr ()来清除cc_c[VINTR]或清除我链接到的手册页中描述的ISIG标志,这里.

You will need to use tcgetattr , first, to read the current terminal settings, adjust them accordingly, then tcsetattr to set them.您需要首先使用tcgetattr来读取当前的终端设置,相应地调整它们,然后使用tcsetattr来设置它们。

You should make sure that the terminal settings get reset to their original defaults when your program terminates, it is not a given that the shell will reset them to default, for you.您应该确保在程序终止时将终端设置重置为其原始默认值,shell 不会为您将它们重置为默认值。

Nope, signal() is not the way.不, signal()不是办法。 You should need to configure the tty driver to do raw input (no input char processing in the driver), so it passes all characters untouched.您应该需要将tty驱动程序配置为进行原始输入(驱动程序中没有输入字符处理),因此它会传递所有未触及的字符。 But this requires considering the tty input device as special and write special code to treat that case (this requires you to issue several ioctl system calls).但这需要将tty输入设备视为特殊设备并编写特殊代码来处理这种情况(这需要您发出多个ioctl系统调用)。 But this is not recommended, for the reasons explained below.但不建议这样做,原因如下所述。

Second, there's another, simpler way that doesn't require to use raw mode.其次,还有另一种更简单的方法,不需要使用原始模式。 You can escape the Ctrl-C character by prepending the tty escape character.您可以通过添加tty转义字符来转义Ctrl-C字符。 In Linux and BSD system, this is normally tied to the Ctrl-V character, so pressing Ctrl-V + Ctrl-C allows you to input a single Ctrl-C char.在 Linux 和 BSD 系统中,这通常与 Ctrl-V 字符相关联,因此按 Ctrl-V + Ctrl-C 可以输入单个 Ctrl-C 字符。 I have just checked it with the hd command:我刚刚用hd命令检查了它:

$ hd
^C
00000000 : 03 0a                                           : ..
00000002
$ _

Next question is, then, how to input a Ctrl-V?那么接下来的问题是,如何输入一个Ctrl-V? well, just double it, (but we are out of scope now, just continue reading)好吧,加倍,(但我们现在没有 scope,继续阅读)

The advantage of this approach is that it doesn't require programming in your program and will work the same way when reading from a file, pipe, fifo, socket, etc. (to which Ctrl-C has no special meaning, as the tty driver is out of scene) The only device that generates an interrupt when detecting Ctrl-C is the tty driver (more exactly, the controlling tty , in it's code generic part, so all tty s do this) and it also has a escape character (by the same reason, all tty s have it) to invalidate the special meaning of the next character.这种方法的优点是它不需要在您的程序中进行编程,并且在从文件中读取时会以相同的方式工作,pipe,fifo,套接字等(Ctrl-C 没有特殊含义,因为tty驱动程序不在现场)在检测到 Ctrl-C 时产生中断的唯一设备是tty驱动程序(更准确地说,控制tty ,在它的代码通用部分,所以所有tty都这样做)并且它还有一个转义字符(出于同样的原因,所有tty都有它)使下一个字符的特殊含义无效。

You can check which character is the escape character with the stty(1) command, configured as the lnext entry.您可以使用配置为lnext条目的stty(1)命令检查哪个字符是转义字符。 But I can almost ensure you that it will be Ctrl-V.但我几乎可以向您保证它将是 Ctrl-V。

Note笔记

This also applies to any other special character (like Ctrl-D, Ctrl-H, etc. that the terminal uses for special purposes)这也适用于终端用于特殊目的的任何其他特殊字符(如 Ctrl-D、Ctrl-H 等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM