keyboard.pp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. Keyboard unit for Win32
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program 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.
  12. **********************************************************************}
  13. unit Keyboard;
  14. interface
  15. {$i keybrdh.inc}
  16. implementation
  17. { WARNING: Keyboard-Drivers (i.e. german) will only work under WinNT.
  18. 95 and 98 do not support keyboard-drivers other than us for win32
  19. console-apps. So we always get the keys in us-keyboard layout
  20. from Win9x.
  21. }
  22. uses
  23. Windows,
  24. Dos,
  25. WinEvent;
  26. {$i keyboard.inc}
  27. const MaxQueueSize = 120;
  28. FrenchKeyboard = $040C040C;
  29. KeyboardActive : boolean =false;
  30. var
  31. keyboardeventqueue : array[0..maxqueuesize] of TKeyEventRecord;
  32. nextkeyevent,nextfreekeyevent : longint;
  33. newKeyEvent : THandle; {sinaled if key is available}
  34. lockVar : TCriticalSection; {for queue access}
  35. lastShiftState : byte; {set by handler for PollShiftStateEvent}
  36. altNumActive : boolean; {for alt+0..9}
  37. altNumBuffer : string [3];
  38. { used for keyboard specific stuff }
  39. KeyBoardLayout : HKL;
  40. procedure incqueueindex(var l : longint);
  41. begin
  42. inc(l);
  43. { wrap around? }
  44. if l>maxqueuesize then
  45. l:=0;
  46. end;
  47. function keyEventsInQueue : boolean;
  48. begin
  49. keyEventsInQueue := (nextkeyevent <> nextfreekeyevent);
  50. end;
  51. { gets or peeks the next key from the queue, does not wait for new keys }
  52. function getKeyEventFromQueue (VAR t : TKeyEventRecord; Peek : boolean) : boolean;
  53. begin
  54. EnterCriticalSection (lockVar);
  55. if keyEventsInQueue then
  56. begin
  57. t := keyboardeventqueue[nextkeyevent];
  58. if not peek then incqueueindex (nextkeyevent);
  59. getKeyEventFromQueue := true;
  60. if not keyEventsInQueue then ResetEvent (newKeyEvent);
  61. end else
  62. begin
  63. getKeyEventFromQueue := false;
  64. ResetEvent (newKeyEvent);
  65. end;
  66. LeaveCriticalSection (lockVar);
  67. end;
  68. { gets the next key from the queue, does wait for new keys }
  69. function getKeyEventFromQueueWait (VAR t : TKeyEventRecord) : boolean;
  70. begin
  71. WaitForSingleObject (newKeyEvent, INFINITE);
  72. getKeyEventFromQueueWait := getKeyEventFromQueue (t, false);
  73. end;
  74. { translate win32 shift-state to keyboard shift state }
  75. function transShiftState (ControlKeyState : dword) : byte;
  76. var b : byte;
  77. begin
  78. b := 0;
  79. if ControlKeyState and SHIFT_PRESSED <> 0 then { win32 makes no difference between left and right shift }
  80. b := b or kbShift;
  81. if (ControlKeyState and LEFT_CTRL_PRESSED <> 0) or
  82. (ControlKeyState and RIGHT_CTRL_PRESSED <> 0) then
  83. b := b or kbCtrl;
  84. if (ControlKeyState and LEFT_ALT_PRESSED <> 0) or
  85. (ControlKeyState and RIGHT_ALT_PRESSED <> 0) then
  86. b := b or kbAlt;
  87. transShiftState := b;
  88. end;
  89. { The event-Handler thread from the unit event will call us if a key-event
  90. is available }
  91. procedure HandleKeyboard(var ir:INPUT_RECORD);
  92. var
  93. i : longint;
  94. c : word;
  95. addThis: boolean;
  96. begin
  97. with ir.KeyEvent do
  98. begin
  99. { key up events are ignored (except alt) }
  100. if bKeyDown then
  101. begin
  102. EnterCriticalSection (lockVar);
  103. for i:=1 to wRepeatCount do
  104. begin
  105. addThis := true;
  106. if (dwControlKeyState and LEFT_ALT_PRESSED <> 0) or
  107. (dwControlKeyState and RIGHT_ALT_PRESSED <> 0) then {alt pressed}
  108. if (wVirtualKeyCode >= $60) and (wVirtualKeyCode <= $69) then {0..9 on NumBlock}
  109. begin
  110. if length (altNumBuffer) = 3 then
  111. delete (altNumBuffer,1,1);
  112. altNumBuffer := altNumBuffer + char (wVirtualKeyCode-48);
  113. altNumActive := true;
  114. addThis := false;
  115. end else
  116. begin
  117. altNumActive := false;
  118. altNumBuffer := '';
  119. end;
  120. if addThis then
  121. begin
  122. keyboardeventqueue[nextfreekeyevent]:=
  123. ir.KeyEvent;
  124. incqueueindex(nextfreekeyevent);
  125. end;
  126. end;
  127. lastShiftState := transShiftState (dwControlKeyState); {save it for PollShiftStateEvent}
  128. SetEvent (newKeyEvent); {event that a new key is available}
  129. LeaveCriticalSection (lockVar);
  130. end
  131. else
  132. begin
  133. lastShiftState := transShiftState (dwControlKeyState); {save it for PollShiftStateEvent}
  134. {for alt-number we have to look for alt-key release}
  135. if altNumActive then
  136. begin
  137. if (wVirtualKeyCode = $12) then {alt-released}
  138. begin
  139. if altNumBuffer <> '' then {numbers with alt pressed?}
  140. begin
  141. Val (altNumBuffer, c, i);
  142. if (i = 0) and (c <= 255) then {valid number?}
  143. begin {add to queue}
  144. fillchar (ir, sizeof (ir), 0);
  145. bKeyDown := true;
  146. AsciiChar := char (c);
  147. {and add to queue}
  148. EnterCriticalSection (lockVar);
  149. keyboardeventqueue[nextfreekeyevent]:=ir.KeyEvent;
  150. incqueueindex(nextfreekeyevent);
  151. SetEvent (newKeyEvent); {event that a new key is available}
  152. LeaveCriticalSection (lockVar);
  153. end;
  154. end;
  155. altNumActive := false; {clear alt-buffer}
  156. altNumBuffer := '';
  157. end;
  158. end;
  159. end;
  160. end;
  161. end;
  162. procedure InitKeyboard;
  163. begin
  164. if KeyboardActive then
  165. exit;
  166. KeyBoardLayout:=GetKeyboardLayout(0);
  167. lastShiftState := 0;
  168. FlushConsoleInputBuffer(StdInputHandle);
  169. newKeyEvent := CreateEvent (nil, // address of security attributes
  170. true, // flag for manual-reset event
  171. false, // flag for initial state
  172. nil); // address of event-object name
  173. if newKeyEvent = INVALID_HANDLE_VALUE then
  174. begin
  175. // what to do here ????
  176. RunError (217);
  177. end;
  178. InitializeCriticalSection (lockVar);
  179. altNumActive := false;
  180. altNumBuffer := '';
  181. nextkeyevent:=0;
  182. nextfreekeyevent:=0;
  183. SetKeyboardEventHandler (@HandleKeyboard);
  184. KeyboardActive:=true;
  185. end;
  186. procedure DoneKeyboard;
  187. begin
  188. if not KeyboardActive then
  189. exit;
  190. SetKeyboardEventHandler(nil); {hangs???}
  191. DeleteCriticalSection (lockVar);
  192. FlushConsoleInputBuffer(StdInputHandle);
  193. closeHandle (newKeyEvent);
  194. KeyboardActive:=false;
  195. end;
  196. {$define USEKEYCODES}
  197. {Translatetable Win32 -> Dos for Special Keys = Function Key, Cursor Keys
  198. and Keys other than numbers on numblock (to make fv happy) }
  199. {combinations under dos: Shift+Ctrl: same as Ctrl
  200. Shift+Alt : same as alt
  201. Ctrl+Alt : nothing (here we get it like alt)}
  202. {$ifdef USEKEYCODES}
  203. { use positive values for ScanCode we want to set
  204. 0 for key where we should leave the scancode
  205. -1 for OEM specifc keys
  206. -2 for unassigned
  207. -3 for Kanji systems ???
  208. }
  209. const
  210. Unassigned = -2;
  211. Kanji = -3;
  212. OEM_specific = -1;
  213. KeyToQwertyScan : array [0..255] of integer =
  214. (
  215. { 00 } 0,
  216. { 01 VK_LBUTTON } 0,
  217. { 02 VK_RBUTTON } 0,
  218. { 03 VK_CANCEL } 0,
  219. { 04 VK_MBUTTON } 0,
  220. { 05 unassigned } -2,
  221. { 06 unassigned } -2,
  222. { 07 unassigned } -2,
  223. { 08 VK_BACK } $E,
  224. { 09 VK_TAB } $F,
  225. { 0A unassigned } -2,
  226. { 0B unassigned } -2,
  227. { 0C VK_CLEAR ?? } 0,
  228. { 0D VK_RETURN } 0,
  229. { 0E unassigned } -2,
  230. { 0F unassigned } -2,
  231. { 10 VK_SHIFT } 0,
  232. { 11 VK_CONTROL } 0,
  233. { 12 VK_MENU (Alt key) } 0,
  234. { 13 VK_PAUSE } 0,
  235. { 14 VK_CAPITAL (Caps Lock) } 0,
  236. { 15 Reserved for Kanji systems} -3,
  237. { 16 Reserved for Kanji systems} -3,
  238. { 17 Reserved for Kanji systems} -3,
  239. { 18 Reserved for Kanji systems} -3,
  240. { 19 Reserved for Kanji systems} -3,
  241. { 1A unassigned } -2,
  242. { 1B VK_ESCAPE } $1,
  243. { 1C Reserved for Kanji systems} -3,
  244. { 1D Reserved for Kanji systems} -3,
  245. { 1E Reserved for Kanji systems} -3,
  246. { 1F Reserved for Kanji systems} -3,
  247. { 20 VK_SPACE} 0,
  248. { 21 VK_PRIOR (PgUp) } 0,
  249. { 22 VK_NEXT (PgDown) } 0,
  250. { 23 VK_END } 0,
  251. { 24 VK_HOME } 0,
  252. { 25 VK_LEFT } 0,
  253. { 26 VK_UP } 0,
  254. { 27 VK_RIGHT } 0,
  255. { 28 VK_DOWN } 0,
  256. { 29 VK_SELECT ??? } 0,
  257. { 2A OEM specific !! } -1,
  258. { 2B VK_EXECUTE } 0,
  259. { 2C VK_SNAPSHOT } 0,
  260. { 2D VK_INSERT } 0,
  261. { 2E VK_DELETE } 0,
  262. { 2F VK_HELP } 0,
  263. { 30 VK_0 '0' } 11,
  264. { 31 VK_1 '1' } 2,
  265. { 32 VK_2 '2' } 3,
  266. { 33 VK_3 '3' } 4,
  267. { 34 VK_4 '4' } 5,
  268. { 35 VK_5 '5' } 6,
  269. { 36 VK_6 '6' } 7,
  270. { 37 VK_7 '7' } 8,
  271. { 38 VK_8 '8' } 9,
  272. { 39 VK_9 '9' } 10,
  273. { 3A unassigned } -2,
  274. { 3B unassigned } -2,
  275. { 3C unassigned } -2,
  276. { 3D unassigned } -2,
  277. { 3E unassigned } -2,
  278. { 3F unassigned } -2,
  279. { 40 unassigned } -2,
  280. { 41 VK_A 'A' } $1E,
  281. { 42 VK_B 'B' } $30,
  282. { 43 VK_C 'C' } $2E,
  283. { 44 VK_D 'D' } $20,
  284. { 45 VK_E 'E' } $12,
  285. { 46 VK_F 'F' } $21,
  286. { 47 VK_G 'G' } $22,
  287. { 48 VK_H 'H' } $23,
  288. { 49 VK_I 'I' } $17,
  289. { 4A VK_J 'J' } $24,
  290. { 4B VK_K 'K' } $25,
  291. { 4C VK_L 'L' } $26,
  292. { 4D VK_M 'M' } $32,
  293. { 4E VK_N 'N' } $31,
  294. { 4F VK_O 'O' } $18,
  295. { 50 VK_P 'P' } $19,
  296. { 51 VK_Q 'Q' } $10,
  297. { 52 VK_R 'R' } $13,
  298. { 53 VK_S 'S' } $1F,
  299. { 54 VK_T 'T' } $14,
  300. { 55 VK_U 'U' } $16,
  301. { 56 VK_V 'V' } $2F,
  302. { 57 VK_W 'W' } $11,
  303. { 58 VK_X 'X' } $2D,
  304. { 59 VK_Y 'Y' } $15,
  305. { 5A VK_Z 'Z' } $2C,
  306. { 5B unassigned } -2,
  307. { 5C unassigned } -2,
  308. { 5D unassigned } -2,
  309. { 5E unassigned } -2,
  310. { 5F unassigned } -2,
  311. { 60 VK_NUMPAD0 NumKeyPad '0' } 11,
  312. { 61 VK_NUMPAD1 NumKeyPad '1' } 2,
  313. { 62 VK_NUMPAD2 NumKeyPad '2' } 3,
  314. { 63 VK_NUMPAD3 NumKeyPad '3' } 4,
  315. { 64 VK_NUMPAD4 NumKeyPad '4' } 5,
  316. { 65 VK_NUMPAD5 NumKeyPad '5' } 6,
  317. { 66 VK_NUMPAD6 NumKeyPad '6' } 7,
  318. { 67 VK_NUMPAD7 NumKeyPad '7' } 8,
  319. { 68 VK_NUMPAD8 NumKeyPad '8' } 9,
  320. { 69 VK_NUMPAD9 NumKeyPad '9' } 10,
  321. { 6A VK_MULTIPLY } 0,
  322. { 6B VK_ADD } 0,
  323. { 6C VK_SEPARATOR } 0,
  324. { 6D VK_SUBSTRACT } 0,
  325. { 6E VK_DECIMAL } 0,
  326. { 6F VK_DIVIDE } 0,
  327. { 70 VK_F1 'F1' } $3B,
  328. { 71 VK_F2 'F2' } $3C,
  329. { 72 VK_F3 'F3' } $3D,
  330. { 73 VK_F4 'F4' } $3E,
  331. { 74 VK_F5 'F5' } $3F,
  332. { 75 VK_F6 'F6' } $40,
  333. { 76 VK_F7 'F7' } $41,
  334. { 77 VK_F8 'F8' } $42,
  335. { 78 VK_F9 'F9' } $43,
  336. { 79 VK_F10 'F10' } $44,
  337. { 7A VK_F11 'F11' } $57,
  338. { 7B VK_F12 'F12' } $58,
  339. { 7C VK_F13 } 0,
  340. { 7D VK_F14 } 0,
  341. { 7E VK_F15 } 0,
  342. { 7F VK_F16 } 0,
  343. { 80 VK_F17 } 0,
  344. { 81 VK_F18 } 0,
  345. { 82 VK_F19 } 0,
  346. { 83 VK_F20 } 0,
  347. { 84 VK_F21 } 0,
  348. { 85 VK_F22 } 0,
  349. { 86 VK_F23 } 0,
  350. { 87 VK_F24 } 0,
  351. { 88 unassigned } -2,
  352. { 89 VK_NUMLOCK } 0,
  353. { 8A VK_SCROLL } 0,
  354. { 8B unassigned } -2,
  355. { 8C unassigned } -2,
  356. { 8D unassigned } -2,
  357. { 8E unassigned } -2,
  358. { 8F unassigned } -2,
  359. { 90 unassigned } -2,
  360. { 91 unassigned } -2,
  361. { 92 unassigned } -2,
  362. { 93 unassigned } -2,
  363. { 94 unassigned } -2,
  364. { 95 unassigned } -2,
  365. { 96 unassigned } -2,
  366. { 97 unassigned } -2,
  367. { 98 unassigned } -2,
  368. { 99 unassigned } -2,
  369. { 9A unassigned } -2,
  370. { 9B unassigned } -2,
  371. { 9C unassigned } -2,
  372. { 9D unassigned } -2,
  373. { 9E unassigned } -2,
  374. { 9F unassigned } -2,
  375. { A0 unassigned } -2,
  376. { A1 unassigned } -2,
  377. { A2 unassigned } -2,
  378. { A3 unassigned } -2,
  379. { A4 unassigned } -2,
  380. { A5 unassigned } -2,
  381. { A6 unassigned } -2,
  382. { A7 unassigned } -2,
  383. { A8 unassigned } -2,
  384. { A9 unassigned } -2,
  385. { AA unassigned } -2,
  386. { AB unassigned } -2,
  387. { AC unassigned } -2,
  388. { AD unassigned } -2,
  389. { AE unassigned } -2,
  390. { AF unassigned } -2,
  391. { B0 unassigned } -2,
  392. { B1 unassigned } -2,
  393. { B2 unassigned } -2,
  394. { B3 unassigned } -2,
  395. { B4 unassigned } -2,
  396. { B5 unassigned } -2,
  397. { B6 unassigned } -2,
  398. { B7 unassigned } -2,
  399. { B8 unassigned } -2,
  400. { B9 unassigned } -2,
  401. { BA OEM specific } 0,
  402. { BB OEM specific } 0,
  403. { BC OEM specific } 0,
  404. { BD OEM specific } 0,
  405. { BE OEM specific } 0,
  406. { BF OEM specific } 0,
  407. { C0 OEM specific } 0,
  408. { C1 unassigned } -2,
  409. { C2 unassigned } -2,
  410. { C3 unassigned } -2,
  411. { C4 unassigned } -2,
  412. { C5 unassigned } -2,
  413. { C6 unassigned } -2,
  414. { C7 unassigned } -2,
  415. { C8 unassigned } -2,
  416. { C9 unassigned } -2,
  417. { CA unassigned } -2,
  418. { CB unassigned } -2,
  419. { CC unassigned } -2,
  420. { CD unassigned } -2,
  421. { CE unassigned } -2,
  422. { CF unassigned } -2,
  423. { D0 unassigned } -2,
  424. { D1 unassigned } -2,
  425. { D2 unassigned } -2,
  426. { D3 unassigned } -2,
  427. { D4 unassigned } -2,
  428. { D5 unassigned } -2,
  429. { D6 unassigned } -2,
  430. { D7 unassigned } -2,
  431. { D8 unassigned } -2,
  432. { D9 unassigned } -2,
  433. { DA unassigned } -2,
  434. { DB OEM specific } 0,
  435. { DC OEM specific } 0,
  436. { DD OEM specific } 0,
  437. { DE OEM specific } 0,
  438. { DF OEM specific } 0,
  439. { E0 OEM specific } 0,
  440. { E1 OEM specific } 0,
  441. { E2 OEM specific } 0,
  442. { E3 OEM specific } 0,
  443. { E4 OEM specific } 0,
  444. { E5 unassigned } -2,
  445. { E6 OEM specific } 0,
  446. { E7 unassigned } -2,
  447. { E8 unassigned } -2,
  448. { E9 OEM specific } 0,
  449. { EA OEM specific } 0,
  450. { EB OEM specific } 0,
  451. { EC OEM specific } 0,
  452. { ED OEM specific } 0,
  453. { EE OEM specific } 0,
  454. { EF OEM specific } 0,
  455. { F0 OEM specific } 0,
  456. { F1 OEM specific } 0,
  457. { F2 OEM specific } 0,
  458. { F3 OEM specific } 0,
  459. { F4 OEM specific } 0,
  460. { F5 OEM specific } 0,
  461. { F6 unassigned } -2,
  462. { F7 unassigned } -2,
  463. { F8 unassigned } -2,
  464. { F9 unassigned } -2,
  465. { FA unassigned } -2,
  466. { FB unassigned } -2,
  467. { FC unassigned } -2,
  468. { FD unassigned } -2,
  469. { FE unassigned } -2,
  470. { FF unassigned } -2
  471. );
  472. {$endif USEKEYCODES}
  473. type TTEntryT = packed record
  474. n,s,c,a : byte; {normal,shift, ctrl, alt, normal only for f11,f12}
  475. end;
  476. CONST
  477. DosTT : ARRAY [$3B..$58] OF TTEntryT =
  478. ((n : $3B; s : $54; c : $5E; a: $68), {3B F1}
  479. (n : $3C; s : $55; c : $5F; a: $69), {3C F2}
  480. (n : $3D; s : $56; c : $60; a: $6A), {3D F3}
  481. (n : $3E; s : $57; c : $61; a: $6B), {3E F4}
  482. (n : $3F; s : $58; c : $62; a: $6C), {3F F5}
  483. (n : $40; s : $59; c : $63; a: $6D), {40 F6}
  484. (n : $41; s : $5A; c : $64; a: $6E), {41 F7}
  485. (n : $42; s : $5B; c : $65; a: $6F), {42 F8}
  486. (n : $43; s : $5C; c : $66; a: $70), {43 F9}
  487. (n : $44; s : $5D; c : $67; a: $71), {44 F10}
  488. (n : $45; s : $00; c : $00; a: $00), {45 ???}
  489. (n : $46; s : $00; c : $00; a: $00), {46 ???}
  490. (n : $47; s : $47; c : $77; a: $97), {47 Home}
  491. (n : $48; s : $00; c : $8D; a: $98), {48 Up}
  492. (n : $49; s : $49; c : $84; a: $99), {49 PgUp}
  493. (n : $4A; s : $00; c : $8E; a: $4A), {4A -}
  494. (n : $4B; s : $4B; c : $73; a: $9B), {4B Left}
  495. (n : $4C; s : $00; c : $00; a: $00), {4C ???}
  496. (n : $4D; s : $4D; c : $74; a: $9D), {4D Right}
  497. (n : $4E; s : $00; c : $90; a: $4E), {4E +}
  498. (n : $4F; s : $4F; c : $75; a: $9F), {4F End}
  499. (n : $50; s : $50; c : $91; a: $A0), {50 Down}
  500. (n : $51; s : $51; c : $76; a: $A1), {51 PgDown}
  501. (n : $52; s : $52; c : $92; a: $A2), {52 Insert}
  502. (n : $53; s : $53; c : $93; a: $A3), {53 Del}
  503. (n : $54; s : $00; c : $00; a: $00), {54 ???}
  504. (n : $55; s : $00; c : $00; a: $00), {55 ???}
  505. (n : $56; s : $00; c : $00; a: $00), {56 ???}
  506. (n : $85; s : $87; c : $89; a: $8B), {57 F11}
  507. (n : $86; s : $88; c : $8A; a: $8C)); {58 F12}
  508. DosTT09 : ARRAY [$02..$0F] OF TTEntryT =
  509. ((n : $00; s : $00; c : $00; a: $78), {02 1 }
  510. (n : $00; s : $00; c : $00; a: $79), {03 2 }
  511. (n : $00; s : $00; c : $00; a: $7A), {04 3 }
  512. (n : $00; s : $00; c : $00; a: $7B), {05 4 }
  513. (n : $00; s : $00; c : $00; a: $7C), {06 5 }
  514. (n : $00; s : $00; c : $00; a: $7D), {07 6 }
  515. (n : $00; s : $00; c : $00; a: $7E), {08 7 }
  516. (n : $00; s : $00; c : $00; a: $7F), {09 8 }
  517. (n : $00; s : $00; c : $00; a: $80), {0A 9 }
  518. (n : $00; s : $00; c : $00; a: $81), {0B 0 }
  519. (n : $00; s : $00; c : $00; a: $82), {0C ß }
  520. (n : $00; s : $00; c : $00; a: $00), {0D}
  521. (n : $00; s : $09; c : $00; a: $00), {0E Backspace}
  522. (n : $00; s : $0F; c : $94; a: $00)); {0F Tab }
  523. function translateKey (t : TKeyEventRecord) : TKeyEvent;
  524. var key : TKeyEvent;
  525. ss : byte;
  526. {$ifdef USEKEYCODES}
  527. ScanCode : byte;
  528. {$endif USEKEYCODES}
  529. b : byte;
  530. begin
  531. Key := 0;
  532. if t.bKeyDown then
  533. begin
  534. { ascii-char is <> 0 if not a specal key }
  535. { we return it here otherwise we have to translate more later }
  536. if t.AsciiChar <> #0 then
  537. begin
  538. {drivers needs scancode, we return it here as under dos and linux
  539. with $03000000 = the lowest two bytes is the physical representation}
  540. {$ifdef USEKEYCODES}
  541. Scancode:=KeyToQwertyScan[t.wVirtualKeyCode AND $00FF];
  542. If ScanCode>0 then
  543. t.wVirtualScanCode:=ScanCode;
  544. Key := byte (t.AsciiChar) + (t.wVirtualScanCode shl 8) + $03000000;
  545. ss := transShiftState (t.dwControlKeyState);
  546. key := key or (ss shl 16);
  547. if (ss and kbAlt <> 0) and (t.dwControlKeyState and RIGHT_ALT_PRESSED = 0) then
  548. key := key and $FFFFFF00;
  549. {$else not USEKEYCODES}
  550. Key := byte (t.AsciiChar) + ((t.wVirtualScanCode AND $00FF) shl 8) + $03000000;
  551. {$endif not USEKEYCODES}
  552. end else
  553. begin
  554. {$ifdef USEKEYCODES}
  555. Scancode:=KeyToQwertyScan[t.wVirtualKeyCode AND $00FF];
  556. If ScanCode>0 then
  557. t.wVirtualScanCode:=ScanCode;
  558. {$endif not USEKEYCODES}
  559. translateKey := 0;
  560. { ignore shift,ctrl,alt,numlock,capslock alone }
  561. case t.wVirtualKeyCode of
  562. $0010, {shift}
  563. $0011, {ctrl}
  564. $0012, {alt}
  565. $0014, {capslock}
  566. $0090, {numlock}
  567. $0091, {scrollock}
  568. { This should be handled !! }
  569. { these last two are OEM specific
  570. this is not good !!! }
  571. $00DC, {^ : next key i.e. a is modified }
  572. { Strange on my keyboard this corresponds to double point over i or u PM }
  573. $00DD: exit; {´ and ` : next key i.e. e is modified }
  574. end;
  575. key := $03000000 + (t.wVirtualScanCode shl 8); { make lower 8 bit=0 like under dos }
  576. end;
  577. { Handling of ~ key as AltGr 2 }
  578. { This is also French keyboard specific !! }
  579. { but without this I can not get a ~ !! PM }
  580. if (t.wVirtualKeyCode=$32) and
  581. (KeyBoardLayout = FrenchKeyboard) and
  582. (t.dwControlKeyState and RIGHT_ALT_PRESSED <> 0) then
  583. key:=(key and $ffffff00) or ord('~');
  584. { ok, now add Shift-State }
  585. ss := transShiftState (t.dwControlKeyState);
  586. key := key or (ss shl 16);
  587. { Reset Ascii-Char if Alt+Key, fv needs that, may be we
  588. need it for other special keys too
  589. 18 Sept 1999 AD: not for right Alt i.e. for AltGr+ß = \ on german keyboard }
  590. if ((ss and kbAlt <> 0) and (t.dwControlKeyState and RIGHT_ALT_PRESSED = 0)) or
  591. (*
  592. { yes, we need it for cursor keys, 25=left, 26=up, 27=right,28=down}
  593. {aggg, this will not work because esc is also virtualKeyCode 27!!}
  594. {if (t.wVirtualKeyCode >= 25) and (t.wVirtualKeyCode <= 28) then}
  595. no VK_ESCAPE is $1B !!
  596. there was a mistake :
  597. VK_LEFT is $25 not 25 !! *)
  598. { not $2E VK_DELETE because its only the Keypad point !! PM }
  599. (t.wVirtualKeyCode in [$21..$28,$2C,$2D,$2F]) then
  600. { if t.wVirtualScanCode in [$47..$49,$4b,$4d,$4f,$50..$53] then}
  601. key := key and $FFFFFF00;
  602. {and translate to dos-scancodes to make fv happy, we will convert this
  603. back in translateKeyEvent}
  604. if (t.dwControlKeyState and RIGHT_ALT_PRESSED) = 0 then {not for alt-gr}
  605. if (t.wVirtualScanCode >= low (DosTT)) and
  606. (t.wVirtualScanCode <= high (dosTT)) then
  607. begin
  608. b := 0;
  609. if (ss and kbAlt) <> 0 then
  610. b := DosTT[t.wVirtualScanCode].a
  611. else
  612. if (ss and kbCtrl) <> 0 then
  613. b := DosTT[t.wVirtualScanCode].c
  614. else
  615. if (ss and kbShift) <> 0 then
  616. b := DosTT[t.wVirtualScanCode].s
  617. else
  618. b := DosTT[t.wVirtualScanCode].n;
  619. if b <> 0 then
  620. key := (key and $FFFF00FF) or (longint (b) shl 8);
  621. end;
  622. {Alt-0 to Alt-9}
  623. if (t.dwControlKeyState and RIGHT_ALT_PRESSED) = 0 then {not for alt-gr}
  624. if (t.wVirtualScanCode >= low (DosTT09)) and
  625. (t.wVirtualScanCode <= high (dosTT09)) then
  626. begin
  627. b := 0;
  628. if (ss and kbAlt) <> 0 then
  629. b := DosTT09[t.wVirtualScanCode].a
  630. else
  631. if (ss and kbCtrl) <> 0 then
  632. b := DosTT09[t.wVirtualScanCode].c
  633. else
  634. if (ss and kbShift) <> 0 then
  635. b := DosTT09[t.wVirtualScanCode].s
  636. else
  637. b := DosTT09[t.wVirtualScanCode].n;
  638. if b <> 0 then
  639. key := (key and $FFFF0000) or (longint (b) shl 8);
  640. end;
  641. TranslateKey := key;
  642. end;
  643. translateKey := Key;
  644. end;
  645. function GetKeyEvent: TKeyEvent;
  646. var t : TKeyEventRecord;
  647. key : TKeyEvent;
  648. begin
  649. if PendingKeyEvent<>0 then
  650. begin
  651. GetKeyEvent:=PendingKeyEvent;
  652. PendingKeyEvent:=0;
  653. exit;
  654. end;
  655. key := 0;
  656. repeat
  657. if getKeyEventFromQueueWait (t) then
  658. key := translateKey (t);
  659. until key <> 0;
  660. GetKeyEvent := key;
  661. end;
  662. function PollKeyEvent: TKeyEvent;
  663. var t : TKeyEventRecord;
  664. k : TKeyEvent;
  665. begin
  666. if PendingKeyEvent<>0 then
  667. exit(PendingKeyEvent);
  668. PollKeyEvent := 0;
  669. if getKeyEventFromQueue (t, true) then
  670. begin
  671. { we get an enty for shift, ctrl, alt... }
  672. k := translateKey (t);
  673. while (k = 0) do
  674. begin
  675. getKeyEventFromQueue (t, false); {remove it}
  676. if not getKeyEventFromQueue (t, true) then exit;
  677. k := translateKey (t)
  678. end;
  679. PollKeyEvent := k;
  680. end;
  681. end;
  682. function TranslateKeyEvent(KeyEvent: TKeyEvent): TKeyEvent;
  683. begin
  684. if KeyEvent and $03000000 = $03000000 then
  685. begin
  686. if KeyEvent and $000000FF <> 0 then
  687. begin
  688. TranslateKeyEvent := KeyEvent and $00FFFFFF;
  689. exit;
  690. end;
  691. {translate function-keys and other specials, ascii-codes are already ok}
  692. case (KeyEvent AND $0000FF00) shr 8 of
  693. {F1..F10}
  694. $3B..$44 : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdF1 + ((KeyEvent AND $0000FF00) SHR 8) - $3B + $02000000;
  695. {F11,F12}
  696. $85..$86 : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdF11 + ((KeyEvent AND $0000FF00) SHR 8) - $85 + $02000000;
  697. {Shift F1..F10}
  698. $54..$5D : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdF1 + ((KeyEvent AND $0000FF00) SHR 8) - $54 + $02000000;
  699. {Shift F11,F12}
  700. $87..$88 : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdF11 + ((KeyEvent AND $0000FF00) SHR 8) - $87 + $02000000;
  701. {Alt F1..F10}
  702. $68..$71 : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdF1 + ((KeyEvent AND $0000FF00) SHR 8) - $68 + $02000000;
  703. {Alt F11,F12}
  704. $8B..$8C : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdF11 + ((KeyEvent AND $0000FF00) SHR 8) - $8B + $02000000;
  705. {Ctrl F1..F10}
  706. $5E..$67 : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdF1 + ((KeyEvent AND $0000FF00) SHR 8) - $5E + $02000000;
  707. {Ctrl F11,F12}
  708. $89..$8A : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdF11 + ((KeyEvent AND $0000FF00) SHR 8) - $89 + $02000000;
  709. {normal,ctrl,alt}
  710. $47,$77,$97 : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdHome + $02000000;
  711. $48,$8D,$98 : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdUp + $02000000;
  712. $49,$84,$99 : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdPgUp + $02000000;
  713. $4b,$73,$9B : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdLeft + $02000000;
  714. $4d,$74,$9D : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdRight + $02000000;
  715. $4f,$75,$9F : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdEnd + $02000000;
  716. $50,$91,$A0 : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdDown + $02000000;
  717. $51,$76,$A1 : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdPgDn + $02000000;
  718. $52,$92,$A2 : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdInsert + $02000000;
  719. $53,$93,$A3 : TranslateKeyEvent := (KeyEvent AND $FCFF0000) + kbdDelete + $02000000;
  720. else
  721. TranslateKeyEvent := KeyEvent;
  722. end;
  723. end else
  724. TranslateKeyEvent := KeyEvent;
  725. end;
  726. function TranslateKeyEventUniCode(KeyEvent: TKeyEvent): TKeyEvent;
  727. begin
  728. exit (KeyEvent); {???}
  729. end;
  730. function PollShiftStateEvent: TKeyEvent;
  731. var t : TKeyEvent;
  732. begin
  733. {may be better to save the last state and return that if no key is in buffer???}
  734. t := lastShiftState;
  735. PollShiftStateEvent := t shl 16;
  736. end;
  737. end.
  738. {
  739. $Log$
  740. Revision 1.3 2001-05-20 12:08:17 peter
  741. * fixed to compile with debug
  742. Revision 1.2 2001/01/14 22:20:00 peter
  743. * slightly optimized event handling (merged)
  744. Revision 1.1 2001/01/13 11:03:59 peter
  745. * API 2 RTL commit
  746. }