DOS下键盘中断处理程序

软件开发大郭
0 评论
/
20 阅读
/
7830 字
07 2022-10

键盘操作要拦截键盘中断,不用系统给的函数。

下面是例子。使用时,在你的循环中直接监测key_ascii就行了。

/*-------------------------------------------------------------------------------*/
#define SCAN_ALT    56
#define SCAN_CTRL     29
#define SCAN_caps 58
#define SCAN_LEFTSHIFT 42
#define SCAN_RIGHTSHIFT 54
#define SCAN_SHIFT (keyflag[SCAN_RIGHTSHIFT]||keyflag[SCAN_LEFTSHIFT])
#define KEY_END 255
#define KEY_LEFT 254
#define KEY_RIGHT 253
#define KEY_PAGEUP 252
#define KEY_UP 251
#define KEY_DOWN 250
#define KEY_HOME 249
#define KEY_CTRLBREAK 248
#define KEY_F1 247
#define KEY_F2 246
#define KEY_F3 245
#define KEY_F4 244
#define KEY_F5 243
#define KEY_F6 242
#define KEY_F7 241
#define KEY_F8 240
#define KEY_F9 239
#define KEY_F10 238
#define KEY_PAGEDOWN 237
#define KEY_INSERT 236
#define KEY_DELETE 235
#define KEY_LEFTALT 234
#define KEY_RIGHTALT 233
#define KEY_RIGHTCTRL 232
#define KEY_LEFTCTRL 231
#define KEY_caps 230
#define KEY_F11 229
#define KEY_F12 228
#define KEY_PRINTSCREEN 228
#define KEY_NUMLOCK 227
#define KEY_SCROLLLOCK 226
#define KEY_LEFTSHIFT 225
#define KEY_RIGHTSHIFT 224
#define KEY_WINDOWS 223
/*-------------------------------------------------------------------------------*/
static unsigned char  asciinames[]={
 0,27,'1','2','3','4','5','6','7','8','9','0','-','=',8,9,
 'q','w','e','r','t','y','u','i','o','p','[',']',13,KEY_LEFTCTRL,'a','s',
 'd','f','g','h','j','k','l',';',39,'`',0,92,'z','x','c','v',
 'b','n','m',',','.','/',0,'*',KEY_LEFTALT,' ',KEY_caps,KEY_F1,KEY_F2,KEY_F3,KEY_F4,KEY_F5,
 KEY_F6,KEY_F7,KEY_F8,KEY_F9,KEY_F10,KEY_NUMLOCK,KEY_SCROLLLOCK,'7','8','9','-','4','5','6','+','1',
 '2','3','0',127,0,0,'\\',KEY_F11,KEY_F12,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
     };
/*-------------------------------------------------------------------------------*/
static unsigned char  shiftnames[]={
 0,27,'!','@','#','$','%','^','&','*','(',')','_','+',8,9,
 'Q','W','E','R','T','Y','U','I','O','P','{','}',13,1,'A','S',
 'D','F','G','H','J','K','L',':',34,'~',KEY_LEFTSHIFT,'|','Z','X','C','V',
 'B','N','M','<','>','?',KEY_RIGHTSHIFT,'*',1,' ',0,0,0,0,0,0,
 0,0,0,0,0,0,0,'7','8','9','-','4','5','6','+','1',
 '2','3','0',127,0,0,'|',0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0
     };
/*-------------------------------------------------------------------------------*/
static char specialnames[]={
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,13,KEY_RIGHTCTRL,0,0,
 0,0,0,0,0,0,0,0,0,0,KEY_WINDOWS,0,0,0,0,0,
 0,0,0,0,0,'/',0,KEY_PRINTSCREEN,KEY_RIGHTALT,0,0,0,0,0,0,0,
 0,0,0,0,0,0,KEY_CTRLBREAK,KEY_HOME,KEY_UP,KEY_PAGEUP,0,KEY_LEFT,0,KEY_RIGHT,0,KEY_END,
 KEY_DOWN,KEY_PAGEDOWN,KEY_INSERT,KEY_DELETE,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    };
/*-------------------------------------------------------------------------------*/
unsigned char pause_key;
unsigned char key_ascii;
unsigned char key_scan;
unsigned char keyflag[128];
static unsigned char caps;
static unsigned char cur_code,key_code;
static void (_interrupt far *_old_key_interrupt)(void);
/*-------------------------------------------------------------------------------*/
void setkeyspeed()
{
 REGS regs;
 regs.w.bx=0x0;
 regs.w.ax=0x0A05;
 int386 (0x16,&regs,&regs);
}
/*-------------------------------------------------------------------------------*/
void clearkey()
{
 int i;
 key_scan=0;
 key_ascii=0;
 memset(keyflag,0,sizeof(keyflag));
}
/*-------------------------------------------------------------------------------*/
static void interrupt key_interrupt(void)
{
static unsigned char specialflag;
unsigned char k,c,temp;
int i;
k=inp(0x60);
outp(0x61,(temp=inp(0x61))|0x80);
outp(0x61,temp);
if(k==0xe0)specialflag=1;
else if(k==0xe1)pause_key=1;
else
{
 if(k&0x80)
 {
  k&=0x7f;
  keyflag[k]=0;
 }
 else
 {
  key_code=cur_code;
  cur_code=key_scan=k;
  keyflag[k]=1;
  if(specialflag)c=specialnames[k];
  else
  {
   if(k==SCAN_caps)
   {
    caps=(~caps)&1;
   }
   if(SCAN_SHIFT)
   {
    c=shiftnames[k];
    if((c>='A')&&(c<='Z')&&caps)
    c+='a'-'A';
   }
   else
   {
    c=asciinames[k];
    if((c>='a')&&(c<='z')&&caps)
    c-='a'-'A';
   }
  }
  if(c)key_ascii=c;
 }
 specialflag=0;
}
outp(0x20,0x20);
}
/*-------------------------------------------------------------------------------*/
static void initkey(void)
{
 clearkey();
 setkeyspeed();
 _old_key_interrupt=_dos_getvect(9);
 _dos_setvect(9,key_interrupt);
}
/*-------------------------------------------------------------------------------*/
static void closekey(void)
{
 _dos_setvect(9,_old_key_interrupt);
}
/*-------------------------------------------------------------------------------*/
unsigned char getscan(void)
{
 unsigned char result;
 while((result=key_scan)!=0);
 key_scan=0;
 return(result);
}
/*-------------------------------------------------------------------------------*/
unsigned char getkey(void)
{
 unsigned char result;
 while((result=key_ascii)==0);
 key_ascii=0;
 return(result);
}




    暂无数据