keybrdh.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. const
  11. { We have an errorcode base of 1010 }
  12. errKbdBase = 1010;
  13. errKbdInitError = errKbdBase + 0;
  14. errKbdNotImplemented = errKbdBase + 1;
  15. type
  16. TKeyEvent = Cardinal;
  17. TKeyRecord = packed record
  18. KeyCode : Word;
  19. ShiftState, Flags : Byte;
  20. end;
  21. { The structure of a TKeyEvent follows in LSB-MSB order:
  22. 2 bytes: depending on flags either the physical representation of a key
  23. (under DOS scancode, ascii code pair), or the translated
  24. ASCII/unicode character
  25. 1 byte: shift-state when this key was pressed (or shortly after)
  26. 1 byte: flags, the following flags are defined:
  27. bit0-1
  28. 0: the lowest two bytes is the translated ASCII value
  29. 1: the lowest two bytes is the translated Unicode value
  30. (wide-char)
  31. 2: the lowest two bytes is a function key, and the lowest
  32. two bytes contains its platform independent code
  33. 3: the lowest two bytes is the physical representation
  34. bit2
  35. 0: the key is pressed
  36. 1: the key is released (This event is not guaranteed to occur on all platforms)
  37. bit3-7 undefined, should be 0
  38. If there are two keys returning the same char-code, there's no way to find
  39. out which one was pressed (Gray+ and Simple+). If you need to know which
  40. was pressed, you'll need to use the untranslated keycodes, which is system
  41. dependent. System dependent constants may be defined to cover those, with
  42. possibily having the same name (but different value). }
  43. { System independent function key codes }
  44. const
  45. kbdF1 = $FF01;
  46. kbdF2 = $FF02;
  47. kbdF3 = $FF03;
  48. kbdF4 = $FF04;
  49. kbdF5 = $FF05;
  50. kbdF6 = $FF06;
  51. kbdF7 = $FF07;
  52. kbdF8 = $FF08;
  53. kbdF9 = $FF09;
  54. kbdF10 = $FF0A;
  55. kbdF11 = $FF0B;
  56. kbdF12 = $FF0C;
  57. kbdF13 = $FF0D;
  58. kbdF14 = $FF0E;
  59. kbdF15 = $FF0F;
  60. kbdF16 = $FF10;
  61. kbdF17 = $FF11;
  62. kbdF18 = $FF12;
  63. kbdF19 = $FF13;
  64. kbdF20 = $FF14;
  65. kbdLWin = $FF15;
  66. kbdRWin = $FF16;
  67. kbdApps = $FF17;
  68. { $15 - $1F reserved for future Fxx keys }
  69. kbdHome = $FF20;
  70. kbdUp = $FF21;
  71. kbdPgUp = $FF22;
  72. kbdLeft = $FF23;
  73. kbdMiddle = $FF24;
  74. kbdRight = $FF25;
  75. kbdEnd = $FF26;
  76. kbdDown = $FF27;
  77. kbdPgDn = $FF28;
  78. kbdInsert = $FF29;
  79. kbdDelete = $FF2A;
  80. { $2B - $2F reserved for future keypad keys }
  81. { possible flag values }
  82. kbASCII = $00;
  83. kbUniCode = $01;
  84. kbFnKey = $02;
  85. kbPhys = $03;
  86. kbReleased = $04;
  87. { shiftstate flags }
  88. kbLeftShift = 1;
  89. kbRightShift = 2;
  90. kbShift = kbLeftShift or kbRightShift;
  91. kbCtrl = 4;
  92. kbAlt = 8;
  93. { ---------------------------------------------------------------------
  94. Key names. Can be localized if needed.
  95. ---------------------------------------------------------------------}
  96. SShift : Array [1..3] of string[5] = ('SHIFT','CTRL','ALT');
  97. SLeftRight : Array [1..2] of string[5] = ('LEFT','RIGHT');
  98. SUnicodeChar : String = 'Unicode character ';
  99. SScanCode : String = 'Key with scancode ';
  100. SUnknownFunctionKey : String = 'Unknown function key : ';
  101. SAnd : String = 'AND';
  102. SKeyPad : Array [0..($FF2F-kbdHome)] of string[6] =
  103. ('Home','Up','PgUp','Left',
  104. 'Middle','Right','End','Down',
  105. 'PgDn','Insert','Delete','',
  106. '','','','');
  107. Type
  108. TKeyboardDriver = Record
  109. InitDriver : Procedure;
  110. DoneDriver : Procedure;
  111. GetKeyEvent : Function : TKeyEvent;
  112. PollKeyEvent : Function : TKeyEvent;
  113. GetShiftState : Function : Byte;
  114. TranslateKeyEvent : Function (KeyEvent: TKeyEvent): TKeyEvent;
  115. TranslateKeyEventUniCode : Function (KeyEvent: TKeyEvent): TKeyEvent;
  116. end;
  117. procedure InitKeyboard;
  118. { Initializes the keyboard interface, additional platform specific parameters
  119. can be passed by global variables (RawMode etc.) for the first implementation
  120. under DOS it does nothing }
  121. procedure DoneKeyboard;
  122. { Deinitializes the keyboard interface }
  123. function GetKeyEvent: TKeyEvent;
  124. { Returns the last keyevent, and waits for one if not available }
  125. procedure PutKeyEvent(KeyEvent: TKeyEvent);
  126. { Adds the given KeyEvent to the input queue. Please note that depending on
  127. the implementation this can hold only one value (NO FIFOs etc) }
  128. function PollKeyEvent: TKeyEvent;
  129. { Checks if a keyevent is available, and returns it if one is found. If no
  130. event is pending, it returns 0 }
  131. function PollShiftStateEvent: TKeyEvent;
  132. { Return the current shiftstate in a keyevent }
  133. function TranslateKeyEvent(KeyEvent: TKeyEvent): TKeyEvent;
  134. { Performs ASCII translation of the KeyEvent }
  135. function TranslateKeyEventUniCode(KeyEvent: TKeyEvent): TKeyEvent;
  136. { Performs Unicode translation of the KeyEvent }
  137. function GetKeyEventFlags(KeyEvent: TKeyEvent): Byte;
  138. { Returns the flags part of the given KeyEvent }
  139. function GetKeyEventChar(KeyEvent: TKeyEvent): Char;
  140. { Returns the charcode part of the given KeyEvent, if it contains a translated
  141. keycode }
  142. function GetKeyEventUniCode(KeyEvent: TKeyEvent): Word;
  143. { Returns the unicode part of the given KeyEvent, if it contains a translated
  144. unicode character }
  145. function GetKeyEventCode(KeyEvent: TKeyEvent): Word;
  146. { Returns the translated function keycode part of the given KeyEvent, if it
  147. contains a translated function keycode }
  148. function GetKeyEventShiftState(KeyEvent: TKeyEvent): Byte;
  149. { Returns the shift-state values of the given KeyEvent }
  150. function IsFunctionKey(KeyEvent: TKeyEvent): Boolean;
  151. { Returns true if the given key was a function key or not }
  152. Function SetKeyboardDriver (Const Driver : TKeyboardDriver) : Boolean;
  153. { Sets the keyboard driver to use }
  154. Procedure GetKeyboardDriver (Var Driver : TKeyboardDriver);
  155. { Returns the currently active keyboard driver }
  156. Function ShiftStateToString(KeyEvent : TKeyEvent; UseLeftRight : Boolean) : String;
  157. { Returns a string representation of a shift state as returned by
  158. pollshiftstate }
  159. Function FunctionKeyName (KeyCode : Word) : String;
  160. { Returns the name of a function key if the key is one of the special keys . }
  161. Function KeyEventToString(KeyEvent : TKeyEvent) : String;
  162. { Returns a string representation of the pressed key }