keyboard.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. {
  2. $Id$
  3. Keyboard unit, part of the portable API for Pascal
  4. Copyright (c) 1997 Balazs Scheidler ([email protected])
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free
  15. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************}
  17. unit Keyboard;
  18. interface
  19. {$i platform.inc}
  20. uses
  21. {$ifdef DEBUG}
  22. {$ifdef win32}
  23. windows,
  24. {$endif win32}
  25. {$endif DEBUG}
  26. ApiComm;
  27. const
  28. { We have an errorcode base of 1010 }
  29. errKbdInitError = errKbdBase + 0;
  30. errKbdNotImplemented = errKbdBase + 1;
  31. type
  32. TKeyEvent = Longint;
  33. { The structure of a TKeyEvent follows in LSB-MSB order:
  34. 2 bytes: depending on flags either the physical representation of a key
  35. (under DOS scancode, ascii code pair), or the translated
  36. ASCII/unicode character
  37. 1 byte: shift-state when this key was pressed (or shortly after)
  38. 1 byte: flags, the following flags are defined:
  39. bit0-1
  40. 0: the lowest two bytes is the translated ASCII value
  41. 1: the lowest two bytes is the translated Unicode value
  42. (wide-char)
  43. 2: the lowest two bytes is a function key, and the lowest
  44. two bytes contains its platform independent code
  45. 3: the lowest two bytes is the physical representation
  46. bit2
  47. 0: the key is pressed
  48. 1: the key is released (This event is not guaranteed to occur on all platforms)
  49. bit3-7 undefined, should be 0
  50. If there are two keys returning the same char-code, there's no way to find
  51. out which one was pressed (Gray+ and Simple+). If you need to know which
  52. was pressed, you'll need to use the untranslated keycodes, which is system
  53. dependent. System dependent constants may be defined to cover those, with
  54. possibily having the same name (but different value). }
  55. { System independent function key codes }
  56. const
  57. kbdF1 = $FF01;
  58. kbdF2 = $FF02;
  59. kbdF3 = $FF03;
  60. kbdF4 = $FF04;
  61. kbdF5 = $FF05;
  62. kbdF6 = $FF06;
  63. kbdF7 = $FF07;
  64. kbdF8 = $FF08;
  65. kbdF9 = $FF09;
  66. kbdF10 = $FF0A;
  67. kbdF11 = $FF0B;
  68. kbdF12 = $FF0C;
  69. kbdF13 = $FF0D;
  70. kbdF14 = $FF0E;
  71. kbdF15 = $FF0F;
  72. kbdF16 = $FF10;
  73. kbdF17 = $FF11;
  74. kbdF18 = $FF12;
  75. kbdF19 = $FF13;
  76. kbdF20 = $FF14;
  77. { $15 - $1F reserved for future Fxx keys }
  78. kbdHome = $FF20;
  79. kbdUp = $FF21;
  80. kbdPgUp = $FF22;
  81. kbdLeft = $FF23;
  82. kbdMiddle = $FF24;
  83. kbdRight = $FF25;
  84. kbdEnd = $FF26;
  85. kbdDown = $FF27;
  86. kbdPgDn = $FF28;
  87. kbdInsert = $FF29;
  88. kbdDelete = $FF2A;
  89. { $2B - $2F reserved for future keypad keys }
  90. { possible flag values }
  91. kbASCII = $00;
  92. kbUniCode = $01;
  93. kbFnKey = $02;
  94. kbPhys = $03;
  95. kbReleased = $04;
  96. { shiftstate flags }
  97. kbLeftShift = 1;
  98. kbRightShift = 2;
  99. kbShift = kbLeftShift or kbRightShift;
  100. kbCtrl = 4;
  101. kbAlt = 8;
  102. var
  103. PendingKeyEvent : TKeyEvent;
  104. procedure InitKeyboard;
  105. { Initializes the keyboard interface, additional platform specific parameters
  106. can be passed by global variables (RawMode etc.) for the first implementation
  107. under DOS it does nothing }
  108. procedure DoneKeyboard;
  109. { Deinitializes the keyboard interface }
  110. function GetKeyEvent: TKeyEvent;
  111. { Returns the last keyevent, and waits for one if not available }
  112. procedure PutKeyEvent(KeyEvent: TKeyEvent);
  113. { Adds the given KeyEvent to the input queue. Please note that depending on
  114. the implementation this can hold only one value (NO FIFOs etc) }
  115. function PollKeyEvent: TKeyEvent;
  116. { Checks if a keyevent is available, and returns it if one is found. If no
  117. event is pending, it returns 0 }
  118. function PollShiftStateEvent: TKeyEvent;
  119. { Return the current shiftstate in a keyevent }
  120. function TranslateKeyEvent(KeyEvent: TKeyEvent): TKeyEvent;
  121. { Performs ASCII translation of the KeyEvent }
  122. function TranslateKeyEventUniCode(KeyEvent: TKeyEvent): TKeyEvent;
  123. { Performs Unicode translation of the KeyEvent }
  124. function GetKeyEventFlags(KeyEvent: TKeyEvent): Byte;
  125. { Returns the flags part of the given KeyEvent }
  126. function GetKeyEventChar(KeyEvent: TKeyEvent): Char;
  127. { Returns the charcode part of the given KeyEvent, if it contains a translated
  128. keycode }
  129. function GetKeyEventUniCode(KeyEvent: TKeyEvent): Word;
  130. { Returns the unicode part of the given KeyEvent, if it contains a translated
  131. unicode character }
  132. function GetKeyEventCode(KeyEvent: TKeyEvent): Word;
  133. { Returns the translated function keycode part of the given KeyEvent, if it
  134. contains a translated function keycode }
  135. function GetKeyEventShiftState(KeyEvent: TKeyEvent): Byte;
  136. { Returns the shift-state values of the given KeyEvent }
  137. function IsFunctionKey(KeyEvent: TKeyEvent): Boolean;
  138. { Returns true if the given key was a function key or not }
  139. {$ifdef DEBUG}
  140. {$ifdef win32}
  141. var last_ir : INPUT_RECORD;
  142. {$endif win32}
  143. {$ifdef Unix}
  144. Function RawReadKey:char;
  145. {$endif unix}
  146. {$endif DEBUG}
  147. implementation
  148. { Include platform dependent routines }
  149. {$i keyboard.inc}
  150. { Platform independent routines }
  151. procedure PutKeyEvent(KeyEvent: TKeyEvent);
  152. begin
  153. PendingKeyEvent := KeyEvent;
  154. end;
  155. function GetKeyEventFlags(KeyEvent: TKeyEvent): Byte;
  156. begin
  157. GetKeyEventFlags := (KeyEvent and $FF000000) shr 24;
  158. end;
  159. function GetKeyEventChar(KeyEvent: TKeyEvent): Char;
  160. begin
  161. if KeyEvent and $03000000 = $00000000 then
  162. GetKeyEventChar := Chr(KeyEvent and $000000FF)
  163. else
  164. GetKeyEventChar := #0;
  165. end;
  166. function GetKeyEventUniCode(KeyEvent: TKeyEvent): Word;
  167. begin
  168. if KeyEvent and $03000000 = $01000000 then
  169. GetKeyEventUniCode := KeyEvent and $0000FFFF
  170. else
  171. GetKeyEventUniCode := 0;
  172. end;
  173. function GetKeyEventCode(KeyEvent: TKeyEvent): Word;
  174. begin
  175. GetKeyEventCode := KeyEvent and $0000FFFF
  176. end;
  177. function GetKeyEventShiftState(KeyEvent: TKeyEvent): Byte;
  178. begin
  179. GetKeyEventShiftState := (KeyEvent and $00FF0000) shr 16;
  180. end;
  181. function IsFunctionKey(KeyEvent: TKeyEvent): Boolean;
  182. begin
  183. IsFunctionKey := KeyEvent and $03000000 = $02000000;
  184. end;
  185. end.
  186. {
  187. $Log$
  188. Revision 1.3 2000-11-13 14:35:57 marco
  189. * Unix Renamefest for defines.
  190. Revision 1.2 2000/10/26 23:10:21 peter
  191. * fixes merge
  192. Revision 1.1 2000/07/13 06:29:38 michael
  193. + Initial import
  194. Revision 1.2 2000/02/29 11:43:16 pierre
  195. Common renamed APIComm to avoid problems with free vision
  196. Revision 1.1 2000/01/06 01:20:31 peter
  197. * moved out of packages/ back to topdir
  198. Revision 1.1 1999/12/23 19:36:47 peter
  199. * place unitfiles in target dirs
  200. Revision 1.2 1999/12/09 21:29:15 pierre
  201. + some debug code for win32
  202. Revision 1.3 1999/11/24 23:36:56 peter
  203. * moved to packages dir
  204. Revision 1.2 1998/12/12 19:12:58 peter
  205. * keyboard updates
  206. * make test target, make all only makes units
  207. Revision 1.1 1998/12/04 12:48:24 peter
  208. * moved some dirs
  209. Revision 1.6 1998/10/29 12:49:47 peter
  210. * more fixes
  211. Revision 1.5 1998/10/28 21:18:22 peter
  212. * more fixes
  213. Revision 1.4 1998/10/26 11:22:51 peter
  214. * updates
  215. Date Version Who Comments
  216. 07/28/98 0.2 Bazsi Added some constants
  217. }