This is a simple script to intercept a hotkey during the specified program execution.
The script uses python-xlib X library to listen for KeyPress events in specified program. Now it's possible to run any program and specify our shell command to be executed on the hotkey pressed.
usage: hotkey-proxy.py [-h] --key KEY [--modifier MODIFIER] --cmd CMD program
positional arguments:
program Program to run
optional arguments:
-h, --help show this help message and exit
--key KEY, -k KEY Key to capture (examples: S, M, etc.)
--modifier MODIFIER, -m MODIFIER
Modifier to capture (examples: shift, ctrl, alt, any, none)
--cmd CMD, -c CMD Shell command to run on captured key
key
argument specifies the keyboard button to be pressedmodifier
sets the modifier forkey
that could be one of the following:shift, ctrl, alt, any, none
.any
modifier will trigger whenkey
is pressed with any ofshift, ctrl, alt
or none of them (justkey
).none
will trigger only whenkey
is pressed without modifierscmd
specifies the shell string to be executed
There is an example ./example.sh
that runs obsidian
program and sets the execution of echo lol >> /tmp/log.txt
shell string after pressing the hotkey Ctrl+S
each time.
-
Add multiple hotkeys
-
It's not well-tested with different programs, so, there could be some issues. There is the problem in window identification after running a program because we cannot simply get the X11 window by PID (I tried to use
_NET_WM_PID
window property for identification, but it's unstable). Now it was temporarily solved by waiting for active window to change