From: 890491g@dragon.acadiau.ca (Don Graves)
Subject: Re: Read any key from the keyboard?
Organization: Acadia University
Date: Wed, 2 Sep 1992 03:23:47 GMT

chris@castlab.engr.wisc.edu (Christian Rohrmeier) writes:

>In article <1992Sep1.123402.160@csghsg5a.bitnet> 89612048s@csghsg5a.bitnet writes:
>>How to read any key from the keyboard?
>>
>>Can someone give me an example how to read any key pressed on the
>>keyboard. That means also the ALT SHIFT CONTROL and combinations
>>of these keys.
>>
>>And also, can you help me about the Num, Scrol and Capslock keys.
>>How to turn them on and off, and how to get there current status?
>>
>>I want to say thank you very much for your help.
>>
>>Yours Markus



	I have a little unit that will answer most of this for you:

{---------- start here ------------}

{--------------------------------------------------------------------------}
Unit KeyBoard;
{--------------------------------------------------------------------------}
{ By Don Graves (Acadia University, Wolfville, N.S., Canada B1A 3N3 }
{ don.graves@dragon.acadiau.ca                                      }
{--------------------------------------------------------------------------}
Interface
{--------------------------------------------------------------------------}
Uses Dos;
{--------------------------------------------------------------------------}
Const
{ Scan code values }
{ list may not be TOTALLY complete and MAYBE there are errors }
    K_F1=59;   { F1 Key }
    K_F2=60;
    K_F3=61;
    K_F4=62;
    K_F5=63;
    K_F6=64;
    K_F7=65;
    K_F8=66;
    K_F9=67;
    K_F10=68;
    K_F11=137;
    K_F12=138;

    K_HOME=71;  { HOME key }
    K_END=79;
    K_PGUP=73;
    K_PGDN=81;
    K_INS=82;
    K_DEL=83;
    K_LEFT=75;
    K_RIGHT=77;
    K_UP=72;
    K_DOWN=80;

    K_L_ENTER=28;
    K_R_ENTER=224;
    K_TAB=15;

    K_ESC=1;
    K_BACKSPACE=14;
    K_SPACE=57;

    CTRL_HOME=119;
    CTRL_END=117;
    CTRL_PGUP=132;
    CTRL_PGDN=118;
    CTRL_INS=146;
    CTRL_DEL=147;
    CTRL_LEFT=115;
    CTRL_RIGHT=116;
    CTRL_UP=141;
    CTRL_DOWN=145;
    CTRL_TAB=148;

    ALT_INS=162;
    ALT_DEL=163;
    ALT_HOME=151;
    ALT_END=159;
    ALT_PGUP=153;
    ALT_PGDN=161;
    ALT_UP=152;
    ALT_DOWN=160;
    ALT_RIGHT=157;
    ALT_LEFT=155;

    K_Q=16;       { scan codes for letter KEYS (not ascii codes) }
    K_W=17;
    K_E=18;
    K_R=19;
    K_T=20;
    K_Y=21;
    K_U=22;
    K_I=23;
    K_O=24;
    K_P=25;

    K_A=30;
    K_S=31;
    K_D=32;
    K_F=33;
    K_G=34;
    K_H=35;
    K_J=36;
    K_K=37;
    K_L=38;

    K_Z=44;
    K_X=45;
    K_C=46;
    K_V=47;
    K_B=48;
    K_N=49;
    K_M=50;

{--------------------------------------------------------------------------}
Function KeyScan: Integer;
    { This procedure waits for a key to be pressed (if a keypress is       }
    { already available in the keyboard buffer, then it returns that press }
    { instead of waiting for one).                                         }
{--------------------------------------------------------------------------}
Function KeyAvailable: Boolean;
    { Returns TRUE if a keypress is waiting in the keyboard buffer and     }
    { FALSE otherwise.                                                     }
{--------------------------------------------------------------------------}
Procedure FlushKeyBuffer;
    { This procedure flushes the keyboard buffer.                          }
{--------------------------------------------------------------------------}
Function NumLock: Boolean;
    { It returns TRUE if the flag is ON, FALSE otherwise.                  }
{--------------------------------------------------------------------------}
Function CapsLock: Boolean;
    { It returns TRUE if the flag is ON, FALSE otherwise.                  }
{--------------------------------------------------------------------------}
Function ScrollLock: Boolean;
    { It returns TRUE if the flag is ON, FALSE otherwise.                  }
{--------------------------------------------------------------------------}
Function InsertMode: Boolean;
    { It returns TRUE if the flag is ON, FALSE otherwise.                  }
{--------------------------------------------------------------------------}
Function CtrlKey: Boolean;
    { It returns TRUE if a CTRL key is pressed and FALSE                   }
    { otherwise.                                                           }
{--------------------------------------------------------------------------}
Function AltKey: Boolean;
    { It returns TRUE if an ALT key is pressed and FALSE                   }
    { otherwise.                                                           }
{--------------------------------------------------------------------------}
Function LeftShift: Boolean;
    { It returns TRUE if the left SHIFT key is pressed                     }
    { and FALSE otherwise.                                                 }
{--------------------------------------------------------------------------}
Function RightShift: Boolean;
    { It returns TRUE if the right SHIFT key is pressed                    }
    { and FALSE otherwise.                                                 }
{--------------------------------------------------------------------------}
Procedure KeyboardRate (delay_value, repeat_rate: Integer);
    { delay value (00h=250ms to 03h=1000ms)                                }
    { repeat rate (00h=30/sec to 0Ch=10/sec [def] to                       }
    { 1Fh=2/sec)                                                           }
{--------------------------------------------------------------------------}
Implementation
{--------------------------------------------------------------------------}
Const
    ins_mode    = $80;
    caps_lock   = $40;
    num_lock    = $20;
    scroll_lock = $10;
    alt_key     = $08;
    ctrl_key    = $04;
    left_shift  = $02;
    right_shift = $01;
    kybd_port   = $60;
{---------------------------------------------------------------------------}
Var r: Registers;
{---------------------------------------------------------------------------}
Procedure KeyboardRate (delay_value, repeat_rate: Integer);
Begin
    r.ah := $03;
    r.al := $05;
    r.bh := delay_value;
    r.bl := repeat_rate;
    Intr ($16, r);
End;
{--------------------------------------------------------------------------}
Procedure FlushKeyBuffer;
Var head : word absolute $40:$1A;
    tail : word absolute $40:$1C;
Begin
    tail := head;
End;
{--------------------------------------------------------------------------}
Function KeyAvailable: Boolean;
Var head : word absolute $40:$1A;
    tail : word absolute $40:$1C;
Begin
    KeyAvailable := (NOT (head = tail));
End;
{--------------------------------------------------------------------------}
Function KeyScan: Integer;
Begin
    r.ah := $10;
    Intr ($16, r);
    KeyScan := r.ah
End;
{--------------------------------------------------------------------------}
Function NumLock: Boolean;
Begin
    r.ah := $12;
    Intr ($16, r);
    r.al := r.al And num_lock;
    If (r.al = num_lock) Then
        NumLock := TRUE
    Else
        NumLock := FALSE;
End;
{--------------------------------------------------------------------------}
Function CapsLock: Boolean;
Begin
    r.ah := $12;
    Intr ($16, r);
    r.al := r.al And caps_lock;
    If (r.al = caps_lock) Then
        CapsLock := TRUE
    Else
        CapsLock := FALSE;
End;
{--------------------------------------------------------------------------}
Function ScrollLock: Boolean;
Begin
    r.ah := $12;
    Intr ($16, r);
    r.al := r.al And scroll_lock;
    If (r.al = scroll_lock) Then
        ScrollLock := TRUE
    Else
        ScrollLock := FALSE;
End;
{--------------------------------------------------------------------------}
Function InsertMode: Boolean;
Begin
    r.ah := $12;
    Intr ($16, r);
    r.al := r.al And ins_mode;
    If (r.al = ins_mode) Then
        InsertMode := TRUE
    Else
        InsertMode := FALSE;
End;
{--------------------------------------------------------------------------}
Function CtrlKey: Boolean;
Begin
    r.ah := $12;
    Intr ($16, r);
    r.al := r.al And ctrl_key;
    If (r.al = ctrl_key) Then
        CtrlKey := TRUE
    Else
        CtrlKey := FALSE;
End;
{--------------------------------------------------------------------------}
Function AltKey: Boolean;
Begin
    r.ah := $12;
    Intr ($16, r);
    r.al := r.al And alt_key;
    If (r.al = alt_key) Then
        AltKey := TRUE
    Else
        AltKey := FALSE;
End;
{--------------------------------------------------------------------------}
Function LeftShift: Boolean;
Begin
    r.ah := $12;
    Intr ($16, r);
    r.al := r.al And left_shift;
    If (r.al = left_shift) Then
        LeftShift := TRUE
    Else
        LeftShift := FALSE;
End;
{--------------------------------------------------------------------------}
Function RightShift: Boolean;
Begin
    r.ah := $12;
    Intr ($16, r);
    r.al := r.al And right_shift;
    If (r.al = right_shift) Then
        RightShift := TRUE
    Else
        RightShift := FALSE;
End;
{--------------------------------------------------------------------------}
End.
{--------------------------------------------------------------------------}

{---------- end here --------------}

You should be able to use this to solve your problem.  To test for shift/ctrl
states, just use
if RightShift then dowhatever;
etc.
None of these procedures change the states of the keyboard flag bits (I didn't
get around to doing this ... yet), but it wouldn't be hard.
Good luck.


-- 
--
Don Graves
Acadia University, Wolfville, N.S. Canada
don.graves@dragon.acadiau.ca || 890491g@ace.acadiau.ca