Low Level Keyboard Handler Functions with Test Examples
Last update: April 8, 2000

By Gary Neal, Jr. -- garyneal@mailcity.com
http://www.geocities.com/SiliconValley/Park/7113/

Disclaimer:
I cannot claim responsibility for any adverse affects that may occur on your
computer for using this code.  Use it at your own risk.

/******************************************************************************/

These functions allow you to implement a low level keyboard handler in your
gaming programs.  You can have the low level keyboard handler chain the
BIOS's handler and/or you can specify a call back function that will be called
from within the keyboard handler.

Check the test example programs to see how these can be implemented and
check back at my web site often for any updates that may be available.

/******************************************************************************/

void InstallKeyboardHandler(bool chainBios, void (far *func)(void));
    Installs the low level keyboard handler which will detect key presses
    and update its internal tables accordingly.

    chainBios -- If set to 'true,' the low level keyboard handler will
    chain itself with the BIOS's keyboard handler.  This will enable the
    BIOS to process the keys normally as well as provide low level keyboard
    services.  Set to 'false' and the low level keyboard handler will have
    exclusive control of the keyboard.

    func -- Specifies a call back function that will be called by the low
    level keyboard handler.  This call back function will be called in an
    interrupt context so it must not make any calls to DOS or take to much
    time to finish.  Set to NULL, if you do not wish to use a call back
    function.  Call back functions can read port 0x60 for the current scan
    code sent from the keyboard since the low level keyboard handler will
    not clear the keyboard until after the call back function returns.

    This function may be called multiple times without having to call the
    'RemoveKeyboardHandler' function first each time.  This can be useful
    for changing options as far as BIOS chaining and re-specifying a new
    (or no) call back function.

void RemoveKeyboardHandler(void);
    Removes the low level keyboard handler and restores full control to BIOS.

    This function call will be ignored if the low level keyboard handler is
    not already installed.

int KeyPressed(int c);
int KeyPressedNow(int c);
    Returns the state of a key specified by its scan code set in c.
    (See KEYBOARD.H for some useful constants that can be used.)

    The return value is zero, if the key is currently not pressed.
    If the key is pressed, the return value is non-zero.

    If a key is pressed, you can determine if it was a standard key or
    an extended key by checking the bit information of the return value.
    For example, you can check to see which ENTER key was pressed as so:

        int key_state = KeyPressed(KEY_ENTER);
        if (key_state == 0x81)
            puts("Both ENTER keys are pressed.");
        else if (key_state & 0x80)
            puts("The ENTER key on the numeric keypad is pressed.");
        else if (key_state & 0x01)
            puts("The ENTER key on the standard keyboard is pressed.");
        else
            puts("The ENTER key is not pressed.");

    The 'KeyPressed' function returns the state of the key since the last
    call to either 'KeyPressed' or 'KeyPressedNow.'

    The 'KeyPressedNow' function returns the absolute state of the key.

bool KeyHardAbort(void);
    This will return 'true' if the user pressed either CTRL-ALT-DEL or
    CTRL-ALT-ESC while the low level keyboard handler was installed.

    When these key combinations are detected by the low level keyboard
    handler, it will:

    1) Remove itself (as if your program called 'RemoveKeyboardHandler').
    2) Restore BIOS's handler.
    3) Set the hard abort flag to true.

    The low level keyboard handler must be reinstalled in order to clear
    the hard abort flag.

    (CTRL-ALT-ESC is reserved because multitasking OS's will detect and
    redirect the CTRL-ALT-DEL sequence.)
