的getchar()最多和向下箭头键返回相同的值(27) [英] getchar() returns the same value (27) for up and down arrow keys

查看:338
本文介绍了的getchar()最多和向下箭头键返回相同的值(27)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,对于键盘上的向上键,我得到27,令人惊讶的向下键我也得到27.我需要我的程序来表现不同的上下键,我似乎无法弄清楚。我使用Linux,并且需要它为Linux工作。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&文件ctype.h GT;
诠释的main()
{
    INT C =的getchar();    如果(C == 27)
    {
        的printf(UP);
    }    如果(C == 28)
    {
        的printf(DOWN);
    }}


解决方案

27 意味着你得到的箭头ANSI转义序列。他们将是三个字符的序列:27,91,然后65,66,67,68(IIRC)的上,下,左,右。如果你收到了 27 从调用的getchar(),然后调用它两次获得 91 并确定哪些箭头键是pressed数量。

至于别人提到的,这是特定于平台的,但你可能没在意。

So for the up key on the keyboard, I get 27, surprisingly for the down key I also get 27. I need my program to behave differently on the up and down key, and I can't seem to figure it out. I am using Linux, and need it to work for Linux.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
    int c = getchar();

    if(c==27)
    {
        printf("UP");
    }

    if(c==28)
    {
        printf("DOWN");
    }

} 

解决方案

The 27 implies that you're getting ANSI escape sequences for the arrows. They're going to be three-character sequences: 27, 91, and then 65, 66, 67, 68 (IIRC) for up, down, right, left. If you get a 27 from a call to getchar(), then call it twice more to get the 91 and the number that determines what arrow key was pressed.

As someone else mentioned, this is platform-specific, but you may not care.

这篇关于的getchar()最多和向下箭头键返回相同的值(27)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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