我正在使用C/C++在Linux上实现一个键盘读取程序。我知道未经缓冲的getchar函数将返回键的少量数据值。对于所有ASCII键(a-z,A-Z,1-9,标点符号,回车,制表符和ESC),getchar()将返回单个值。对于其他键,例如箭头键,将读取ESC键,但是当再次调用getchar()时,它将获得另一个值(A、B、C或D)。
A = 65
B = 66
向上箭头 = 27 91 65
F5 = 27 91 49 53 126
ESC = 27
完整表格在此处 有没有办法检查是否有更多字符需要读取或只有一个字符?当读取键并且其第一个值为ESC时,我不知道它是以ESC开头的功能键还是仅为ESC键。
A = 65
B = 66
向上箭头 = 27 91 65
F5 = 27 91 49 53 126
ESC = 27
完整表格在此处 有没有办法检查是否有更多字符需要读取或只有一个字符?当读取键并且其第一个值为ESC时,我不知道它是以ESC开头的功能键还是仅为ESC键。
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>
using namespace std;
int main(int argc, char *argv[]) {
int ch[5];
int i;
struct termios term;
tcgetattr( STDIN_FILENO, &term );
term.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &term );
ch[0] = getchar();
// If ch[0] = 27 and there is more data in the buffer
// printf("You pressed a function key");
// Else
// printf("You pressed ESC");
return 0;
}