asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.45k stars 1.26k forks source link

Can't get add_hotkey to work #721

Closed RobertvdLeeuw closed 2 years ago

RobertvdLeeuw commented 2 years ago

I'm trying to run some code from someone else's GitHub repo, but it wouldn't work. Eventually I figured out that the issue lies at the add_hotkey function, which that project imports from this module. The function never fires 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: time.sleep(0.1)" instead of "keyboard.wait()", but that yielded the exact same results on both machines, Linux working fine and W10 not so much. I also tried to run this code on a Linux VM on my PC 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 or anything of the sort.

RobertvdLeeuw commented 2 years ago

Well, after a lot of testing it seemed to be one of the services in the boot options of my W10 instance that was messing up.