Search code examples
c++pythonlinuxxserver

How can I, on some global keystroke, paste some text to current active application in linux with Python or C++


I want to write app, which will work like a daemon and on some global keystroke paste some text to current active application (text editor, browser, jabber client) I think i will need to use some low level xserver api. How i can do this with Python or C++ ?


Solution

  • You can use the xmacroplay utility from xmacro to do this under X windows I think. Either use it directly - send it commands to standard input using the subprocess module, or read the source code and find out how it does it! I don't think there are python bindings for it.

    From the xmacroplay website

    xmacroplay:
    Reads lines from the standard input. It can understand the following lines:
    
    Delay [sec]     - delays the program with [sec] secundums
    ButtonPress [n] - sends a ButtonPress event with button [n]
              this emulates the pressing of the mouse button [n]
    ButtonRelease [n]   - sends a ButtonRelease event with button [n]
              this emulates the releasing of the mouse button [n]
    ... snip lots more ...
    

    This is probably the command you are interested in

    String [max. 1024 long string]
            - Sends the string as single characters converted to
              KeyPress and KeyRelease events based on a
              character table in chartbl.h (currently only
              Latin1 is used...)
    

    There is also Xnee which does a similar thing.