ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Dos下键盘的完全控制 ------- 一系列的BIOS级别的键盘控制函数! (转)

Dos下键盘的完全控制 ------- 一系列的BIOS级别的键盘控制函数! (转)

原创 Linux操作系统 作者:amyz 时间:2007-11-29 17:13:42 0 删除 编辑
Dos下键盘的完全控制 ------- 一系列的BIOS级别的键盘控制函数! (转)[@more@]

#ifndef XRH_KEYS_H
#define XRH_KEYS_H
/*
  Topic:
  This is The KeyBoard Controlling Functions Set

  [Author:superspirit]

  *******************************
  * SuperSpirit Software Studio *
  *******************************
  modified date: 2001-10-11
-----------------------------------------------------------------------------

  Including 6 function: 

  1> void Clean_Kb_Buf(void)  *** Cleaning The KeyBord Buffer  ***
  2> void Get_Key(void)  *** Geting The Key's Ascii And ScanCode  ***
  3> int  Detect_Key(void)  *** Detecting The Key Pressing And Geting Key ***
  4> void Get_KB_State(void)  *** Getting The KeyBoard Status  ***
  5> void Send_Key(char far *Key_p)  *** Send Some Key To KeyBoard Buffer  ***
  6> void Set_Key (char AscII,char ScanCode) *** Send One Character To Key Board Buffer  ***
*/


#include
#define  ZERO_FLAG 0x040  /* '0000000001000000',ZeroFlag in FLAG_REGISTER */
#define  KEYBOARD  0x16  /* KeyBoard Interrupt Number  */

/*Below:Defined For Register 'AL' */

#define  R_SHIFT_PRESSED  0x01
#define  L_SHIFT_PRESSED  0x02
#define  CTRL_PRESSED  0x04
#define  ALT_PREESED  0x08
#define  SCROLL_ENABLED  0x10
#define  NUM_ENABLED  0x20
#define  CAPS_ENABLED  0x40
#define  INSERT_PRESSED  0x80
/*Below:Defined For Register 'AH' */

#define  L_CTRL_PRESSED  0x01
#define  L_ALT_PRESSED  0x02
#define  R_CTRL_PRESSED  0x04
#define  R_ALT_PRESSED  0x08
#define  SCROLL_PRESSED  0x10
#define  NUM_PRESSED  0x20
#define  CAPS_PRESSED  0x40
#define  PRINT_PRESSED  0x80

 

/**************************/

  int KeyCode_Num=73;
  unsigned char KeyCode[74][2]={
  27,  1,
  '1', 2,
  '!', 2,
  '2', 3,
  '@', 3,
  '3', 4,
  '#', 4,
  '4', 5,
  '$', 5,
  '5', 6,
  '%', 6,
  '6', 7,
  '^', 7,
  '7', 8,
  '&', 8,
  '8', 9,
  '*', 9,
  '9',10,
  '(',10,
  '0',11,
  ')',11,
  '-',12,
  '_',12,
  '=',13,
  '+',13,
  '',43,
  '|',43,
  '`',41,
  '~',41,
  8 ,14,
  9 ,15,
  'A',30,
  'B',48,
  'C',46,
  'D',32,
  'E',18,
  'F',33,
  'G',34,
  'H',35,
  'I',23,
  'J',36,
  'K',37,
  'L',38,
  'M',50,
  'N',49,
  'O',24,
  'P',25,
  'Q',16,
  'R',19,
  'S',31,
  'T',20,
  'U',22,
  'V',47,
  'W',17,
  'X',45,
  'Y',21,
  'Z',44,
  ',',51,
  '<',51,
  '.',52,
  '>',52,
  '/',53,
  '?',53,
  ';',39,
  ':',39,
  39,40,
  '"',40,
  '[',26,
  '{',26,
  ']',27,
  '}',27,
  13,28,
  ' ',37,
  0, 0,
  };


typedef union {
  unsigned  int  Key_Num;
  unsigned char Either[2];
  }KB_Key;

typedef union {
  unsigned char Pressed[2];/* Pressed[0] is LEFT,Pressed[1] is RIGHT */
  unsigned IsPressed;
  } Press_It_1;

typedef struct{
  unsigned char Pressed;
  unsigned char Enabled;
  } Press_It_2;

