密码和箭头代码

我正在编写一个getch()用于扫描箭头键的程序。到目前为止,我的代码是:


switch(getch()) {

    case 65:    // key up

        break;

    case 66:    // key down

        break;

    case 67:    // key right

        break;

    case 68:    // key left

        break;

}

问题是,当我按下'A','B','C'或'D'代码也将执行,因为65是十进制代码'A',等等。


有没有一种方法可以在不呼叫他人的情况下检查箭头键?


谢谢!


元芳怎么了
浏览 828回答 3
3回答

ABOUTYOU

通过按一个箭头键getch将三个值推入缓冲区:'\033''[''A','B','C'或者'D'因此,代码将如下所示:if (getch() == '\033') { // if the first value is esc    getch(); // skip the [    switch(getch()) { // the real value        case 'A':            // code for arrow up            break;        case 'B':            // code for arrow down            break;        case 'C':            // code for arrow right            break;        case 'D':            // code for arrow left            break;    }}

慕田峪9158850

如FatalError的注释中所述,getch()函数返回两个用于箭头键(以及一些其他特殊键)的键码。它首先返回0(0x00)或224(0xE0),然后返回一个代码,该代码标识按下的键。对于箭头键,它首先返回224,然后返回72(向上),80(向下),75(左)和77(右)。如果按下了数字键盘的箭头键(关闭了NumLock),getch()将首先返回0,而不是224。请注意,getch()并未以任何方式标准化,并且这些代码可能因编译器而异。这些代码由Windows上的MinGW和Visual C ++返回。查看各种键的getch()动作的便捷程序是:#include <stdio.h>#include <conio.h>int main (){&nbsp; &nbsp; int ch;&nbsp; &nbsp; while ((ch = _getch()) != 27) /* 27 = Esc key */&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; printf("%d", ch);&nbsp; &nbsp; &nbsp; &nbsp; if (ch == 0 || ch == 224)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf (", %d", _getch ());&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("\n");&nbsp; &nbsp; }&nbsp; &nbsp; printf("ESC %d\n", ch);&nbsp; &nbsp; return (0);}这适用于MinGW和Visual C ++。这些编译器使用名称_getch()而不是getch()来指示它是非标准函数。因此,您可以执行以下操作:ch = _getch ();if (ch == 0 || ch == 224){&nbsp; &nbsp; switch (_getch ())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; case 72:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Code for up arrow handling */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 80:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Code for down arrow handling */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; /* ... etc ... */&nbsp; &nbsp; }}

弑天下

因此,经过大量的奋斗,我奇迹般地解决了这个令人讨厌的问题!我试图模仿一个Linux终端,并卡在其中保留了命令历史记录的部分,可以通过按上下箭头键进行访问。我发现ncurses lib难以理解且学习缓慢。char ch = 0, k = 0;while(1){&nbsp; ch = getch();&nbsp; if(ch == 27)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if ch is the escape sequence with num code 27, k turns 1 to signal the next&nbsp; &nbsp; k = 1;&nbsp; if(ch == 91 && k == 1)&nbsp; &nbsp; &nbsp; &nbsp;// if the previous char was 27, and the current 91, k turns 2 for further use&nbsp; &nbsp; k = 2;&nbsp; if(ch == 65 && k == 2)&nbsp; &nbsp; &nbsp; &nbsp;// finally, if the last char of the sequence matches, you've got a key !&nbsp; &nbsp; printf("You pressed the up arrow key !!\n");&nbsp; if(ch == 66 && k == 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; printf("You pressed the down arrow key !!\n");&nbsp; if(ch != 27 && ch != 91)&nbsp; &nbsp; &nbsp; // if ch isn't either of the two, the key pressed isn't up/down so reset k&nbsp; &nbsp; k = 0;&nbsp; printf("%c - %d", ch, ch);&nbsp; &nbsp; // prints out the char and it's int code它有点大胆,但可以解释很多。祝好运 !
打开App,查看更多内容
随时随地看视频慕课网APP