The code is working fine the hotkey for the function is recognized and the buttons are being pressed.
An OBS function is triggered by the HotKey in hotkey_e().
When I press it physicaly it works. But when I run this script the obs function doesn't get triggered.
The only difference I've found through the print is that if you press the combination it prints <69> (for e) but if the script is pressing this combination it gives me a string "w"
from pynput.keyboard import Key, Controller, Listener, HotKey
kc = Controller()
def hotkey_e():
kc.press(Key.alt_l)
kc.press(Key.ctrl_l)
kc.press(Key.shift)
kc.press("w")
kc.release(Key.alt_l)
kc.release(Key.ctrl_l)
kc.release(Key.shift)
kc.release("w")
HOTKEYS = [HotKey(HotKey.parse("<shift>+<ctrl>+<alt>+" + "e"), hotkey_e)]
def on_press(key):
if key == Key.esc:
listener.stop()
print(str(key))
for hotkey in HOTKEYS:
hotkey.press(listener.canonical(key))
def on_release(key):
for hotkey in HOTKEYS:
hotkey.release(listener.canonical(key))
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
I also tried
with kc.pressed(Key.alt):
with kc.pressed(Key.ctrl):
with kc.pressed(Key.shift):
kc.press("w")
kc.release("w")
Edit: I found out that if I the OBS windows is the active window the HotKey is working. But it should run in background
My solution was to set some sleep between the key presses
def hotkey_e():
kc.press(Key.alt_l)
time.sleep(0.05)
kc.press(Key.ctrl_l)
time.sleep(0.05)
kc.press(Key.shift)
time.sleep(0.05)
kc.press("w")
time.sleep(0.05)
kc.release(Key.alt_l)
time.sleep(0.05)
kc.release(Key.ctrl_l)
time.sleep(0.05)
kc.release(Key.shift)
time.sleep(0.05)
kc.release("w")