Dzmitry
Dzmitry

Reputation: 23

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++ ?

Upvotes: 1

Views: 410

Answers (2)

Nick Craig-Wood
Nick Craig-Wood

Reputation: 54079

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.

Upvotes: 0

DigitalRoss
DigitalRoss

Reputation: 146043

Probably you want to hack xmon...


AFAIK there is no easy way to hook the X protocol. You will need to do "deep packet inspection", which would be fairly easy in the application event loop but not so easy, as you want, "like a daemon", or on "global keystroke[s]".

So, I know this is really brute force and ignorance, but I think you will have to wrap the X server by starting it on a non-standard port or publishing an environment variable, just like you were using something like an SSH tunnel to forward an X server connection.

There is an X protocol monitor called Xmon for which source is available. It might be a good starting point.

Upvotes: 1

Related Questions