11.1 Constants, Type and variables

Constants

The following constants define some error constants, which may be returned by the keyboard functions.

 errKbdBase           = 1010;
 errKbdInitError      = errKbdBase + 0;
 errKbdNotImplemented = errKbdBase + 1;
The following constants denote special keyboard keys. The first constants denote the function keys:
 const
   kbdF1        = $FF01;
   kbdF2        = $FF02;
   kbdF3        = $FF03;
   kbdF4        = $FF04;
   kbdF5        = $FF05;
   kbdF6        = $FF06;
   kbdF7        = $FF07;
   kbdF8        = $FF08;
   kbdF9        = $FF09;
   kbdF10       = $FF0A;
   kbdF11       = $FF0B;
   kbdF12       = $FF0C;
   kbdF13       = $FF0D;
   kbdF14       = $FF0E;
   kbdF15       = $FF0F;
   kbdF16       = $FF10;
   kbdF17       = $FF11;
   kbdF18       = $FF12;
   kbdF19       = $FF13;
   kbdF20       = $FF14;
Constants $15 till $1F are reserved for future function keys. The following constants denote the cursor movement keys:
   kbdHome      = $FF20;
   kbdUp        = $FF21;
   kbdPgUp      = $FF22;
   kbdLeft      = $FF23;
   kbdMiddle    = $FF24;
   kbdRight     = $FF25;
   kbdEnd       = $FF26;
   kbdDown      = $FF27;
   kbdPgDn      = $FF28;
 
   kbdInsert    = $FF29;
   kbdDelete    = $FF2A;
Constants $2B till $2F are reserved for future keypad keys. The following flags are also defined:
   kbASCII       = $00;
   kbUniCode     = $01;
   kbFnKey       = $02;
   kbPhys        = $03;
   kbReleased    = $04;
They can be used to check what kind of data a key event contains. The following shift-state flags can be used to determine the shift state of a key (i.e. which of the SHIFT, ALT and CTRL keys were pressed simultaneously with a key):
   kbLeftShift   = 1;
   kbRightShift  = 2;
   kbShift       = kbLeftShift or kbRightShift;
   kbCtrl        = 4;
   kbAlt         = 8;
The following constant strings are used in the key name functions FunctionKeyName (294) and KeyEventToString (300):
 SShift       : Array [1..3] of string[5] = ('SHIFT','CTRL','ALT');
 LeftRight   : Array [1..2] of string[5] = ('LEFT','RIGHT');
 UnicodeChar : String = 'Unicode character ';
 SScanCode    : String = 'Key with scancode ';
 SUnknownFunctionKey : String = 'Unknown function key : ';
 SAnd         : String = 'AND';
 SKeyPad      : Array [0..($FF2F-kbdHome)] of string[6] =
                ('Home','Up','PgUp','Left',
                 'Middle','Right','End','Down',
                 'PgDn','Insert','Delete','',
                 '','','','');
They can be changed to localize the key names when needed.

Types

The TKeyEvent type is the base type for all keyboard events:
   TKeyEvent = Longint;
The key stroke is encoded in the 4 bytes of the TKeyEvent type. The various fields of the key stroke encoding can be obtained by typecasting the TKeyEvent type to the TKeyRecord type:
   TKeyRecord = packed record
     KeyCode : Word;
     ShiftState, Flags : Byte;
   end;
The structure of a TKeyRecord structure is explained in table (11.1).

Table 11.1: Structure of TKeyRecord
Field Meaning


KeyCode Depending on flags either the physical representation of a key (under DOS scancode, ascii code pair), or the translated ASCII/unicode character.
ShiftState Shift-state when this key was pressed (or shortly after)
Flags Determine how to interpret KeyCode



The shift-state can be checked using the various shift-state constants, and the flags in the last byte can be checked using one of the kbASCII, kbUniCode, kbFnKey, kbPhys, kbReleased constants. If there are two keys returning the same char-code, there’s no way to find out which one was pressed (Gray+ and Simple+). If it needs to be known which was pressed, the untranslated keycodes must be used, but these are system dependent. System dependent constants may be defined to cover those, with possibily having the same name (but different value).

The TKeyboardDriver record can be used to install a custom keyboard driver with the SetKeyboardDriver (303) function:

 Type
   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 various fields correspond to the different functions of the keyboard unit interface. For more information about this record see section 11.4, page 313