/components/mouseandkeyinput/xkeyinput.pas

http://github.com/graemeg/lazarus · Pascal · 198 lines · 155 code · 23 blank · 20 comment · 2 complexity · 7642a4a0b7fa527adbf6d68ca6db884b MD5 · raw file

  1. { XKeyInput
  2. Copyright (C) 2008 Tom Gregorovic
  3. This source is free software; you can redistribute it and/or modify it under the terms of the
  4. GNU General Public License as published by the Free Software Foundation; either version 2 of the
  5. License, or (at your option) any later version.
  6. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  7. even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8. General Public License for more details.
  9. A copy of the GNU General Public License is available on the World Wide Web at
  10. <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software
  11. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  12. }
  13. unit XKeyInput;
  14. {$mode objfpc}{$H+}
  15. {$linklib Xtst}
  16. interface
  17. uses
  18. Classes, SysUtils, Controls, Forms,
  19. X, XLib, KeySym,
  20. KeyInputIntf;
  21. type
  22. { TXKeyInput }
  23. TXKeyInput = class(TKeyInput)
  24. protected
  25. procedure DoDown(Key: Word); override;
  26. procedure DoUp(Key: Word); override;
  27. end;
  28. function InitializeKeyInput: TKeyInput;
  29. function XTestFakeKeyEvent(dpy: PDisplay; keycode: dword; is_press: Boolean;
  30. delay: dword): longint; cdecl; external;
  31. implementation
  32. uses LCLType;
  33. function InitializeKeyInput: TKeyInput;
  34. begin
  35. Result := TXKeyInput.Create;
  36. end;
  37. function VirtualKeyToXKeySym(Key: Word): TKeySym;
  38. begin
  39. case Key of
  40. VK_BACK: Result := XK_BackSpace;
  41. VK_TAB: Result := XK_Tab;
  42. VK_CLEAR: Result := XK_Clear;
  43. VK_RETURN: Result := XK_Return;
  44. VK_SHIFT: Result := XK_Shift_L;
  45. VK_CONTROL: Result := XK_Control_L;
  46. VK_MENU: Result := XK_VoidSymbol; // alt key crashes app, XK_Alt_R;
  47. VK_CAPITAL: Result := XK_Caps_Lock;
  48. VK_ESCAPE: Result := XK_Escape;
  49. VK_SPACE: Result := XK_space;
  50. VK_PRIOR: Result := XK_Prior;
  51. VK_NEXT: Result := XK_Next;
  52. VK_END: Result := XK_End;
  53. VK_HOME: Result := XK_Home;
  54. VK_LEFT: Result := XK_Left;
  55. VK_UP: Result := XK_Up;
  56. VK_RIGHT: Result := XK_Right;
  57. VK_DOWN: Result := XK_Down;
  58. VK_SELECT: Result := XK_Select;
  59. VK_PRINT: Result := XK_Print;
  60. VK_EXECUTE: Result := XK_Execute;
  61. VK_INSERT: Result := XK_Insert;
  62. VK_DELETE: Result := XK_Delete;
  63. VK_HELP: Result := XK_Help;
  64. VK_0: Result := XK_0;
  65. VK_1: Result := XK_1;
  66. VK_2: Result := XK_2;
  67. VK_3: Result := XK_3;
  68. VK_4: Result := XK_4;
  69. VK_5: Result := XK_5;
  70. VK_6: Result := XK_6;
  71. VK_7: Result := XK_7;
  72. VK_8: Result := XK_8;
  73. VK_9: Result := XK_9;
  74. VK_A: Result := XK_a;
  75. VK_B: Result := XK_b;
  76. VK_C: Result := XK_c;
  77. VK_D: Result := XK_d;
  78. VK_E: Result := XK_e;
  79. VK_F: Result := XK_f;
  80. VK_G: Result := XK_g;
  81. VK_H: Result := XK_h;
  82. VK_I: Result := XK_i;
  83. VK_J: Result := XK_j;
  84. VK_K: Result := XK_k;
  85. VK_L: Result := XK_l;
  86. VK_M: Result := XK_m;
  87. VK_N: Result := XK_n;
  88. VK_O: Result := XK_o;
  89. VK_P: Result := XK_p;
  90. VK_Q: Result := XK_q;
  91. VK_R: Result := XK_r;
  92. VK_S: Result := XK_s;
  93. VK_T: Result := XK_t;
  94. VK_U: Result := XK_u;
  95. VK_V: Result := XK_v;
  96. VK_W: Result := XK_w;
  97. VK_X: Result := XK_x;
  98. VK_Y: Result := XK_y;
  99. VK_Z: Result := XK_z;
  100. VK_NUMPAD0: Result := XK_KP_0;
  101. VK_NUMPAD1: Result := XK_KP_1;
  102. VK_NUMPAD2: Result := XK_KP_2;
  103. VK_NUMPAD3: Result := XK_KP_3;
  104. VK_NUMPAD4: Result := XK_KP_4;
  105. VK_NUMPAD5: Result := XK_KP_5;
  106. VK_NUMPAD6: Result := XK_KP_6;
  107. VK_NUMPAD7: Result := XK_KP_7;
  108. VK_NUMPAD8: Result := XK_KP_8;
  109. VK_NUMPAD9: Result := XK_KP_9;
  110. VK_MULTIPLY: Result := XK_KP_Multiply;
  111. VK_ADD: Result := XK_KP_Add;
  112. VK_SEPARATOR: Result := XK_KP_Separator;
  113. VK_SUBTRACT: Result := XK_KP_Subtract;
  114. VK_DECIMAL: Result := XK_KP_Decimal;
  115. VK_DIVIDE: Result := XK_KP_Divide;
  116. VK_F1: Result := XK_F1;
  117. VK_F2: Result := XK_F2;
  118. VK_F3: Result := XK_F3;
  119. VK_F4: Result := XK_F4;
  120. VK_F5: Result := XK_F5;
  121. VK_F6: Result := XK_F6;
  122. VK_F7: Result := XK_F7;
  123. VK_F8: Result := XK_F8;
  124. VK_F9: Result := XK_F9;
  125. VK_F10: Result := XK_F10;
  126. VK_F11: Result := XK_F11;
  127. VK_F12: Result := XK_F12;
  128. VK_F13: Result := XK_F13;
  129. VK_F14: Result := XK_F14;
  130. VK_F15: Result := XK_F15;
  131. VK_F16: Result := XK_F16;
  132. VK_F17: Result := XK_F17;
  133. VK_F18: Result := XK_F18;
  134. VK_F19: Result := XK_F19;
  135. VK_F20: Result := XK_F20;
  136. VK_F21: Result := XK_F21;
  137. VK_F22: Result := XK_F22;
  138. VK_F23: Result := XK_F23;
  139. VK_F24: Result := XK_F24;
  140. VK_NUMLOCK: Result := XK_Num_Lock;
  141. VK_SCROLL: Result := XK_Scroll_Lock;
  142. else
  143. Result := XK_VoidSymbol;
  144. end;
  145. end;
  146. { TXKeyInput }
  147. procedure TXKeyInput.DoDown(Key: Word);
  148. var
  149. Display: PDisplay;
  150. KeySym: TKeySym;
  151. begin
  152. KeySym := VirtualKeyToXKeySym(Key);
  153. if KeySym = XK_VoidSymbol then Exit;
  154. Display := XOpenDisplay(nil);
  155. XTestFakeKeyEvent(Display, XKeysymToKeycode(Display, KeySym), True, 0);
  156. XFlush(Display);
  157. XCloseDisplay(Display);
  158. end;
  159. procedure TXKeyInput.DoUp(Key: Word);
  160. var
  161. Display: PDisplay;
  162. KeySym: TKeySym;
  163. begin
  164. KeySym := VirtualKeyToXKeySym(Key);
  165. if KeySym = XK_VoidSymbol then Exit;
  166. Display := XOpenDisplay(nil);
  167. XTestFakeKeyEvent(Display, XKeysymToKeycode(Display, KeySym), False, 0);
  168. XFlush(Display);
  169. XCloseDisplay(Display);
  170. end;
  171. end.