Global hotkey for stopping windows script

I am working on a script that takes control of my mouse and runs during a simple endless while loop.

def main(): while True: do_mouse_stuff() 

Due to mouse control, it is a pain to click on a python window and press ctrl-c, so I was looking for a way to implement a global hotkey in windows. I am also relative Python noob, so I probably missed the obvious answer. The material I found:

pyhk - the closest I got, but for some reason this module does unpleasant things on my computer (maybe something I'm doing wrong), it introduces a big mouse delay, a complete input lock, all kinds of things, m is not smart enough, to deal with.

pyHook - after the tutorial it works fine, but the infinite stream of messages and my while loop work exclusively, and I don’t know, t figured out how to make it work.

Another method - I found this method, but I have the same problem as pyHook, the try loop and my while loop cannot coexist.

I tried to figure out how to integrate my cycle into these examples, rather than maintaining a separate cycle, but I could not do this work, again, probably due to my lack of ability. Can anyone fix me how to make this work?

+4
source share
1 answer

Perhaps using msvcrt ? I'm not sure if this is β€œglobal,” and I can't check it right now, unfortunately, but here is an example of finding the Escape key (taken from this question ) integrated with your keyboard:

 import msvcrt def main(): while True: do_mouse_stuff() # Check if `Esc` has been pressed if msvcrt.kbhit() and msvcrt.getch() == chr(27).encode(): aborted = True break 
-1
source

All Articles