如何检查C中的“backspace”字符

时间:2021-07-25 20:13:11

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

我想知道如何检查用户是否输入了“backspace”字符。

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()函数。“key = getch()”在我的C程序中,我想检查何时按退格键。线:

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

doesn't work.

是行不通的。

5 个解决方案

#1


27  

The problem with reading Backspace is 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的问题是,大多数终端都是“煮熟”的,因为像Backspace这样的键是由终端驱动来处理的。但是,curses函数getch()可以读取backspace,因为它没有绑定到终端。

Edit

I just noticed your code is using 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 assignment operator = when it should be using the equality operator ==

还要注意,您的示例代码在应该使用等号操作符=时使用赋值操作符=

#2


1  

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'字符之前不会刷新。完全缓冲流直到缓冲区满时才刷新。如果您在全浅黄色流中编写一个后置空间,“\b”可能会被捕获。

Reference the unix environment advantage program.

参考unix环境优势程序。

#3


1  

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 key against KEY_BACKSPACE.

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

#4


0  

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.htm or lookup the functions for the ctype.h header file of the C Standard Library.

参见:http://www.tutorialspoint.com/c_standard_library/ctype_h.htm或查找ctype的函数。C标准库的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.

很好,你得到了输入。谢谢你的信息。我只是查了一下backspace代码,发现了这个问题。

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;行吗?

#5


-3  

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

我认为系统输入驱动是行缓冲的。所以在标准C中是不可能的。

#1


27  

The problem with reading Backspace is 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的问题是,大多数终端都是“煮熟”的,因为像Backspace这样的键是由终端驱动来处理的。但是,curses函数getch()可以读取backspace,因为它没有绑定到终端。

Edit

I just noticed your code is using 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 assignment operator = when it should be using the equality operator ==

还要注意,您的示例代码在应该使用等号操作符=时使用赋值操作符=

#2


1  

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'字符之前不会刷新。完全缓冲流直到缓冲区满时才刷新。如果您在全浅黄色流中编写一个后置空间,“\b”可能会被捕获。

Reference the unix environment advantage program.

参考unix环境优势程序。

#3


1  

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 key against KEY_BACKSPACE.

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

#4


0  

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.htm or lookup the functions for the ctype.h header file of the C Standard Library.

参见:http://www.tutorialspoint.com/c_standard_library/ctype_h.htm或查找ctype的函数。C标准库的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.

很好,你得到了输入。谢谢你的信息。我只是查了一下backspace代码,发现了这个问题。

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;行吗?

#5


-3  

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

我认为系统输入驱动是行缓冲的。所以在标准C中是不可能的。