podcast
podcast

Reputation: 141

How to catch xdotool key from Python GUI App

I have a Python Tkinter window and I want to catch a key which is triggered by xdotool, even though my window is not active.

I am able to catch the key when the window is active by following code.

import tkinter

def hit(e):
    print("hit...", e)

win = tkinter.Tk()
win.title("python key test")

win.bind_all("h", hit)

win.mainloop()

Then I can catch the "h" key with the following command when the window "python key test" is active.

sleep 5; xdotool key --window $(xdotool search --name "python key test") h

But I want to manage that even though the window "python key test" is minimized.

The same situation works for Firefox but doesn't work for Chromium.

Thanks.

Upvotes: 0

Views: 484

Answers (1)

podcast
podcast

Reputation: 141

I tried xvkbd and it worked... But... (I will explain this part below)

xvkbd -window "python key test" -text "h"

I think this is somehow related to XTEST and XSendEvent.

I found some information about xdotool on https://linuxcommandlibrary.com/man/xdotool

It is important to note that for key and mouse events, we only use XSendEvent when a specific window is targeted. Otherwise, we use XTEST.

Some programs can be configured to accept events even if they are generated by xdotool. Seek the documentation of your application for help.

So, according to this information it may be possible to configure our app to accept xdotool events. But how, I don't know.

I wish I could say xvkbd solved my problem. But unfortunately. I don't know why but the key "h" sent repeatedly many times.

I tried many things and I find something, a weak workaround, somehow working so far. I am not sure if it will work for long or not.

xvkbd -window "python key test" -text "h\D9"

I hope someone can give me a better solution, preferably for xdotool or at least for xvkbd.

Upvotes: 0

Related Questions