|
发表于 2004-8-28 15:39:25
|
显示全部楼层
是C程序(不叫GCC程序)。处理键盘消息要根据你所写的应用以及要采用的库来决定,比如SDL和gtk等都提供了现成的函数,如果你只是想用C和一些基本库来处理键盘输入,一般都会用到ioctl,如下所示函数可以读取键盘输入,但无输入亦会返回。
[code:1]
#include <termios.h>
#include <sys/ioctl.h>
int getkey(void)
{
struct termio zap, original;
int e;
char c;
ioctl(fileno(stdin), TCGETA, &original); /* Get termio */
zap = original;
zap.c_cc[VMIN] = 0; /* Modify termio */
zap.c_cc[VTIME] = 0;
zap.c_lflag = 0;
ioctl(fileno(stdin), TCSETA, &zap); /* Set new termio */
e = read(fileno(stdin), &c, 1); /* Read one char */
ioctl(fileno(stdin), TCSETA, &original); /* Restore termio */
if (e != 1)
return 0; /* No key pressed. */
return c; /* Return key. */
}
[/code:1]
如何应用可再看以下例子(它会等待键被按下):
[code:1]
#define K_ESC -1
#define K_UP -8
#define K_DOWN -9
#define K_LEFT -10
#define K_RIGHT -11
#define K_ENTER -12
#define K_SPACE -13
#define K_F1 -21
#define K_F2 -22
#define K_F3 -23
#define K_F4 -24
#define K_F5 -25
#define K_UNUSE -111
int my_getkey(void)
{
int key;
for(;;){
key=vga_getkey();
if(key!=0)break; /* If no key pressed then continue. */
}
if(key != 0x1b) { /* not ESC */
switch (key) {
case 10:
return K_ENTER; /* ENTER */
case 32:
return K_SPACE; /* Space */
default:
return key;
}
}
if((key = vga_getkey()) != '[') {
return K_ESC; /* it was ESC */
}
switch (key = vga_getkey()) {
case 'A':
return K_UP; /* up */
case 'B':
return K_DOWN; /* down */
case 'C':
return K_RIGHT; /* right */
case 'D':
return K_LEFT; /* left */
case '[':
switch (key = vga_getkey()) {
case 'A':
return K_F1;
case 'B':
return K_F2;
case 'C':
return K_F3;
case 'D':
return K_F4;
case 'E':
return K_F5;
default:
return -111;
}
default:
return -111; /* ignore */
}
}
[/code:1] |
|