typedef struct{

  Press_It_1  Shift ,  Ctrl ,  Alt ;
  Press_It_2  Scroll,  Num  ,  Caps;
  unsigned char  Insert,  Print;

  } KeyBoard_State;
 /**************************************************************************/


 KB_Key  The_Key;  /* Define Static Symbol Holding KeyBoard Status. */

 KeyBoard_State  KB_State;


 union REGS in_regs,out_regs;

 /* Get KeyBoard Buffer Pointer */

 unsigned far *Kb_Buf_Start=(unsigned far *)0x00400080;
 unsigned far *Kb_Buf_End  =(unsigned far *)0x00400082;
 unsigned far *Kb_Buf_Head =(unsigned far *)0x0040001A;
 unsigned far *Kb_Buf_Tail =(unsigned far *)0x0040001C;
  char  far *Kb_Buf_Ascii=(  char  far *)0x00400000;
  char  far *Kb_Buf_ScanCode=(char  far *)0x00400000;
 /*********!!!!!**************/
 void Set_Key(char Ascii,char ScanCode)
  {
  Kb_Buf_Ascii[*Kb_Buf_Tail]=Ascii;
  Kb_Buf_ScanCode[*Kb_Buf_Tail+1]=ScanCode;
  *Kb_Buf_Tail+=2;
  if(*Kb_Buf_Tail>=*Kb_Buf_End)
 *Kb_Buf_Tail=*Kb_Buf_Start;
  return;
  }


 void KeyCode_Sort(void)
  {
  int i=0,j=0,Flag;
  unsigned char Mid[2];
  if(KeyCode[KeyCode_Num][0])
 return;
  for(i=0;i  { Flag=0;
 for(j=0;j  {
  if(KeyCode[j][0]>KeyCode[j+1][0])
  {
  Flag=1;
  Mid[0]=KeyCode[j][0];
  Mid[1]=KeyCode[j][1];
  KeyCode[j][0]=KeyCode[j+1][0];
  KeyCode[j][1]=KeyCode[j+1][1];
  KeyCode[j+1][0]=Mid[0];
  KeyCode[j+1][1]=Mid[1];
  }
  }

  if(!Flag) break;
  }
  KeyCode[KeyCode_Num][0]=1;
  return;
  }

 int KeyCode_Find(char Ch)
  {
 int Start=0,End=KeyCode_Num-1;
 int Now=0;
 if(Ch>='a'&&Ch<='z')
  Ch-=32;
  while(1)
 {
  Now=(Start+End)/2;

  if(Start>End) return -1;
  if(Ch>KeyCode[Now][0])
  {
  Start=Now+1;continue;
  }
  if(Ch  {
  End=Now-1;continue;
  }
  if(Ch==KeyCode[Now][0])
  {
  return Now;
  }

  }
 }

 /********!!!!!!!!!*********/
void Send_Key(char far *Key_p)  /* Send Some Key To KeyBoard Buffer,As Input From KeyBoard?*/
  {
  int i=0;

  KeyCode_Sort();

  for(;*Key_p;Key_p++)
  {
  i=KeyCode_Find(*Key_p);
  if(i>=0)
  Set_Key(*Key_p,KeyCode[i][1]);
  }
  return;
  }


/************!!!!!!!!!!*********/
void Clean_Kb_Buf(void) /*** Cleaning The KeyBord Buffer ***/
 {
 *Kb_Buf_Head=*Kb_Buf_Tail;
  }

 /**********************/

void Adjust(void)
  {
  if(The_Key.Either[0]==0xE0)
  The_Key.Either[0]=0;
  return;
  }

/*******!!!!!********/
void Get_Key(void) /* Get Key's ASCII & Key's Scancode */
  {
  in_regs.h.ah=0x10;

  int86(KEYBOARD,&in_regs,&out_regs);
  The_Key.Key_Num=out_regs.x.ax; /*ASCII is in Either[0],ScanCode is in Either[1]*/

  Adjust();

  return;
  }

/*******!!!!!*****************/
 int Detect_Key(void)  /* Detect If A Key Be Preesed Now */
  {
  in_regs.h.ah=0x11;
  int86(KEYBOARD,&in_regs,&out_regs);
  if(out_regs.x.flags&ZERO_FLAG)
  return (0);
  The_Key.Key_Num=out_regs.x.ax; /* ASCII is in Either[0],ScanCode is in Either[1] */
  Adjust();
  return(1);
  }
/************!!!!!*************************/
 /*char far *KeyState=(char far *) ((unsigned long )(0x40<<16)+0x17);*/
  char far *KeyState=(char far*)MK_FP(0x0040,0x0017);
/*
  BIOS Data Area 0040:0017 KeyBoard Control Key Status: ( 1 Byte,8 Bits)
  bit 0: Right 'Shift' Key--- 1 -> Pressed,0-> Unpressed
  bit 1: Left  'Shift' Key--- 1 -> Pressed,0-> Unpressed
  bit 2: 'Ctrl' Key  --- 1 -> pressed,0-> Unpressed
  bit 3: 'Alt'  Key  --- 1 -> pressed,0-> Unpressed
  bit 4: 'Scroll Lock Key'--- 1 -> Open  ,0-> Close
  bit 5: 'Num Lock  Key'  --- 1 -> Open  ,0-> Close
  bit 6: 'Caps Lock Key  --- 1 -> Open  ,0-> Close
  bit 7: 'Insert Lock Key --- 1 -> Open  ,0-> Close
*/

void Get_KB_State(void) /* Get KeyBoard's Status To a Symbol 'KB_State' */
  {
  if(Detect_Key()) Get_Key();
  in_regs.h.ah=0x12;
  int86(KEYBOARD,&in_regs,&out_regs); /* KeyBoard Interrupter */
  /***** Below is  Geting State of KeyBord ****************/

  KB_State.Shift.Pressed[1]=out_regs.h.al & R_SHIFT_PRESSED;
  KB_State.Shift.Pressed[0]=out_regs.h.al & L_SHIFT_PRESSED;
  KB_State.Ctrl.Pressed[1] =out_regs.h.ah & R_CTRL_PRESSED;
  KB_State.Ctrl.Pressed[0] =out_regs.h.ah & L_CTRL_PRESSED;
  KB_State.Alt.Pressed[1]  =out_regs.h.ah & R_ALT_PRESSED;
  KB_State.Alt.Pressed[0]  =out_regs.h.ah & L_ALT_PRESSED;
  KB_State.Scroll.Pressed  =out_regs.h.ah & SCROLL_PRESSED;
  KB_State.Scroll.Enabled  =out_regs.h.al & SCROLL_ENABLED;
  KB_State.Num.Pressed  =out_regs.h.ah & NUM_PRESSED;
  KB_State.Num.Enabled  =out_regs.h.al & NUM_ENABLED;
  KB_State.Caps.Pressed  =out_regs.h.ah & CAPS_PRESSED;
  KB_State.Caps.Enabled  =out_regs.h.al & CAPS_ENABLED;
  KB_State.Insert  =out_regs.h.al & INSERT_PRESSED;
  KB_State.Print  =out_regs.h.ah & PRINT_PRESSED;
  return;
  }

  /************  ===== Fouctions End =====  *************/

 

 

 

 

 

 


/***  Testing *********************************/
/* test 1 */
/*
 main()
  {

  int i=0;
  Get_KB_State();
  while(!kbhit())
  {
  Get_KB_State();
if(KB_State.Insert||KB_State.Print||KB_State.Shift.IsPressed
  ||KB_State.Ctrl.IsPressed ||KB_State.Alt.IsPressed||KB_State.Scroll.Pressed
  ||KB_State.Num.Pressed||KB_State.Caps.Pressed)
 {
 i++;
 printf("**************************************************************%d %sn",i,(i>1)?"Times":"Time");
 printf("KB_State.Shift.R_Pressed:%dn",KB_State.Shift.Pressed[1]);
 printf("KB_State.Shift.L_Pressed:%dn",KB_State.Shift.Pressed[0]);
 printf("KB_State.Ctrl.R_Pressed :%dn",KB_State.Ctrl.Pressed[1] );
 printf("KB_State.Ctrl.L_Pressed :%dn",KB_State.Ctrl.Pressed[0] );
 printf("KB_State.Alt.R_Pressed  :%dn",KB_State.Alt.Pressed[1]  );
 printf("KB_State.Alt.L_Pressed  :%dn", KB_State.Alt.Pressed[0] );
 printf("KB_State.Scroll.Pressed :%dn",KB_State.Scroll.Pressed  );
 printf("KB_State.Scroll.Enabled :%dn",KB_State.Scroll.Enabled  );
 printf("KB_State.Num.Pressed  :%dn", KB_State.Num.Pressed  );
 printf("KB_State.Num.Enabled  :%dn", KB_State.Num.Enabled  );
 printf("KB_State.Caps.Pressed  :%dn",  KB_State.Caps.Pressed  );
 printf("KB_State.Caps.Enabled  :%dn", KB_State.Caps.Enabled  );
 printf("KB_State.Insert  :%dn", KB_State.Insert  );
 printf("KB_State.Print  :%dn", KB_State.Print  );
 }
  delay(5000);
  }
 return ;
 }
*/

/* test 2 */


/* main()
{
 int i;
 clrscr();
while(1)
 {

 if(Detect_Key())
 {
 clrscr();


 printf("<%c,%x>,(%x,%x)[%x,%x]",The_Key.Either[0],The_Key.Either[1],*Kb_Buf_Head,*Kb_Buf_Tail,*Kb_Buf_Start,*Kb_Buf_End );

 Clean_Kb_Buf();

 delay (30000);
 }
 printf("fslfsf");
 if(The_Key.Either[0]==27)
 break;

 }
} */

/* test 3 */

/*
 main()
 {
 Detect_Key();
 Key();
 clrscr();
 printf("<%x,%x>,(%x,%x)",*Kb_Buf_Start,*Kb_Buf_End,*Kb_Buf_Head,*Kb_Buf_Tail);
  getch();
 }*/

#endif


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-986997/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3984
  • 访问量
    7343023