Dec 082017
 
Low-level keyboard handler. Tests for key combinations that other routines will not handle. Libraries for C.
File KEYLIB11.ZIP from The Programmer’s Corner in
Category C Source Code
Low-level keyboard handler. Tests for key combinations that other routines will not handle. Libraries for C.
File Name File Size Zip Size Zip Type
KEY.DOC 8939 3178 deflated
KEY.H 3212 769 deflated
KEYC.LIB 3072 1242 deflated
KEYL.LIB 3584 1306 deflated
KEYM.LIB 3584 1306 deflated
KEYS.LIB 3072 1241 deflated
KEYT.LIB 3072 1243 deflated
KEYTEST.C 1030 358 deflated
KEYTEST.COM 6816 4598 deflated

Download File KEYLIB11.ZIP Here

Contents of the KEY.DOC file


/****************************************************************************\

Selective Keyboard Handler

Copyright (c) 1994 Douglas Peterson

\****************************************************************************/


This is a set of library routines for getting keystrokes from the
user. It is written in Assembly language for use with C/C++. It doesn't
make use of any C runtime library routines and SHOULD be compatible with
any ANSI C compiler.

I've seen quite a few low level keyboard handlers in my time, but have
never been fully satisfied with them. I wanted a handler that functioned
similar to BIOS, but 1) Didn't use a buffer (kept only the last pressed key),
2) Blocked out BIOS interrupts like CTRL-C, CTRL-ALT-DELETE, PRINT SCREEN,
etc.., and 3) Keys weren't limited to those "scan-codes" accepted by BIOS
like CTRL-ALT-SHIFT-ENTER (You really can test for this with this handler!).

So here it is..


This library is hereby released into the public domain and is completely
free of charges. I cannot and will not be held liable for any problems that
might occur with the coincidental use of this code. Use at your own risk!


NOTICE: This is a low level keyboard handler. It takes over interrupt
vector 09h and only releases control back to the original owner when
key_init(RESTORE) is called. Make sure you do this BEFORE your program
terminates and if you spawn a child process.


/****************************************************************************\

Revision List

\****************************************************************************/

Version 1.00 - First release.
1.01 - Added StateTable so that the handler can mimic
the usual sort of low level handler.
Function key_test() altered and it's original
function is mimicked by key_ready().



/****************************************************************************\


\****************************************************************************/

All of the low level key handlers I've seen just let you test to see
if a particular key is currently pressed or released. This one is
different. It has only a 1 word buffer that stores a special scan code
representing the last combination of key presses. It can be cleared
manually and is cleared after fetching a key with key_wait() or key_check().

As of version 1.01, you can test key presses and releases with the
key_test() function. key_test()'s original usage has been replaced by
key_ready().


The public symbol for the buffer is KeyBuf (with an underscore), but
since it is altered by the ISR, direct manipulation is not recommended.
It is possible that it's value could change while you are messing with it.

For example:

if (KeyBuf==CR) {
<------------- The ISR could change KeyBuf right here
key=KeyBuf;
...
}


The structure of the buffer is:

151413121110 9 8 7 6 5 4 3 2 1 0 <- Bit position
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
| | | |_____________|
| | | |________ Scan code of key
| | |_________________ SHIFT flag
| |___________________ ALT flag
|_____________________ CTRL flag

Bits 11-15 are unused.

There are macros defined for every possible key. Feel free to alter
them at will as some might consider the '-' character a "DASH" and some
might better like "MINUS". There are a few special definitions, they
are:

KP_ASTERISK - Key pad asterisk (*)
KP_DASH - Key pad dash/minus (-)
KP_PLUS - Key pad plus (+)
KP_5 - Key pad five key (5)

You can build on a definition in your code like:

Key Combination C Code Example
------------------ ----------------------------------------------------
CTRL-ALT-4 if (key==CTRL+ALT+_4)

SHIFT-F case SHIFT+_F:

SHIFT-CTRL-ALT-ESC while (key_wait()!=SHIFT+CTRL+ALT+ESC) ;

ALT-F1 case ALT+F1:

ENTER or
SHIFT-ENTER or
CTRL-ENTER or
ALT-ENTER if ((char)key==CR)

Q or
SHIFT-Q if ((key&0x06FF)==_Q) NOTE: we are masking out the shift flag bit

This can be especially usefull in case statements because you can test
for the 'I' key by switching only the first 8 bits (switch ((char)key) {)
and you don't have to convert to upper case or check the ALT key.


Lighted keys (CAPSLOCK,SCROLLLOCK,and NUMLOCK) return scan codes just
like any other key, but the lights are unaffected and meaningless.

Interrupt accessing keys like Print Screen, and Pause appear to return
different codes depending on BIOS/keyboard. They will return them like
any other key, but I'm not sure that they are accurate.


One very odd thing I have found so far (at least on my keyboard) is that
SHIFT-INS (or HOME,END,DEL,PGUP,PGDN) on the gray keys gets returned without
the SHIFT flag set. On the number pad this is not the case (7,9,1,3,0,. are
considered as HOME,PGUP,END,PGDN,INS,DEL respectively). I cannot find any
explination for this at all.


/****************************************************************************\

KeyASCIITable[]

\****************************************************************************/

char KeyASCIITable[128] is a character buffer that can be used to index
a scan code with an ASCII character. For instance the _A key (code 30)
would be returned (KeyASCIITable[30]) as 'A'.

This table is limited to only the basic keys as there is no ASCII
character for the F1 key. Most will return 0. Referencing this table
will cost you 128 bytes in your code and because it's fairly useless,
don't use it unless you need to.



/****************************************************************************\

VERSION INFO

\****************************************************************************/

If you have a librarian like TLIB (Borland) or LIB (Microsoft) you can
determine the version of this library by generating a .LST (list) file
and looking at the module VERSION. VERSION has just one public symbol
which will be called _Version_X_xx, where X is the major and xx is the
minor version numbers.



/****************************************************************************\

FUNCTION DESCRIPTIONS

\****************************************************************************/

----------------------------------------
void key_init(int state)
----------------------------------------

This function invokes and removes the handler. You can initialize
and remove it as many times as you like. If it is already installed,
calling key_init() with 1 will have no effect and vice-versa.

Remember to remove the handler before your program exits!!

PARAMETERS:

state - 0 (RESTORE) to remove the handler
1 (INIT or any non-zero number) to install the handler


----------------------------------------
void key_clear(void)
----------------------------------------

This macro simply sets the buffer to zero.


----------------------------------------
int key_check(void)
----------------------------------------

This function checks the buffer for a waiting key. It will 'fetch'
the key if one exists.

RETURNS:

0 - If no key available -or-
Scan-code/Flag of key waiting


----------------------------------------
int key_ready(void)
----------------------------------------

This function tests the buffer for a waiting key. It does NOT
'fetch' the character.

RETURNS:

0 - If no key available -or-
1 - If key IS available


----------------------------------------
int key_test(int keycode)
----------------------------------------

This function allows you to test the press and release status of
the keys just like the usual brand of key handler.

PARAMETERS:

keycode - Key Scan Code to test

RETURNS:

0 - If key is currently released
1 - If key is currently pressed


----------------------------------------
int key_wait(void)
----------------------------------------

This function clears the buffer and waits until a key is pressed
before returning.

RETURNS:

Scan-code/Flag of key pressed



/****************************************************************************\

\****************************************************************************/

If you have any questions or comments I can be reached:

Douglas Peterson

CompuServe 73014,3320

-or-

Dust Devil BBS (702) 796-7134

-or-

P.O. Box 26331
San Diego, CA 92196


 December 8, 2017  Add comments

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)