Solved

keyboard status

Posted on 1997-03-03
1
606 Views
Last Modified: 2006-11-17
I need to test the keyboard status.
I mean: to know wich keys are pressed in a given instant.
It's important to reveal if more than one key are pressed (and holding pressed) at once.
I use Turbo C (1.0).
0
Comment
Question by:blaze030297
1 Comment
 

Accepted Solution

by:
Doug8 earned 100 total points
ID: 1249524
There is a lot of code for that, for all of the keys, but ill give it to ya anyway.

just put all this into your main program, it might scrunch up because of space though


void (_interrupt _far *Old_Keyboard_ISR)();  // holds old keyboard interrupt handler

int raw_scan_code=0;  // the global keyboard scan code

// this holds the keyboard state table which tracks the state of every key
// on the keyboard

int keyboard_state[128];

// this tracks the number of keys that are currently pressed, helps
// with keyboard testing logic

int keys_active = 0;

void _interrupt _far Keyboard_Driver()
{
// this function is used as the new keyboard driver. once it is installed
// it will continually update a keyboard state table that has an entry for
// every key on the keyboard, when a key is down the appropriate entry will be
// set to 1, when released the entry will be reset. any key can be querried by
// accessing the keyboard_state[] table with the make code of the key as the
// index

// need to use assembly for speed since this is an interrupt

_asm
   {
   sti                    ; re-enable interrupts
   in al, KEY_BUFFER      ; get the key that was pressed from the keyboard
   xor ah,ah              ; zero out upper 8 bits of AX
   mov raw_scan_code, ax  ; store the key in global variable
   in al, KEY_CONTROL     ; set the control register to reflect key was read
   or al, 82h             ; set the proper bits to reset the keyboard flip flop
   out KEY_CONTROL,al     ; send the new data back to the control register
   and al,7fh
   out KEY_CONTROL,al     ; complete the reset
   mov al,20h             ; reset command
   out PIC_PORT,al        ; tell PIC to re-enable interrupts

   } // end inline assembly

// update the keyboard table

// test if the scan code was a make code or a break code

if (raw_scan_code <128)
   {
   // index into table and set this key to the "on" state if it already isn't
   // on

   if (keyboard_state[raw_scan_code]==KEY_UP)
      {
      // there is one more active key

      keys_active++;

      // update the state table

      keyboard_state[raw_scan_code] = KEY_DOWN;

      } // end if key wasn't already pressed

   } // end if a make code
else
   {
   // must be a break code, therefore turn the key "off"
   // note that 128 must be subtracted from the raw key to access the correct
   // element of the state table

   if (keyboard_state[raw_scan_code-128]==KEY_DOWN)
      {
      // there is one less active key

      keys_active--;

      // update the state table

      keyboard_state[raw_scan_code-128] = KEY_UP;

      } // end if key wasn't already pressed

   } // end else

} // end Keyboard_Driver

///////////////////////////////////////////////////////////////////////////////

void Keyboard_Install_Driver(void)
{
// this function installs the new keyboard driver

int index; // used as loop variable

// clear out the keyboard state table

for (index=0; index<128; index++)
    keyboard_state[index]=0;

// save the old keyboard driver

Old_Keyboard_ISR = _dos_getvect(KEYBOARD_INTERRUPT);

// install the new keyboard driver

_dos_setvect(KEYBOARD_INTERRUPT, Keyboard_Driver);

} // end Keyboard_Install_Driver

///////////////////////////////////////////////////////////////////////////////

void Keyboard_Remove_Driver(void)
{

// this functions restores the old keyboard driver (DOS version) with the
// previously saved vector

_dos_setvect(KEYBOARD_INTERRUPT, Old_Keyboard_ISR);

} // end Keyboard_Remove_Driver



#define KEYBOARD_INTERRUPT  0x09  // the keyboard interrupt number
#define KEY_BUFFER          0x60  // the port of the keyboard buffer
#define KEY_CONTROL         0x61  // the port of the keyboard controller
#define PIC_PORT            0x20  // the port of the programmable
                                  // interrupt controller (PIC)

