C语言 如何检查C中的“退格”字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4363309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:18:41  来源:igfitidea点击:

how to check for the "backspace" character in C

ccharacterbackspace

提问by joel

I'd like to know how to check if a user types the "backspace" character.

我想知道如何检查用户是否键入了“退格”字符。

I'm using the getch() function i.e. "key = getch()"in my C program and i'd like to check when backspace is pressed. the line:

getch() function i.e. "key = getch()"在我的 C 程序中使用 ,我想检查何时按下退格键。该行:

 if(key = '\b') { ....

doesn't work.

不起作用。

回答by SiegeX

The problem with reading Backspaceis that most terminals are 'cooked' in that keys like backspace are handled by the terminal driver. However, the curses function getch()can read the backspace as it's not tied to the terminal.

阅读的问题Backspace在于大多数终端都是“熟的”,因为像退格键这样的键是由终端驱动程序处理的。但是,curses 函数getch()可以读取退格,因为它不绑定到终端。

Edit

编辑

I just noticed your code isusing getch()for input. I ran a little test program and getch()returns 127 when you hit backspace. Therefore try:

我只注意到你的代码使用getch()的输入。我运行了一个小测试程序,getch()当你按下退格键时返回 127。因此尝试:

if (key == 127 || key == 8) { ... /* Checks for both Delete or Backspace */

Also note that your sample code uses the assignmentoperator =when it should be using the equalityoperator ==

另请注意,您的示例代码在应该使用相等运算符时使用了赋值运算符===

回答by user531771

The type of i/o stream may helps. Standard input stream is a kind of line buffered stream, which do not flush until you write a '\n' char into it. Full buffered stream never flush until the buffer is full. If you write a backspace in full buff stream, the '\b' may be captured.

i/o 流的类型可能会有所帮助。标准输入流是一种行缓冲流,在您向其中写入 '\n' 字符之前不会刷新。完全缓冲的流永远不会刷新,直到缓冲区已满。如果您在完整的 buff 流中写入退格,则可能会捕获 '\b'。

Reference the unix environment advantage program.

参考unix环境优势程序。

回答by Adrian Lopez

You didn't say which library the getch()function comes from (it isn't part of the C standard), but if it's the one from ncurses you can check the value of keyagainst KEY_BACKSPACE.

您没有说明该getch()函数来自哪个库(它不是 C 标准的一部分),但是如果它是来自 ncurses 的库,您可以检查keyagainst的值KEY_BACKSPACE

回答by Douglas G. Allen

Try this:

尝试这个:

#include <stdio.h>      /* printf   */
#include <ctype.h>      /* isalpha isdigit isspace etc      */

#define FALSE 0
#define TRUE  1

/* function declarations */
int char_type(char);

main()
{
 char ch;

 ch = 127;
 char_type(ch);

 ch = '\b';
 char_type(ch);

 return 0;
}

int char_type(char ch)
{
 if ( iscntrl(ch) != FALSE)
   printf("%c is a control character\n", ch); 
}

This is a complete program but it only tests for control characters. You could use principles of it, your choice. Just learning too!

这是一个完整的程序,但它只测试控制字符。你可以使用它的原则,你的选择。也只能学习了!

See : http://www.tutorialspoint.com/c_standard_library/ctype_h.htmor lookup the functions for the ctype.h header file of the C Standard Library.

请参阅:http: //www.tutorialspoint.com/c_standard_library/ctype_h.htm或查找 C 标准库的 ctype.h 头文件的函数。

It's good that you're getting input. Thanks all for the info. I was just looking up backspace code and found this question.

很高兴你能得到输入。谢谢你的信息。我只是在查找退格代码并发现了这个问题。

BTW try '\0' before any char. Not sure what that does but it stops all code after it. Is that like the return 0; line?

顺便说一句,在任何字符之前尝试 '\0' 。不知道它做了什么,但它会停止它之后的所有代码。是不是像返回0一样?线?

回答by Madhur Ahuja

I believe the system input driver is line buffered. So its not possible in standard C.

我相信系统输入驱动程序是行缓冲的。所以它在标准 C 中是不可能的。