DOS's full control of the keyboard ------ a series of BIOS level keyboard control functions!

zhaozj2021-02-11  225

#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 / * '000000000101000000', 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 ', 10,' (', 10,' 0 ', 11,') ', 11,' - ', 12,' _ ', 12, '=', 13, ' ', 13, '//', 43, '|', 43, '`', 41, '~', 41, 8, 14, 9, 15, 'a' , 30, 'b', 48, 'c', 46, 'd', 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, ',', 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;} 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 [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];}}}}}} keycode [Keycode_num] [0] = 1; Return;}

INT keycode_find (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]) {start = now 1; Continue;} if (ch

}

/ ********!!!!!!!!! ********* / 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 preserated 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 = (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: 'INSE RT 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 Download Adobe Reader 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 {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) /1" ,kb_state.scroll.enabled); Printf ("kb_state.num.pressed:% D / N", KB_STATE.NUM.PRESSED); Printf ("kb_state.num.enable) :% 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 ()) {clrs CRSCR ();

Printf ("<% C,% 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 (); clrs CR (); printf ("<% x,% x>, (% x,% x)", * kb_buf_start, * kb_buf_end, * kb_buf_head, * kb_buf_tail) GetCh ();} * /

#ENDIF

转载请注明原文地址:https://www.9cbs.com/read-4363.html

New Post(0)