getchar()的怪异行为
目前,我正在使用C语言进行一个项目,其中我必须编写一段时间循环才能不断接收键盘输入或操纵杆输入。操纵杆输入还可以,但是对于键盘输入,有两种输入类型:箭头和正常输入('a','b',...)。对于键盘输入,我引用了此链接( https://stackoverflow.com/a/11432632 )以接收箭头键。 这是我的代码:
while(true){
if (getchar() == '\033') { // if the first value is esc
getchar(); // skip the [
switch(getchar()) { // the real value
case 'A':
printf("arrow up \n");
break;
case 'B':
printf("arrow down \n");
break;
case 'C':
printf("arrow right \n");
break;
case 'D':
printf("arrow left \n");
break;
}
}
if (getchar() != '\033'){
printf("non arrow \n");
}
}
但是,即使我按下了箭头按钮,也会不断出现“非箭头”。
如果我通过具有变量char c从printf(“ non Arrow \ n”)更改了代码,请将getchar()分配给它,然后将getchar()和以后的打印c:
if (getchar() != '\033'){
printf("non arrow \n");
}
输出量为箭头密钥的预期(接收和打印符合预期),但是输入“ E”或“ R”或其他单个字符键时,什么都不会出现。
我想知道我的代码问题是什么,如何将其修改以按照我的意愿接收行为。
我希望尽快得到答案。
谢谢你,恩吉恩。
Currently, I am doing a project in c language in which I have to write a while loop to constant receive either keyboard input or joystick input. The joystick input is okay but for the keyboard input, there are 2 types of inputs: arrow and normal input ('a', 'b', ...). For the keyboard input, I referred to this link (https://stackoverflow.com/a/11432632) to receive arrow keys.
Here is my code:
while(true){
if (getchar() == '\033') { // if the first value is esc
getchar(); // skip the [
switch(getchar()) { // the real value
case 'A':
printf("arrow up \n");
break;
case 'B':
printf("arrow down \n");
break;
case 'C':
printf("arrow right \n");
break;
case 'D':
printf("arrow left \n");
break;
}
}
if (getchar() != '\033'){
printf("non arrow \n");
}
}
However, the "non arrow" constantly appears even though I pressed arrow button.
If I changed the code from printf("non arrow \n") by have a variable char c, assign getchar() to it and later print c:
if (getchar() != '\033'){
printf("non arrow \n");
}
The output will be as expected for arrow key (receive and print as expected) but nothing appeared when 'e' or 'r' or other single character key is entered.
I wonder what is the problem for my code and how can I modify it to receive behavior as I want.
I hope to receive answer soon.
Thank you, Huy Nguyen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您执行
时,您正在阅读另一个字符,如果(getchar()!='\ 033')
,而不是测试与第一次测试相同的字符。使用
else
,而不是再次调用getchar()
。我添加了
默认值:
case switch ,以防它们发送不同的逃生序列。您可能还应该检查ESC
是[
之后的第一个字符,而不仅仅是假设它是。如果您有两个以上的条件,则可以使用
switch(getchar())
,就像ecc [
之后的字符一样,也可以分配getchar()
到变量并在条件下测试。另外,如果您在测试相互排他性的条件时,则应使用 else 。
You're reading another character when you do
if (getchar() != '\033')
, not testing the same character that you tested the first time.Use
else
instead of callinggetchar()
again.I've added a
default:
case to theswitch
in case they send a different escape sequence. You should probably also check that the first character afterESC
is[
, not just assume it will be.If you have more than two conditions, you can use
switch(getchar())
there, like you do with the character afterESC [
, or you can assign the result ofgetchar()
to a variable and test that in the conditions.Also, you should use
else if
when you're testing mutually-exclusive conditions.