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:
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,
' '.',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
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);
/*
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(",(%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("fslf");
if(The_Key.Either[0]==27)
break;
}
} */
/* test 3 */
/*
main()
{
Detect_Key();
Key();
clrscr();
printf(",(%x,%x)",*Kb_Buf_Start,*Kb_Buf_End,*Kb_Buf_Head,*Kb_Buf_Tail);
getch();
}*/
#endif
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-998618/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 有趣的Python:Python控制鍵盤滑鼠Python
- 鍵盤控制滑鼠 windows QtWindowsQT
- 鍵盤控制游標移動作業
- 機械鍵盤怎麼選購?機械鍵盤和普通鍵盤的區別對比
- JavaScript經典案例:鍵盤控制元素移動JavaScript
- 解密鍵盤輸入:探索裝置控制器的奧秘解密
- Mac技巧|如何用鍵盤快捷鍵開啟 macOS 控制中心Mac
- 51、52微控制器使用矩陣鍵盤矩陣
- 去掉鍵盤的方式
- 鍵盤四種軸手感哪個好 鍵盤各類軸之間的區別
- 鍵盤各個鍵的功能圖解 電腦鍵盤全圖詳細圖解
- 膝上型電腦鍵盤上的Fn鍵作用大全 鍵盤上的Fn鍵有什麼用?
- 鍵盤快捷鍵
- 鍵盤快捷鍵使用大全表圖片 電腦鍵盤的快捷鍵用法大全圖解圖解
- win10遊戲鍵盤失靈怎麼辦_win10打遊戲鍵盤無法控制 一鍵修復Win10遊戲
- 控制make的函式函式
- javaScript實現鍵盤控制元素移動位置及縮放JavaScript
- 直播系統平臺搭建,控制鍵盤彈出收縮
- 在 Linux 中加速工作的鍵盤快捷鍵Linux
- 鍵盤軸色不同功能有什麼區別 機械鍵盤的各種軸有什麼區別
- 【快捷鍵】—— 鍵盤篇
- Java中的鍵盤錄入Java
- 程式設計師的鍵盤程式設計師
- Win10系統怎麼使用鍵盤控制滑鼠【圖文】Win10
- 怎樣用一個滑鼠和鍵盤控制兩臺電腦
- 從鍵盤鍵入String型別的資料插入資料庫中型別資料庫
- win10系統下鍵盤ctrl鍵失靈的解決方法Win10
- 鍵盤亂鍵怎麼處理 電腦鍵盤按鍵錯亂
- iOS 九宮格鍵盤的UIKeyboardTypeNumbersAndPunctuation預設型別iOSUI型別
- 筆記本鍵盤進水個別鍵失靈的解決方法 電腦鍵盤進水了該怎麼辦?筆記
- 鍵盤操作
- 鍵盤事件事件
- 聯想筆記本怎麼進入bios 聯想筆記本進bios按鍵盤哪個鍵筆記iOS
- AutoTyper for Mac(鍵盤快捷鍵)Mac
- 筆記本鍵盤個別鍵失靈怎麼修復 筆記本自帶鍵盤失靈了的解決教程筆記
- 鍵盤壞了怎麼用軟鍵盤 電腦怎麼調出桌面鍵盤
- mac中使用妙控鍵盤頂部的功能鍵Mac
- Mac電腦上“預覽”中的鍵盤快捷鍵!Mac
- Mac 鍵盤與滑鼠的對映Mac