keyboard.pp 27 KB

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