keyboard.tex 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % The Keyboard unit
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4. \chapter{The KEYBOARD unit}
  5. \FPCexampledir{kbdex}
  6. The \file{KeyBoard} unit implements a keyboard access layer which is system
  7. independent. It can be used to poll the keyboard state and wait for certaine
  8. events.
  9. \section{Constants, Type and variables }
  10. \subsection{Constants}
  11. The following constants define some error constants, which may be returned
  12. by the keyboard functions.
  13. \begin{verbatim}
  14. errKbdBase = 1010;
  15. errKbdInitError = errKbdBase + 0;
  16. errKbdNotImplemented = errKbdBase + 1;
  17. \end{verbatim}
  18. The following constants denote special keyboard keys. The first constants
  19. denote the function keys:
  20. \begin{verbatim}
  21. const
  22. kbdF1 = $FF01;
  23. kbdF2 = $FF02;
  24. kbdF3 = $FF03;
  25. kbdF4 = $FF04;
  26. kbdF5 = $FF05;
  27. kbdF6 = $FF06;
  28. kbdF7 = $FF07;
  29. kbdF8 = $FF08;
  30. kbdF9 = $FF09;
  31. kbdF10 = $FF0A;
  32. kbdF11 = $FF0B;
  33. kbdF12 = $FF0C;
  34. kbdF13 = $FF0D;
  35. kbdF14 = $FF0E;
  36. kbdF15 = $FF0F;
  37. kbdF16 = $FF10;
  38. kbdF17 = $FF11;
  39. kbdF18 = $FF12;
  40. kbdF19 = $FF13;
  41. kbdF20 = $FF14;
  42. \end{verbatim}
  43. Constants \$15 till \$1F are reserved for future function keys. The
  44. following constants denote the curso movement keys:
  45. \begin{verbatim}
  46. kbdHome = $FF20;
  47. kbdUp = $FF21;
  48. kbdPgUp = $FF22;
  49. kbdLeft = $FF23;
  50. kbdMiddle = $FF24;
  51. kbdRight = $FF25;
  52. kbdEnd = $FF26;
  53. kbdDown = $FF27;
  54. kbdPgDn = $FF28;
  55. kbdInsert = $FF29;
  56. kbdDelete = $FF2A;
  57. \end{verbatim}
  58. Constants \$2B till \$2F are reserved for future keypad key.
  59. The following flags are also defined:
  60. \begin{verbatim}
  61. kbASCII = $00;
  62. kbUniCode = $01;
  63. kbFnKey = $02;
  64. kbPhys = $03;
  65. kbReleased = $04;
  66. \end{verbatim}
  67. They can be used to check And the following shift-state flags:
  68. \begin{verbatim}
  69. kbLeftShift = 1;
  70. kbRightShift = 2;
  71. kbShift = kbLeftShift or kbRightShift;
  72. kbCtrl = 4;
  73. kbAlt = 8;
  74. \end{verbatim}
  75. \subsection{Types}
  76. The \var{TKeyEvent} type is the base type for all keyboard events:
  77. \begin{verbatim}
  78. TKeyEvent = Longint;
  79. \end{verbatim}
  80. The structure of a \var{TKeyEvent} is explained in \seet{keyevent}.
  81. \begin{FPCltable}{ll}{Structure of TKeyEvent}{keyevent}
  82. Bytes & Meaning \\ \hline
  83. 2 bytes & Depending on \var{flags} either the physical representation of a key
  84. (under DOS scancode, ascii code pair), or the translated
  85. ASCII/unicode character.\\
  86. 1 byte & shift-state when this key was pressed (or shortly after) \\
  87. 1 byte & \var{flags}, determining how to read the first two bytes \\ \hline.
  88. \end{FPCltable}
  89. The shift-state can be checked using the various shift-state constants,
  90. and the flags in the last byte can be checked using one of the
  91. kbASCII,kbUniCode,kbFnKey,kbPhys, kbReleased constants.
  92. If there are two keys returning the same char-code, there's no way to find
  93. out which one was pressed (Gray+ and Simple+). If it needs to be known which
  94. was pressed, the untranslated keycodes must be used, but these are system
  95. dependent. System dependent constants may be defined to cover those, with
  96. possibily having the same name (but different value).
  97. \subsection{Variables}
  98. The following variable contains any pending (i.e. not yet consumed) keyboard
  99. event:
  100. \begin{verbatim}
  101. var
  102. PendingKeyEvent : TKeyEvent;
  103. \end{verbatim}
  104. \section{Functions and Procedures}
  105. \begin{procedure}{DoneKeyboard}
  106. \Declaration
  107. Procedure DoneKeyboard;
  108. \Description
  109. \var{DoneKeyboard} de-initializes the keyboard interface.
  110. It clears up any allocated memory, or restores the console or terminal
  111. the program was running in to its initial state. This function should
  112. be called on program exit. Failing to do so may leave the terminal or
  113. console window in an unusable state. Its exact action depends on the
  114. platform on which the program is running.
  115. \Errors
  116. None.
  117. \SeeAlso
  118. \seep{InitKeyBoard}
  119. \end{procedure}
  120. For an example, see most other functions.
  121. \begin{function}{GetKeyEvent}
  122. \Declaration
  123. function GetKeyEvent: TKeyEvent;
  124. \Description
  125. \var{GetKeyEvent} returns the last keyevent if one was stored in
  126. \var{PendingKeyEvent}, or waits for one if none is available.
  127. A non-blocking version is available in \seef{PollKeyEvent}.
  128. The returned key is encoded as a \var{TKeyEvent} type variable, and
  129. is normally the physical key code, which can be translated with one
  130. of the translation functions \seef{TranslateKeyEvent} or
  131. \seef{TranslateKeyEventUniCode}. See the types section for a
  132. description of how the key is described.
  133. \Errors
  134. If no key became available, 0 is returned.
  135. \SeeAlso
  136. \seep{PutKeyEvent}, \seef{PollKeyEvent}, \seef{TranslateKeyEvent},
  137. \seef{TranslateKeyEventUniCode}
  138. \end{function}
  139. \FPCexample{ex1}
  140. \begin{function}{GetKeyEventChar}
  141. \Declaration
  142. function GetKeyEventChar(KeyEvent: TKeyEvent): Char;
  143. \Description
  144. \var{GetKeyEventChar} returns the charcode part of the given
  145. \var{KeyEvent}, if it contains a translated character key
  146. keycode. The charcode is simply the ascii code of the
  147. character key that was pressed.
  148. It returns the null character if the key was not a character key, but e.g. a
  149. function key.
  150. \Errors
  151. None.
  152. \SeeAlso
  153. \seef{GetKeyEventUniCode},
  154. \seef{GetKeyEventShiftState},
  155. \seef{GetKeyEventFlags},
  156. \seef{GetKeyEventCode},
  157. \seef{GetKeyEvent}
  158. \end{function}
  159. For an example, see \seef{GetKeyEvent}
  160. \begin{function}{GetKeyEventCode}
  161. \Declaration
  162. function GetKeyEventCode(KeyEvent: TKeyEvent): Word;
  163. \Description
  164. \var{GetKeyEventCode} returns the translated function keycode part of
  165. the given KeyEvent, if it contains a translated function key.
  166. If the key pressed was not a function key, the null character is returned.
  167. \Errors
  168. None.
  169. \SeeAlso
  170. \seef{GetKeyEventUniCode},
  171. \seef{GetKeyEventShiftState},
  172. \seef{GetKeyEventFlags},
  173. \seef{GetKeyEventChar},
  174. \seef{GetKeyEvent}
  175. \end{function}
  176. \FPCexample{ex2}
  177. \begin{function}{GetKeyEventFlags}
  178. \Declaration
  179. function GetKeyEventFlags(KeyEvent: TKeyEvent): Byte;
  180. \Description
  181. \var{GetKeyEventFlags} returns the flags part of the given
  182. \var{KeyEvent}.
  183. \Errors
  184. None.
  185. \SeeAlso
  186. \seef{GetKeyEventUniCode},
  187. \seef{GetKeyEventShiftState},
  188. \seef{GetKeyEventCode},
  189. \seef{GetKeyEventChar},
  190. \seef{GetKeyEvent}
  191. \end{function}
  192. For an example, see \seef{GetKeyEvent}
  193. \begin{function}{GetKeyEventShiftState}
  194. \Declaration
  195. function GetKeyEventShiftState(KeyEvent: TKeyEvent): Byte;
  196. \Description
  197. \var{GetKeyEventShiftState} returns the shift-state values of
  198. the given \var{KeyEvent}. This can be used to detect which of the modifier
  199. keys \var{Shift}, \var{Alt} or \var{Ctrl} were pressed. If none were
  200. pressed, zero is returned.
  201. Note that this function does not always return expected results;
  202. In a unix X-Term, the modifier keys do not always work.
  203. \Errors
  204. None.
  205. \SeeAlso
  206. \seef{GetKeyEventUniCode},
  207. \seef{GetKeyEventFlags},
  208. \seef{GetKeyEventCode},
  209. \seef{GetKeyEventChar},
  210. \seef{GetKeyEvent}
  211. \end{function}
  212. \FPCexample{ex3}
  213. \begin{function}{GetKeyEventUniCode}
  214. \Declaration
  215. function GetKeyEventUniCode(KeyEvent: TKeyEvent): Word;
  216. \Description
  217. \var{GetKeyEventUniCode} returns the unicode part of the
  218. given \var{KeyEvent} if it contains a translated unicode
  219. character.
  220. \Errors
  221. None.
  222. \SeeAlso
  223. \seef{GetKeyEventShiftState},
  224. \seef{GetKeyEventFlags},
  225. \seef{GetKeyEventCode},
  226. \seef{GetKeyEventChar},
  227. \seef{GetKeyEvent}
  228. \end{function}
  229. No example available yet.
  230. \begin{procedure}{InitKeyBoard}
  231. \Declaration
  232. procedure InitKeyboard;
  233. \Description
  234. \var{InitKeyboard} initializes the keyboard interface, any
  235. additional platform specific parameters should be passed by
  236. setting platform-specific global variables.
  237. This function should be called once, before using any of the
  238. keyboard functions.
  239. \Errors
  240. None.
  241. \SeeAlso
  242. \seep{DoneKeyboard}
  243. \end{procedure}
  244. For an example, see most other functions.
  245. \begin{function}{IsFunctionKey}
  246. \Declaration
  247. function IsFunctionKey(KeyEvent: TKeyEvent): Boolean;
  248. \Description
  249. \var{IsFunctionKey} returns \var{True} if the given key event
  250. in \var{KeyEvent} was a function key or not.
  251. \Errors
  252. None.
  253. \SeeAlso
  254. \seef{GetKeyEvent}
  255. \end{function}
  256. \FPCexample{ex7}
  257. \begin{function}{PollKeyEvent}
  258. \Declaration
  259. function PollKeyEvent: TKeyEvent;
  260. \Description
  261. \var{PollKeyEvent} checks whether a key event is available,
  262. and returns it if one is found. If no event is pending,
  263. it returns 0.
  264. Note that this does not remove the key from the pending keys.
  265. The key should still be retrieved from the pending key events
  266. list with the \seef{GetKeyEvent} function.
  267. \Errors
  268. None.
  269. \SeeAlso
  270. \seep{PutKeyEvent}, \seef{GetKeyEvent}
  271. \end{function}
  272. \FPCexample{ex4}
  273. \begin{function}{PollShiftStateEvent}
  274. \Declaration
  275. function PollShiftStateEvent: TKeyEvent;
  276. \Description
  277. \var{PollShiftStateEvent} returns the current shiftstate in a
  278. keyevent. This will return 0 if there is no key event pending.
  279. \Errors
  280. None.
  281. \SeeAlso
  282. \seef{PollKeyEvent}, \seef{GetKeyEvent}
  283. \end{function}
  284. \FPCexample{ex6}
  285. \begin{procedure}{PutKeyEvent}
  286. \Declaration
  287. procedure PutKeyEvent(KeyEvent: TKeyEvent);
  288. \Description
  289. \var{PutKeyEvent} adds the given \var{KeyEvent} to the input
  290. queue. Please note that depending on the implementation this
  291. can hold only one value, i.e. when calling \var{PutKeyEvent}
  292. multiple times, only the last pushed key will be remembered.
  293. \Errors
  294. None
  295. \SeeAlso
  296. \seef{PollKeyEvent}, \seef{GetKeyEvent}
  297. \end{procedure}
  298. \FPCexample{ex5}
  299. \begin{function}{TranslateKeyEvent}
  300. \Declaration
  301. function TranslateKeyEvent(KeyEvent: TKeyEvent): TKeyEvent;
  302. \Description
  303. \var{TranslateKeyEvent} performs ASCII translation of the \var{KeyEvent}.
  304. It translates a physical key to a function key if the key is a function key,
  305. and translates the physical key to the ordinal of the ascii character if
  306. there is an equivalent character key.
  307. \Errors
  308. None.
  309. \SeeAlso
  310. \seef{TranslateKeyEventUniCode}
  311. \end{function}
  312. For an example, see \seef{GetKeyEvent}
  313. \begin{function}{TranslateKeyEventUniCode}
  314. \Declaration
  315. function TranslateKeyEventUniCode(KeyEvent: TKeyEvent): TKeyEvent;
  316. \Description
  317. \var{TranslateKeyEventUniCode} performs Unicode translation of the
  318. \var{KeyEvent}. It is not yet implemented for all platforms.
  319. \Errors
  320. If the function is not yet implemented, then the \var{ErrorCode} of the
  321. \file{system} unit will be set to \var{errKbdNotImplemented}
  322. \SeeAlso
  323. \end{function}
  324. No example available yet.
  325. \section{Keyboard scan codes}
  326. Special physical keys are encoded with the DOS scan codes for these keys
  327. in the second byte of the \var{TKeyEvent} type.
  328. A complete list of scan codes can be found in \seet{keyscans}.
  329. A list of scan codes for special keys and combinations with the SHIFT, ALT
  330. and CTRl keys can be found in \seet{speckeys}.
  331. \begin{FPCltable}{llllll}{Physical keys scan codes}{keyscans}
  332. Code & Key & Code & Key & Code & Key\\ \hline
  333. \input{keys.tex}
  334. \hline
  335. \end{FPCltable}
  336. \begin{FPCltable}{llccc}{Special keys scan codes}{speckeys}
  337. Key & Code & SHIFT-Key & CTRL-Key & Alt-Key \\ \hline
  338. NoKey & 00 & & & \\
  339. F1 & 3B & 54 & 5E & 68 \\
  340. F2 & 3C & 55 & 5F & 69 \\
  341. F3 & 3D & 56 & 60 & 6A \\
  342. F4 & 3E & 57 & 61 & 6B \\
  343. F5 & 3F & 58 & 62 & 6C \\
  344. F6 & 40 & 59 & 63 & 6D \\
  345. F7 & 41 & 5A & 64 & 6E \\
  346. F8 & 42 & 5A & 65 & 6F \\
  347. F9 & 43 & 5B & 66 & 70 \\
  348. F10 & 44 & 5C & 67 & 71 \\
  349. F11 & 85 & 87 & 89 & 8B \\
  350. F12 & 86 & 88 & 8A & 8C \\
  351. Home & 47 & & 77 & 97 \\
  352. Up & 48 & & 8D & 98 \\
  353. PgUp & 49 & & 84 & 99 \\
  354. Left & 4B & & 73 & 9B \\
  355. Center & 4C & & 8F & \\
  356. Right & 4D & & 74 & 9D \\
  357. end & 4F & & 75 & 9F \\
  358. Down & 50 & & 91 & A0 \\
  359. PgDn & 51 & & 76 & A1 \\
  360. Ins & 52 & 05 & 04 & A2 \\
  361. Del & 53 & 07 & 06 & A3 \\
  362. Tab & 8 & 0F & 94 & A5 \\
  363. GreyPlus & & & 90 & 4E \\
  364. \hline
  365. \end{FPCltable}