Writing a keyboard driver means that hooks must be created for most of the keyboard
unit functions. The TKeyBoardDriver record contains a field for each of the possible
hooks:
TKeyboardDriver = Record
InitDriver : Procedure;
DoneDriver : Procedure;
GetKeyEvent : Function : TKeyEvent;
PollKeyEvent : Function : TKeyEvent;
GetShiftState : Function : Byte;
TranslateKeyEvent : Function (KeyEvent: TKeyEvent): TKeyEvent;
TranslateKeyEventUniCode: Function (KeyEvent: TKeyEvent): TKeyEvent;
end;
|
The meaning of these hooks is explained below:
-
InitDriver
- Called to initialize and enable the driver. Guaranteed to be called only once.
This should initialize all needed things for the driver.
-
DoneDriver
- Called to disable and clean up the driver. Guaranteed to be called after a call
to initDriver. This should clean up all things initialized by InitDriver.
-
GetKeyEvent
- Called by GetKeyEvent (295). Must wait for and return the next key event.
It should NOT store keys.
-
PollKeyEvent
- Called by PollKeyEvent (301). It must return the next key event if there is
one. Should not store keys.
-
GetShiftState
- Called by PollShiftStateEvent (301). Must return the current shift state.
-
TranslateKeyEvent
- Should translate a raw key event to a cOrrect key event, i.e. should
fill in the shiftstate and convert function key scancodes to function key keycodes. If
the TranslateKeyEvent is not filled in, a default translation function will be called
which converts the known scancodes from the tables in the previous section to a correct
keyevent.
-
TranslateKeyEventUniCode
- Should translate a key event to a unicode key
representation.
Strictly speaking, only the GetKeyEvent and PollKeyEvent hooks must be implemented for the
driver to function correctly.
The following unit demonstrates how a keyboard driver can be installed. It takes the installed
driver, and hooks into the GetKeyEvent function to register and log the key events in a file. This
driver can work on top of any other driver, as long as it is inserted in the uses clause after the real
driver unit, and the real driver unit should set the driver record in its initialization
section.
Listing: kbdex/logkeys.pp
The following program demonstrates the use of the unit:
Listing: kbdex/ex9.pp
Note that with a simple extension of this unit could be used to make a driver that is capable of
recording and storing a set of keyboard strokes, and replaying them at a later time, so a ’keyboard
macro’ capable driver. This driver could sit on top of any other driver.