WarPie90

X11Keyboard

Jan 16th, 2016
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 15.16 KB | None | 0 0
  1. unit X11Keyboard;
  2. {=============================================================================================]
  3.  This (for some unknown reason) is unstable (at best) for me.
  4.  Issue:
  5.    1.) It writes the expected text, but only some times... After a few test-runs it seems to
  6.    stall/freeze the application for a half a minute, and once it "unfreezes" I see a few
  7.    of the expected characters gets written.
  8.    2.) If keys are pressed with no wait between them some keys seems to be skipped.
  9.  
  10.  Testsystem:
  11.  - Linux Mint Debian - x86-64
  12.  - Simba - x86-64
  13.  - Running inside Oracle VirtualBox
  14.  
  15.  The very similar Python program using python xlib works fine.
  16.  
  17.  Note:
  18.    Not completed, lacks support for other modifiers than SHIFT, for example ALT_GR
  19. [=============================================================================================}
  20. {$mode objfpc}{$H+}
  21. {$linklib Xtst}
  22.  
  23. interface
  24.  
  25. uses
  26.   SysUtils, ctypes, x, xlib;
  27.  
  28. function XTestFakeKeyEvent(display:PDisplay; keycode: cuint; is_press: CBool; delay: CUInt): CInt; cdecl; external;
  29. procedure XKeyDown(display:PDisplay; key: Word);
  30. procedure XKeyUp(display:PDisplay; key: Word);
  31. procedure KeyDown(display:PDisplay; key: String);
  32. procedure KeyUp(display:PDisplay; key: String);
  33.  
  34. (*
  35.   More keycodes exits in the keysym unit. Only the important ones are declared here.
  36. *)
  37. var
  38.   XK_SHIFT_L,
  39.   XK_SHIFT,
  40.   XK_SHIFT_R,
  41.   XK_ALT_L,
  42.   XK_ALT,
  43.   XK_ALT_R,
  44.   XK_ALT_GR,
  45.   XK_CONTROL_L,
  46.   XK_CONTROL_R,
  47.   XK_CONTROL,
  48.   XK_CAPITAL,
  49.   XK_META_L,
  50.   XK_META_R,
  51.   XK_SUPER_L,
  52.   XK_SUPER_R,
  53.   XK_HYPER_L,
  54.   XK_HYPER_R,
  55.   XK_LWIN,
  56.   XK_RWIN,
  57.  
  58.   XK_HOME,
  59.   XK_BEGIN,
  60.   XK_END,
  61.   XK_UP,
  62.   XK_DOWN,
  63.   XK_LEFT,
  64.   XK_RIGHT,
  65.   XK_PRIOR,
  66.   XK_NEXT,
  67.   XK_PGUP,
  68.   XK_PGDN,
  69.  
  70.   XK_BACK,
  71.   XK_TAB,
  72.   XK_LINEFEED,
  73.   XK_CLEAR,
  74.   XK_RETURN,
  75.   XK_PAUSE,
  76.   XK_SCROLL,
  77.   XK_SYS_REQ,
  78.   XK_ESCAPE,
  79.   XK_SELECT,
  80.   XK_PRINT,
  81.   XK_EXECUTE,
  82.   XK_SNAPSHOT,
  83.   XK_INSERT,
  84.   XK_DELETE,
  85.   XK_UNDO,
  86.   XK_REDO,
  87.   XK_MENU,
  88.   XK_APPS,
  89.   XK_FIND,
  90.   XK_CANCEL,
  91.   XK_HELP,
  92.   XK_BREAK,
  93.   XK_NUMLOCK,
  94.  
  95.   XK_MODE_SWITCH,
  96.   XK_MODECHANGE,
  97.   XK_SCRIPT_SWITCH,
  98.  
  99.   XK_NUMPAD_SPACE,
  100.   XK_NUMPAD_TAB,
  101.   XK_NUMPAD_ENTER,
  102.   XK_NUMPAD_F1,
  103.   XK_NUMPAD_F2,
  104.   XK_NUMPAD_F3,
  105.   XK_NUMPAD_F4,
  106.   XK_NUMPAD_HOME,
  107.   XK_NUMPAD_LEFT,
  108.   XK_NUMPAD_UP,
  109.   XK_NUMPAD_RIGHT,
  110.   XK_NUMPAD_DOWN,
  111.   XK_NUMPAD_PRIOR,
  112.   XK_NUMPAD_PAGE_UP,
  113.   XK_NUMPAD_NEXT,
  114.   XK_NUMPAD_PAGE_DOWN,
  115.   XK_NUMPAD_END,
  116.   XK_NUMPAD_BEGIN,
  117.   XK_NUMPAD_INSERT,
  118.   XK_NUMPAD_DELETE,
  119.   XK_NUMPAD_EQUAL,
  120.   XK_NUMPAD_MUL,
  121.   XK_NUMPAD_ADD,
  122.   XK_NUMPAD_SEPARATOR,
  123.   XK_NUMPAD_SUBTRACT,
  124.   XK_NUMPAD_DECIMAL,
  125.   XK_NUMPAD_DIVIDE,
  126.   XK_NUMPAD0,
  127.   XK_NUMPAD1,
  128.   XK_NUMPAD2,
  129.   XK_NUMPAD3,
  130.   XK_NUMPAD4,
  131.   XK_NUMPAD5,
  132.   XK_NUMPAD6,
  133.   XK_NUMPAD7,
  134.   XK_NUMPAD8,
  135.   XK_NUMPAD9,
  136.  
  137.   XK_SPACE,
  138.   XK_EXCLAM,
  139.   XK_NUMSIGN,
  140.   XK_PERCENT,
  141.   XK_DOLLAR,
  142.   XK_AMPERSAND,
  143.   XK_QUOTE,
  144.   XK_APOSTROPHE,
  145.   XK_PAREN_L,
  146.   XK_PAREN_R,
  147.   XK_ASTERISK,
  148.   XK_EQUAL,
  149.   XK_PLUS,
  150.   XK_COMMA,
  151.   XK_MINUS,
  152.   XK_PERIOD,
  153.   XK_FRONTSLASH,
  154.   XK_COLON,
  155.   XK_SEMICOLON,
  156.   XK_LESS,
  157.   XK_GREATER,
  158.   XK_QUESTION,
  159.   XK_AT,
  160.   XK_BRACKET_L,
  161.   XK_BRACKET_R,
  162.   XK_BACKSLASH,
  163.   XK_CIRCUMFLEX,
  164.   XK_UNDERSCORE,
  165.   XK_GRAVE,
  166.   XK_BRACE_L,
  167.   XK_BRACE_R,
  168.   XK_BAR,
  169.   XK_TILDE,
  170.  
  171.   XK_F1,
  172.   XK_F2,
  173.   XK_F3,
  174.   XK_F4,
  175.   XK_F5,
  176.   XK_F6,
  177.   XK_F7,
  178.   XK_F8,
  179.   XK_F9,
  180.   XK_F10,
  181.   XK_F11,
  182.   XK_F12,
  183.   XK_F13,
  184.   XK_F14,
  185.   XK_F15,
  186.   XK_F16,
  187.   XK_F17,
  188.   XK_F18,
  189.   XK_F19,
  190.   XK_F20,
  191.   XK_F21,
  192.   XK_F22,
  193.   XK_F23,
  194.   XK_F24,
  195.  
  196.   XK_0,
  197.   XK_1,
  198.   XK_2,
  199.   XK_3,
  200.   XK_4,
  201.   XK_5,
  202.   XK_6,
  203.   XK_7,
  204.   XK_8,
  205.   XK_9,
  206.  
  207.   XK_A,
  208.   XK_B,
  209.   XK_C,
  210.   XK_D,
  211.   XK_E,
  212.   XK_F,
  213.   XK_G,
  214.   XK_H,
  215.   XK_I,
  216.   XK_J,
  217.   XK_K,
  218.   XK_L,
  219.   XK_M,
  220.   XK_N,
  221.   XK_O,
  222.   XK_P,
  223.   XK_Q,
  224.   XK_R,
  225.   XK_S,
  226.   XK_T,
  227.   XK_U,
  228.   XK_V,
  229.   XK_W,
  230.   XK_X,
  231.   XK_Y,
  232.   XK_Z: Word;
  233.  
  234. implementation
  235.  
  236. uses Classes;
  237.  
  238. var
  239.   SpecialXKeys: TStringList;
  240.   Display0: PDisplay;
  241.  
  242.  
  243. function IsShifted(k:Char): Boolean;
  244. const SHIFTKEYS:String = '<>?:"{}|~!@#$%^&*()_+=';
  245. begin
  246.   Result := (k in ['A'..'Z']) or (Pos(k, SHIFTKEYS) > 0)
  247. end;
  248.  
  249. function GetKeyCode(Key:AnsiString; display:PDisplay=nil): Word;
  250. var
  251.   ks: TKeySym;
  252. begin
  253.   if display = nil then display := display0;
  254.  
  255.   ks := XStringToKeysym(PChar(Key));
  256.   if (ks = NoSymbol) then
  257.     raise Exception.Create(Format('Undefined key: "%s"', [key]));
  258.  
  259.   Result := XKeysymToKeycode(display, ks);
  260. end;
  261.  
  262. function GetKeyCode(keysym:Int32; display:PDisplay=nil): Word; overload;
  263. begin
  264.   if display = nil then display := display0;
  265.   if (KeySym = NoSymbol) then
  266.     raise Exception.Create(Format('Undefined keysym: "%d"', [KeySym]));
  267.  
  268.   Result := XKeysymToKeycode(display, keysym);
  269. end;
  270.  
  271. (*
  272.   Note all keys loaded are case sensitive.
  273. *)
  274. procedure InitializeXKeys();
  275. begin
  276.   //MODIFIER KEYS
  277.   XK_SHIFT_L    := GetKeyCode('Shift_L');
  278.   XK_SHIFT      := XK_SHIFT_L;
  279.   XK_SHIFT_R    := GetKeyCode('Shift_R');
  280.   XK_ALT_L      := GetKeyCode('Alt_L');
  281.   XK_ALT        := XK_ALT_L;
  282.   XK_ALT_R      := GetKeyCode('Alt_R');
  283.   XK_ALT_GR     := GetKeyCode($FE03); //GetKeyCode('ISO_Level3_Shift');
  284.   XK_CONTROL_L  := GetKeyCode('Control_L');
  285.   XK_CONTROL_R  := GetKeyCode('Control_R');
  286.   XK_CONTROL    := XK_CONTROL_L;
  287.   XK_CAPITAL    := GetKeyCode('Caps_Lock');
  288.   XK_META_L     := GetKeyCode('Meta_L');
  289.   XK_META_R     := GetKeyCode('Meta_R');
  290.   XK_SUPER_L    := GetKeyCode('Super_L');
  291.   XK_SUPER_R    := GetKeyCode('Super_R');
  292.   XK_HYPER_L    := GetKeyCode('Hyper_L');
  293.   XK_HYPER_R    := GetKeyCode('Hyper_R');
  294.   XK_LWIN       := XK_SUPER_L;
  295.   XK_RWIN       := XK_SUPER_R;
  296.  
  297.   //CURSOR CONTROL AND MOTION
  298.   XK_HOME  := GetKeyCode('Home');
  299.   XK_BEGIN := GetKeyCode('Begin'); //^
  300.   XK_END   := GetKeyCode('End');
  301.   XK_UP    := GetKeyCode('Up');
  302.   XK_DOWN  := GetKeyCode('Down');
  303.   XK_LEFT  := GetKeyCode('Left');
  304.   XK_RIGHT := GetKeyCode('Right');
  305.   XK_PRIOR := GetKeyCode('Prior');
  306.   XK_NEXT  := GetKeyCode('Next');
  307.   XK_PGUP  := GetKeyCode('Page_Up'); //^
  308.   XK_PGDN  := GetKeyCode('Page_Down');
  309.  
  310.   //MISC FUNCTIONS & TTY FUNCTION KEYS
  311.   XK_BACK     := GetKeyCode('BackSpace');
  312.   XK_TAB      := GetKeyCode('Tab');
  313.   XK_LINEFEED := GetKeyCode('Linefeed');
  314.   XK_CLEAR    := GetKeyCode('Clear');
  315.   XK_RETURN   := GetKeyCode('Return');
  316.   XK_PAUSE    := GetKeyCode('Pause');
  317.   XK_SCROLL   := GetKeyCode('Scroll_Lock');
  318.   XK_SYS_REQ  := GetKeyCode('Sys_Req');
  319.   XK_ESCAPE   := GetKeyCode('Escape');
  320.   XK_SELECT   := GetKeyCode('Select');
  321.   XK_PRINT    := GetKeyCode('Print');
  322.   XK_EXECUTE  := GetKeyCode('Execute');
  323.   XK_SNAPSHOT := XK_PRINT;
  324.   XK_INSERT   := GetKeyCode('Insert');
  325.   XK_DELETE   := GetKeyCode('Delete');
  326.   XK_UNDO     := GetKeyCode('Undo');
  327.   XK_REDO     := GetKeyCode('Redo');
  328.   XK_MENU     := GetKeyCode('Menu');
  329.   XK_APPS     := XK_MENU;
  330.   XK_FIND     := GetKeyCode('Find');
  331.   XK_CANCEL   := GetKeyCode('Cancel');
  332.   XK_HELP     := GetKeyCode('Help');
  333.   XK_BREAK    := GetKeyCode('Break');
  334.   XK_NUMLOCK  := GetKeyCode('Num_Lock');
  335.  
  336.   XK_MODE_SWITCH   := GetKeyCode('Mode_switch');
  337.   XK_MODECHANGE    := XK_MODE_SWITCH;
  338.   XK_SCRIPT_SWITCH := GetKeyCode('script_switch');
  339.  
  340.   //KEYPAD | NUMPAD
  341.   XK_NUMPAD_SPACE     := GetKeyCode('KP_Space');
  342.   XK_NUMPAD_TAB       := GetKeyCode('KP_Tab');
  343.   XK_NUMPAD_ENTER     := GetKeyCode('KP_Enter');
  344.   XK_NUMPAD_F1        := GetKeyCode('KP_F1');
  345.   XK_NUMPAD_F2        := GetKeyCode('KP_F2');
  346.   XK_NUMPAD_F3        := GetKeyCode('KP_F3');
  347.   XK_NUMPAD_F4        := GetKeyCode('KP_F4');
  348.   XK_NUMPAD_HOME      := GetKeyCode('KP_Home');
  349.   XK_NUMPAD_LEFT      := GetKeyCode('KP_Left');
  350.   XK_NUMPAD_UP        := GetKeyCode('KP_Up');
  351.   XK_NUMPAD_RIGHT     := GetKeyCode('KP_Right');
  352.   XK_NUMPAD_DOWN      := GetKeyCode('KP_Down');
  353.   XK_NUMPAD_PRIOR     := GetKeyCode('KP_Prior');
  354.   XK_NUMPAD_PAGE_UP   := GetKeyCode('KP_Page_Up');
  355.   XK_NUMPAD_NEXT      := GetKeyCode('KP_Next');
  356.   XK_NUMPAD_PAGE_DOWN := GetKeyCode('KP_Page_Down');
  357.   XK_NUMPAD_END       := GetKeyCode('KP_End');
  358.   XK_NUMPAD_BEGIN     := GetKeyCode('KP_Begin');
  359.   XK_NUMPAD_INSERT    := GetKeyCode('KP_Insert');
  360.   XK_NUMPAD_DELETE    := GetKeyCode('KP_Delete');
  361.   XK_NUMPAD_EQUAL     := GetKeyCode('KP_Equal');
  362.   XK_NUMPAD_MUL       := GetKeyCode('KP_Multiply');
  363.   XK_NUMPAD_ADD       := GetKeyCode('KP_Add');
  364.   XK_NUMPAD_SEPARATOR := GetKeyCode('KP_Separator');
  365.   XK_NUMPAD_SUBTRACT  := GetKeyCode('KP_Subtract');
  366.   XK_NUMPAD_DECIMAL   := GetKeyCode('KP_Decimal');
  367.   XK_NUMPAD_DIVIDE    := GetKeyCode('KP_Divide');
  368.   XK_NUMPAD0          := GetKeyCode('KP_0');
  369.   XK_NUMPAD1          := GetKeyCode('KP_1');
  370.   XK_NUMPAD2          := GetKeyCode('KP_2');
  371.   XK_NUMPAD3          := GetKeyCode('KP_3');
  372.   XK_NUMPAD4          := GetKeyCode('KP_4');
  373.   XK_NUMPAD5          := GetKeyCode('KP_5');
  374.   XK_NUMPAD6          := GetKeyCode('KP_6');
  375.   XK_NUMPAD7          := GetKeyCode('KP_7');
  376.   XK_NUMPAD8          := GetKeyCode('KP_8');
  377.   XK_NUMPAD9          := GetKeyCode('KP_9');
  378.  
  379.   //SYMBOL KEYS
  380.   XK_SPACE      := GetKeyCode('space');
  381.   XK_EXCLAM     := GetKeyCode('exclam');
  382.   XK_NUMSIGN    := GetKeyCode('numbersign');
  383.   XK_PERCENT    := GetKeyCode('percent');
  384.   XK_DOLLAR     := GetKeyCode('dollar');
  385.   XK_AMPERSAND  := GetKeyCode('ampersand');
  386.   XK_QUOTE      := GetKeyCode('quotedbl');
  387.   XK_APOSTROPHE := GetKeyCode('apostrophe');
  388.   XK_PAREN_L    := GetKeyCode('parenleft');
  389.   XK_PAREN_R    := GetKeyCode('parenright');
  390.   XK_ASTERISK   := GetKeyCode('asterisk');
  391.   XK_EQUAL      := GetKeyCode('equal');
  392.   XK_PLUS       := GetKeyCode('plus');
  393.   XK_COMMA      := GetKeyCode('comma');
  394.   XK_MINUS      := GetKeyCode('minus');
  395.   XK_PERIOD     := GetKeyCode('period');
  396.   XK_FRONTSLASH := GetKeyCode('slash');
  397.   XK_COLON      := GetKeyCode('colon');
  398.   XK_SEMICOLON  := GetKeyCode('semicolon');
  399.   XK_LESS       := GetKeyCode('less');
  400.   XK_GREATER    := GetKeyCode('greater');
  401.   XK_QUESTION   := GetKeyCode('question');
  402.   XK_AT         := GetKeyCode('at');
  403.   XK_BRACKET_L  := GetKeyCode('bracketleft');
  404.   XK_BRACKET_R  := GetKeyCode('bracketright');
  405.   XK_BACKSLASH  := GetKeyCode('backslash');
  406.   XK_CIRCUMFLEX := GetKeyCode('asciicircum');
  407.   XK_UNDERSCORE := GetKeyCode('underscore');
  408.   XK_GRAVE      := GetKeyCode('grave');
  409.   XK_BRACE_L    := GetKeyCode('braceleft');
  410.   XK_BRACE_R    := GetKeyCode('braceright');
  411.   XK_BAR        := GetKeyCode('bar');
  412.   XK_TILDE      := GetKeyCode('asciitilde');
  413.  
  414.   //FUNCTION KEYS
  415.   XK_F1  := GetKeyCode('F1');
  416.   XK_F2  := GetKeyCode('F2');
  417.   XK_F3  := GetKeyCode('F3');
  418.   XK_F4  := GetKeyCode('F4');
  419.   XK_F5  := GetKeyCode('F5');
  420.   XK_F6  := GetKeyCode('F6');
  421.   XK_F7  := GetKeyCode('F7');
  422.   XK_F8  := GetKeyCode('F8');
  423.   XK_F9  := GetKeyCode('F9');
  424.   XK_F10 := GetKeyCode('F10');
  425.   XK_F11 := GetKeyCode('F11');
  426.   XK_F12 := GetKeyCode('F12');
  427.   XK_F13 := GetKeyCode('F13');
  428.   XK_F14 := GetKeyCode('F14');
  429.   XK_F15 := GetKeyCode('F15');
  430.   XK_F16 := GetKeyCode('F16');
  431.   XK_F17 := GetKeyCode('F17');
  432.   XK_F18 := GetKeyCode('F18');
  433.   XK_F19 := GetKeyCode('F19');
  434.   XK_F20 := GetKeyCode('F20');
  435.   XK_F21 := GetKeyCode('F21');
  436.   XK_F22 := GetKeyCode('F22');
  437.   XK_F23 := GetKeyCode('F23');
  438.   XK_F24 := GetKeyCode('F24');
  439.  
  440.   //NUMBER KEYS
  441.   XK_0  := GetKeyCode('0');
  442.   XK_1  := GetKeyCode('1');
  443.   XK_2  := GetKeyCode('2');
  444.   XK_3  := GetKeyCode('3');
  445.   XK_4  := GetKeyCode('4');
  446.   XK_5  := GetKeyCode('5');
  447.   XK_6  := GetKeyCode('6');
  448.   XK_7  := GetKeyCode('7');
  449.   XK_8  := GetKeyCode('8');
  450.   XK_9  := GetKeyCode('9');
  451.  
  452.   //CHARACTER KEYS
  453.   XK_A  := GetKeyCode('a');
  454.   XK_B  := GetKeyCode('b');
  455.   XK_C  := GetKeyCode('c');
  456.   XK_D  := GetKeyCode('d');
  457.   XK_E  := GetKeyCode('e');
  458.   XK_F  := GetKeyCode('f');
  459.   XK_G  := GetKeyCode('g');
  460.   XK_H  := GetKeyCode('h');
  461.   XK_I  := GetKeyCode('i');
  462.   XK_J  := GetKeyCode('j');
  463.   XK_K  := GetKeyCode('k');
  464.   XK_L  := GetKeyCode('l');
  465.   XK_M  := GetKeyCode('m');
  466.   XK_N  := GetKeyCode('n');
  467.   XK_O  := GetKeyCode('o');
  468.   XK_P  := GetKeyCode('p');
  469.   XK_Q  := GetKeyCode('q');
  470.   XK_R  := GetKeyCode('r');
  471.   XK_S  := GetKeyCode('s');
  472.   XK_T  := GetKeyCode('t');
  473.   XK_U  := GetKeyCode('u');
  474.   XK_V  := GetKeyCode('v');
  475.   XK_W  := GetKeyCode('w');
  476.   XK_X  := GetKeyCode('x');
  477.   XK_Y  := GetKeyCode('y');
  478.   XK_Z  := GetKeyCode('z');
  479.  
  480.   SpecialXKeys := TStringList.Create();
  481.   SpecialXKeys.Values[#9 ] := IntToStr(XK_TAB);
  482.   SpecialXKeys.Values[#10] := IntToStr(XK_RETURN);
  483.   SpecialXKeys.Values[#32] := IntToStr(XK_SPACE);
  484.   SpecialXKeys.Values[#34] := IntToStr(XK_QUOTE);
  485.   SpecialXKeys.Values[#39] := IntToStr(XK_APOSTROPHE);
  486.   SpecialXKeys.Values['!'] := IntToStr(XK_EXCLAM);
  487.   SpecialXKeys.Values['#'] := IntToStr(XK_NUMSIGN);
  488.   SpecialXKeys.Values['%'] := IntToStr(XK_PERCENT);
  489.   SpecialXKeys.Values['$'] := IntToStr(XK_DOLLAR);
  490.   SpecialXKeys.Values['&'] := IntToStr(XK_AMPERSAND);
  491.   SpecialXKeys.Values['('] := IntToStr(XK_PAREN_L);
  492.   SpecialXKeys.Values[')'] := IntToStr(XK_PAREN_R);
  493.   SpecialXKeys.Values['*'] := IntToStr(XK_ASTERISK);
  494.   SpecialXKeys.Values['='] := IntToStr(XK_EQUAL);
  495.   SpecialXKeys.Values['+'] := IntToStr(XK_PLUS);
  496.   SpecialXKeys.Values[','] := IntToStr(XK_COMMA);
  497.   SpecialXKeys.Values['-'] := IntToStr(XK_MINUS);
  498.   SpecialXKeys.Values['.'] := IntToStr(XK_PERIOD);
  499.   SpecialXKeys.Values['/'] := IntToStr(XK_FRONTSLASH);
  500.   SpecialXKeys.Values[':'] := IntToStr(XK_COLON);
  501.   SpecialXKeys.Values[';'] := IntToStr(XK_SEMICOLON);
  502.   SpecialXKeys.Values['<'] := IntToStr(XK_LESS);
  503.   SpecialXKeys.Values['>'] := IntToStr(XK_GREATER);
  504.   SpecialXKeys.Values['?'] := IntToStr(XK_QUESTION);
  505.   SpecialXKeys.Values['@'] := IntToStr(XK_AT);
  506.   SpecialXKeys.Values['['] := IntToStr(XK_BRACKET_L);
  507.   SpecialXKeys.Values[']'] := IntToStr(XK_BRACKET_R);
  508.   SpecialXKeys.Values['\'] := IntToStr(XK_BACKSLASH);
  509.   SpecialXKeys.Values['^'] := IntToStr(XK_CIRCUMFLEX);
  510.   SpecialXKeys.Values['_'] := IntToStr(XK_UNDERSCORE);
  511.   SpecialXKeys.Values['`'] := IntToStr(XK_GRAVE);
  512.   SpecialXKeys.Values['{'] := IntToStr(XK_BRACE_L);
  513.   SpecialXKeys.Values['|'] := IntToStr(XK_BAR);
  514.   SpecialXKeys.Values['}'] := IntToStr(XK_BRACE_R);
  515.   SpecialXKeys.Values['~'] := IntToStr(XK_TILDE);
  516. end;
  517.  
  518. procedure XKeyDown(display:PDisplay; key: Word);
  519. begin
  520.   try
  521.     XTestFakeKeyEvent(display, key, True, 0);
  522.     XFlush(display);
  523.   except
  524.   end;
  525. end;
  526.  
  527. procedure XKeyUp(display:PDisplay; key: Word);
  528. begin
  529.   try
  530.     XTestFakeKeyEvent(display, key, False, 0);
  531.     XFlush(display);
  532.   except
  533.   end;
  534. end;
  535.  
  536. procedure KeyDown(display:PDisplay; key: String);
  537. var
  538.   xk:Int32;
  539.   specialk:String;
  540. begin
  541.   if key = '' then Exit;
  542.  
  543.   if IsShifted(key[1]) then XKeyDown(display, XK_SHIFT_L); //incomplete: ISO_Level3_Shift (AltGr)
  544.   specialk := SpecialXKeys.Values[key];
  545.   if specialk <> '' then
  546.     xk := StrToInt(specialk)
  547.   else
  548.     xk := GetKeyCode(key, display);
  549.   XKeyDown(display, xk);
  550. end;
  551.  
  552. procedure KeyUp(display:PDisplay; key: String);
  553. var
  554.   xk:Int32;
  555.   specialk:String;
  556. begin
  557.   if key = '' then Exit;
  558.  
  559.   specialk := SpecialXKeys.Values[key];
  560.   if specialk <> '' then
  561.     xk := StrToInt(specialk)
  562.   else
  563.     xk := GetKeyCode(key, display);
  564.   XKeyUp(display, xk);
  565.  
  566.   if IsShifted(key[1]) then XKeyUp(display, XK_SHIFT_L); //incomplete: ISO_Level3_Shift (AltGr)
  567. end;
  568.  
  569.  
  570. initialization
  571.   display0 := XOpenDisplay(nil);
  572.   InitializeXKeys();
  573.  
  574. finalization
  575.   XCloseDisplay(display0);
  576.  
  577. end.
Add Comment
Please, Sign In to add comment