gpt4 book ai didi

c - 读取c中的删除按键

转载 作者:行者123 更新时间:2023-11-30 16:39:14 25 4
gpt4 key购买 nike

我遇到了一些我不太清楚的事情。我无法从 MacBook Air 读取删除输入。我正在用 c 编写一个程序来读取输入,并使用了以下转义序列字符:

c='127'

我可以将变量 c 设置为十进制值 8,根据我的理解,这是退格转义序列。另外,在“终端”>“首选项”>“高级”中,我选中了启用删除以发送 Control-H 的复选框。

完成上述操作后,我可以读取输入设置 c 变量,如下所示:

c = '8'

<小时/>

这是我尝试从 MacBook 读取删除键的代码空气:

#include <stdio.h>

main() {
/* Copy program input to program output,
replacing each tab by \t,
each backspace by \b,
and each backslas by \\.
This makes tabs and backspaces visible in an unambiguous way.
*/

int c; // a variable for a character

while((c = getchar()) != EOF) {
if (c == '\t')
printf("\\t");
// check box found in:
// Terminal>Preferences>Advanced>Delete sends Control-H
// default setting DOES NOT send Control-H
// The Delete key does not appear to work with the ascii decimal character value 127.
if (c == '\127')
printf("Print to the screen if Delete key is detected.");
if (c == '\\')
printf("\\\\");
if (c != '\t')
if (c != '\b')
if (c != '\\')
putchar(c);
}
}

最佳答案

尝试以下代码,看看会发生什么:

#include <stdio.h>
#include <stdlib.h>

int
main() {
int c;
system("stty raw -echo");
while (1) {
c = getchar();
if (c == 3) break; // exits when Ctrl-C is pressed
printf("pressed code: %d\r\n", c);
}
system("stty -raw echo");
return 0;
}

希望这有帮助。

关于c - 读取c中的删除按键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com