新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 C/C++编程思想 』 → Dos下键盘的完全控制 ------- 一系列的BIOS级别的键盘控制函数! 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 3930 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: Dos下键盘的完全控制 ------- 一系列的BIOS级别的键盘控制函数! 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 Dos下键盘的完全控制 ------- 一系列的BIOS级别的键盘控制函数!

    #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:       <Marked With '!!!!!' at below.>

        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 <DOS.H>
    #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<KeyCode_Num-1;i++)
          { Flag=0;
    for(j=0;j<KeyCode_Num-1-i;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<KeyCode[Now][0])
         {
          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'<Which is a sturcture.> */
       {
       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 %s",i,(i>1)?"Times":"Time");
    printf("KB_State.Shift.R_Pressed:%d",KB_State.Shift.Pressed[1]);
    printf("KB_State.Shift.L_Pressed:%d",KB_State.Shift.Pressed[0]);
    printf("KB_State.Ctrl.R_Pressed :%d",KB_State.Ctrl.Pressed[1] );
    printf("KB_State.Ctrl.L_Pressed :%d",KB_State.Ctrl.Pressed[0] );
    printf("KB_State.Alt.R_Pressed  :%d",KB_State.Alt.Pressed[1]  );
    printf("KB_State.Alt.L_Pressed  :%d", KB_State.Alt.Pressed[0] );
    printf("KB_State.Scroll.Pressed :%d",KB_State.Scroll.Pressed  );
    printf("KB_State.Scroll.Enabled :%d",KB_State.Scroll.Enabled  );
    printf("KB_State.Num.Pressed    :%d", KB_State.Num.Pressed    );
    printf("KB_State.Num.Enabled    :%d", KB_State.Num.Enabled    );
    printf("KB_State.Caps.Pressed   :%d",  KB_State.Caps.Pressed  );
    printf("KB_State.Caps.Enabled   :%d", KB_State.Caps.Enabled   );
    printf("KB_State.Insert         :%d", KB_State.Insert         );
    printf("KB_State.Print          :%d", 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


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/12/11 9:44:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/11/28 13:25:57

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    125.000ms