Search code examples
pythonkeyboardhotkeys

Python keyboard module add_hotkey doesn't work


I'm trying to run some code from someone else's GitHub repo, but it wouldn't work. Eventually I figured out that the "keyboard.add_hotkey" function isn't working properly on my PC (W10). I wrote this small test script to isolate the issue:

import keyboard

keyboard.write('hehe')
keyboard.add_hotkey('a', lambda: keyboard.write('test'))
keyboard.add_hotkey('ctrl + shift + a', print, args =('input', 'hotkey'))
keyboard.hook_key('q', lambda: print(1))

keyboard.wait()

The write function works perfectly fine, but all methods of binding a function to a key fails to execute when entering the required input. I ran the exact same code on my laptop (Linux Mint) and it ran perfectly fine. By the way, I also tried "while True: pass" instead of "keyboard.wait()", but that yielded the exact same results on both machines, Linux working fine and W10 not so much.

EDIT: I tried to run this code on a Linux VM and the code worked perfectly fine, so this must have something to do with my instance of Windows and isn't some weird (physical) keyboard issue.


Solution

  • Finally figured it out. Had to turn off one of the starting services (SentryBay) in the boot options. It was conflicting with the keyhooks.