分享
 
 
 

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

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

#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\n",i,(i>1)?"Times":"Time");

printf("KB_State.Shift.R_Pressed:%d\n",KB_State.Shift.Pressed[1]);

printf("KB_State.Shift.L_Pressed:%d\n",KB_State.Shift.Pressed[0]);

printf("KB_State.Ctrl.R_Pressed :%d\n",KB_State.Ctrl.Pressed[1] );

printf("KB_State.Ctrl.L_Pressed :%d\n",KB_State.Ctrl.Pressed[0] );

printf("KB_State.Alt.R_Pressed :%d\n",KB_State.Alt.Pressed[1] );

printf("KB_State.Alt.L_Pressed :%d\n", KB_State.Alt.Pressed[0] );

printf("KB_State.Scroll.Pressed :%d\n",KB_State.Scroll.Pressed );

printf("KB_State.Scroll.Enabled :%d\n",KB_State.Scroll.Enabled );

printf("KB_State.Num.Pressed :%d\n", KB_State.Num.Pressed );

printf("KB_State.Num.Enabled :%d\n", KB_State.Num.Enabled );

printf("KB_State.Caps.Pressed :%d\n", KB_State.Caps.Pressed );

printf("KB_State.Caps.Enabled :%d\n", KB_State.Caps.Enabled );

printf("KB_State.Insert :%d\n", KB_State.Insert );

printf("KB_State.Print :%d\n", 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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有