keyboard.pp 28 KB

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