keybrdh.inc 6.9 KB

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