|
发表于 2005-3-4 13:13:03
|
显示全部楼层
我刚好在整理这个东西,贡献出来吧,没怎么测试过
[code:1]
/* kbhit.h*/
#ifndef _MY_KBHIT_H_
#define _MY_KBHIT_H_
#define KEY_TAB 0x0009
#define KEY_ESC 0x001B
#define KEY_F1 0xff01
#define KEY_F2 0xff02
#define KEY_F3 0xff03
#define KEY_F4 0xff04
#define KEY_F5 0xff05
#define KEY_F6 0xff06
#define KEY_F7 0xff07
#define KEY_F8 0xff08
#define KEY_F9 0xff09
#define KEY_F10 0xff0a
#define KEY_F11 0xff0b
#define KEY_F12 0xff0c
#define KEY_INS 0xff0d
#define KEY_DEL 0xff0e
#define KEY_END 0xff0f
#define KEY_UP 0xff10
#define KEY_DOWN 0xff11
#define KEY_LEFT 0xff12
#define KEY_RIGHT 0xff13
#define KEY_HOME 0xff14
#define KEY_PgDn 0xff15
#define KEY_PgUp 0xff16
int set_tty();
int kbhit();
void restore();
#endif
[/code:1]
[code:1]
/* kbhit.c */
#include <stdio.h>
#include <termio.h>
#include "kbhit.h"
char keysym_def[][6]={
// first 5 bytes are key codes
{0x1b,0x5b,0x5b,0x41,0x00,(char) (0xff&KEY_F1)},
{0x1b,0x5b,0x5b,0x42,0x00,(char) (0xff&KEY_F2)},
{0x1b,0x5b,0x5b,0x43,0x00,(char) (0xff&KEY_F3)},
{0x1b,0x5b,0x5b,0x44,0x00,(char) (0xff&KEY_F4)},
{0x1b,0x5b,0x5b,0x45,0x00,(char) (0xff&KEY_F5)},
{0x1b,0x5b,0x31,0x37,0x7e,(char) (0xff&KEY_F6)},
{0x1b,0x5b,0x31,0x38,0x7e,(char) (0xff&KEY_F7)},
{0x1b,0x5b,0x31,0x39,0x7e,(char) (0xff&KEY_F8)},
{0x1b,0x5b,0x32,0x30,0x7e,(char) (0xff&KEY_F9)},
{0x1b,0x5b,0x32,0x31,0x7e,(char) (0xff&KEY_F10)},
{0x1b,0x5b,0x32,0x33,0x7e,(char) (0xff&KEY_F11)},
{0x1b,0x5b,0x32,0x34,0x7e,(char) (0xff&KEY_F12)},
{0x1b,0x5b,0x32,0x7e,0x00,(char) (0xff&KEY_INS)},
{0x1b,0x5b,0x33,0x7e,0x00,(char) (0xff&KEY_DEL)},
{0x1b,0x5b,0x34,0x7e,0x00,(char) (0xff&KEY_END)},
{0x1b,0x5b,0x41,0x00,0x00,(char) (0xff&KEY_UP)},
{0x1b,0x5b,0x42,0x00,0x00,(char) (0xff&KEY_DOWN)},
{0x1b,0x5b,0x44,0x00,0x00,(char) (0xff&KEY_LEFT)},
{0x1b,0x5b,0x43,0x00,0x00,(char) (0xff&KEY_RIGHT)},
{0x1b,0x5b,0x31,0x7e,0x00,(char) (0xff&KEY_HOME)},
{0x1b,0x5b,0x36,0x7e,0x00,(char) (0xff&KEY_PgDn)},
{0x1b,0x5b,0x35,0x7e,0x00,(char) (0xff&KEY_PgUp)},
{0x00,0x00,0x00,0x00,0x00,0x00}
};
struct termio ttysave;
int set_tty()
{
struct termio tty;
if(ioctl(0, TCGETA, &tty)==-1){
printf("error while setup tty\n");
return -1;
}
ttysave = tty;
tty.c_lflag &= ~(ICANON | ECHO| ISIG);
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
if(ioctl(0, TCSETAF, &tty)==-1){
restore();
printf("error while setup tty\n");
return -1;
}
return 0;
}
int kbhit()
{
char ch[10]={0,0,0,0,0,0,0,0,0,0};
read(0, ch, 10);
if(ch[0]>=0x20 && ch[0]<=0x7e || ch[0]==0x0a || ch[0] == 0x09)
return ch[0];
else if(ch[0] == 0x1b && ch[1] == 0x00)
return KEY_ESC;
else if(ch[0] == 0x1b && ch[1] == 0x5b)
{
int i=0;
while(keysym_def[i][0] != 0x00)
{
if(keysym_def[i][2] == ch[2]
&& keysym_def[i][3] == ch[3]
&& keysym_def[i][4] == ch[4])
{
return (0xff00+keysym_def[i][5]);
}
i++;
i++;
}
}
else
return 0x11;
}
void restore()
{
if(ioctl(0, TCSETAF, &ttysave)==-1){
printf("restore error\n");
}
return;
}
int main(int argc, char* argv[])
{
char ch;
char str[1024];
set_tty();
do{
ch=kbhit();
printf("You Press 0x%02x\n",ch);
}while(ch!=0x11); //Ctrl+C is 0x11
restore();
exit(0);
}
[/code:1] |
|