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

发布于 2024-10-06 08:31:47 字数 169 浏览 0 评论 0原文

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

我在我的 C 程序中使用 getch() 函数,即“key = getch()”,我想检查何时按下退格键。该行:

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

不起作用。

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:

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

doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凡间太子 2024-10-13 08:31:47

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

编辑

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

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

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

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.

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:

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 ==

薄暮涼年 2024-10-13 08:31:47

I/O 流的类型可能会有所帮助。标准输入流是一种行缓冲流,在您向其中写入 '\n' 字符之前,它不会刷新。满缓冲流在缓冲区满之前不会刷新。如果您在完整的 buff 流中写入退格键,则可能会捕获“\b”。

参考unix环境优势程序。

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.

Reference the unix environment advantage program.

小帐篷 2024-10-13 08:31:47

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

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.

莳間冲淡了誓言ζ 2024-10-13 08:31:47

试试这个:

#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); 
}

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

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

很高兴您能得到意见。
感谢大家提供的信息。我只是在查找退格代码并发现了这个问题。

顺便说一句,在任何字符之前尝试“\0”。不确定它会做什么,但它会停止其后的所有代码。是不是像return 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.

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?

蝶…霜飞 2024-10-13 08:31:47

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文