// bitmasks for the "shift state"

// note there is a difference between "on" and "down"

#define SHIFT_RIGHT         0x0001    // right shift
#define SHIFT_LEFT          0x0002    // left shift
#define CTRL                0x0004    // control key
#define ALT                 0x0008    // alternate key
#define SCROLL_LOCK_ON      0x0010    // the scroll lock is on
#define NUM_LOCK_ON         0x0020    // the numeric lock is on
#define CAPS_LOCK_ON        0x0040    // the capitals lock is on
#define INSERT_MODE         0x0080    // insert or overlay mode
#define CTRL_LEFT           0x0100    // the left control key is pressed
#define ALT_LEFT            0x0200    // the left alternate key is pressed
#define CTRL_RIGHT          0x0400    // the right control key is pressed
#define ALT_RIGHT           0x0800    // the right alternate key is pressed
#define SCROLL_LOCK_DOWN    0x1000    // the scroll lock key is pressed
#define NUM_LOCK_DOWN       0x2000    // the numeric lock is pressed
#define CAPS_LOCK_DOWN      0x4000    // the captials lock key is pressed
#define SYS_REQ_DOWN        0x8000    // the system request key is pressed

// these are the scan codes for the keys on the keyboard, note they are all
// from 0-127 and hence are the "make" scan codes, 128-255 are for the break
// scan codes and are computed simply by adding 128 to each of the make codes

#define MAKE_ESC              1
#define MAKE_1                2
#define MAKE_2                3
#define MAKE_3                4
#define MAKE_4                5
#define MAKE_5                6
#define MAKE_6                7
#define MAKE_7                8
#define MAKE_8                9
#define MAKE_9                10
#define MAKE_0                11
#define MAKE_MINUS            12
#define MAKE_EQUALS           13
#define MAKE_BKSP             14
#define MAKE_TAB              15
#define MAKE_Q                16
#define MAKE_W                17
#define MAKE_E                18
#define MAKE_R                19
#define MAKE_T                20
#define MAKE_Y                21
#define MAKE_U                22
#define MAKE_I                23
#define MAKE_O                24
#define MAKE_P                25
#define MAKE_LFT_BRACKET      26
#define MAKE_RGT_BRACKET      27
#define MAKE_ENTER            28
#define MAKE_CTRL             29
#define MAKE_A                30
#define MAKE_S                31
#define MAKE_D                32
#define MAKE_F                33
#define MAKE_G                34
#define MAKE_H                35
#define MAKE_J                36
#define MAKE_K                37
#define MAKE_L                38
#define MAKE_SEMI             39
#define MAKE_APOS             40
#define MAKE_TILDE            41
#define MAKE_LEFT_SHIFT       42
#define MAKE_BACK_SLASH       43
#define MAKE_Z                44
#define MAK 8
#define MAKE_8                9
#define MAKE_9                10
#define MAKE_0                11
#define MAKE_MINUS            12
#define MAKE_EQUALS           13
#define MAKE_BKSP             14
#define MAKE_TAB              15
#define MAKE_Q                16
#define MAKE_W                17
#define MAKE_E                18
#define MAKE_R                19
#define MAKE_T                20
#define MAKE_Y                21
#define MAKE_U                22
#define MAKE_I                23
#define MAKE_O                24
#define MAKE_P                25
#define MAKE_LFT_BRACKET      26
#define MAKE_RGT_BRACKET      27
#define MAKE_ENTER            28
#define MAKE_CTRL             29
#define MAKE_A                30
#define MAKE_S                31
#define MAKE_D                32
#define MAKE_F                33
#define MAKE_G                34
#define MAKE_H                35
#define MAKE_J                36
#define MAKE_K                37
#define MAKE_L                38
#define MAKE_SEMI             39
#define MAKE_APOS             40
#define MAKE_TILDE            41
#define MAKE_LEFT_SHIFT       42
#define MAKE_BACK_SLASH       43
#define MAKE_Z                44
#define MAKQaC RlVSA% TL8U! V,W
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now