Hi DJGPP users! Here's a piece of code illustrating my approach to
keyboard interrupt handling under DJGPP v2.00. This code is freeware
and you are allowed to do whatever you like with it. I take no 
responsibility for any loss or damage caused by this code, though.

My goal was not to write a useful keyboard library, but to show how to
install a protected mode hardware interrupt handler, lock its code and
data, give it access to program's data segment, and chain to previous 
handler procedure. The handler itself only keeps track of what keys are 
down for the moment, it does not do any keystroke buffering or such. It 
is up to you to write a handler that would satisfy your needs. It is
advised to have the DPMI specification near you when studying the code.

There are 2 variables and 5 functions declared in `keyboard.h':

volatile char keyboard_map[128]; -- an array of byte flags indicating
   whether the key with an appropriate scan code is down. The `volatile'
   attribute prevents GCC optimizer from assuming, that elements of the
   array are always of the same value, since it will not ever see any
   code changing them.

int keyboard_chain_flag; -- an integer flag, indicating whether the 
   handler should or should not chain to the default keyboard interrupt
   procedure. If it is cleared, you have keyboard all for yourself: 
   forget ctrl-c, ctrl-break and even ctrl-alt-del.
   
int keyboard_init(); -- initialization routine. Performs all necessary 
   steps and returns zero on success or -1 on failure.
   
void keyboard_close(); -- termination routine. Shuts the handler down.

int keyboard_key_down(int scan); -- returns 1 if the key with a given
   scan code is down, otherwise returns zero. Scan code constants are
   defined in `keyboard.h'.
   
void keyboard_chain(); -- sets the keyboard_chain_flag. You can use
   functions like getkey(), kbhit(), scanf() etc. whet it is set.
   
void keyboard_unchain(); -- clears the keyboard_chain_flag(). Now only
   information you can get about keyboard is through calls to
   keyboard_key_down().

You could check keyboard_map or update keyboard_chain_flag directly 
instead of calling the functions, but the latter way is more ellegant,
and the functions are defined inline anyway.

NOTE: if Shift, Alt or Ctrl keys are down when keyboard_unchain() is 
called, BIOS will think they still are after you .._unchain(). So it 
is up to you to update the BIOS variables at 0x417 and 0x418. See info 
on _bios_keybrd() from the C library for details.

I would like to thank the whole DJGPP development team for bringing us
such a great programming environment. What else could one wish... I also
thank all the people who helped me to test this code and track down 
multiple bugs. God bless you all, go write something really cool!..

Best regards,
Martynas Kunigelis

P.S. until June 1996 you can e-mail me at Martynas.Kunigelis@vm.ktu.lt;
     Later just write to the DJGPP mailing list at djgpp@delorie.com: I'll
     be there.
