如何检查"退格"字符用C [英] how to check for the "backspace" character in C

查看:259
本文介绍了如何检查"退格"字符用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何检查,如果用户键入退格字。

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

我用在我的C程序中的的getch()函数,即键=残培(),我想检查时,退格为pressed。行:

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') { ....

不起作用。

推荐答案

有阅读问题<大骨节病>退格是,大多数终端像退格由终端驱动程序处理的键熟。不过,诅咒功能残培()可以阅读退格,因为它是不依赖于终端。

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.

我只注意到你的code的的使用残培()输入。我跑了一个小测试程序和残培()返回1​​27,当你点击退格键。因此尝试:

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 */

另外请注意,您的样本code使用分配运营商 = 时,它应该使用的平等运营商 ==

Also note that your sample code uses the assignment operator = when it should be using the equality operator ==

这篇关于如何检查&QUOT;退格&QUOT;字符用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