5

I'm working on a script that grabs control of my mouse and runs within a simple infinite while loop.

def main():
    while True:
        do_mouse_stuff()

Because of the mouse control, it's a pain to click on the python window and hit ctrl-c, so I've been looking for a way to implement a global hotkey in windows. I'm also a relative Python noob so I've probably missed an obvious answer. Stuff I've found:

pyhk - the closest I've gotten, but this module does nasty things to my computer for some reason (probably something I'm doing wrong), it introduces major mouse lag, complete input lockout, all kinds of stuff I'm not smart enough to deal with.

pyHook - Followed the tutorial, works fine but the infinite running message pump and my while loop appear to run exclusively and I haven't figured out how to make it work.

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

I've tried to figure out how to integrate my loop into these examples rather than maintaining a separate loop but I've not been able to make that work, again likely due to my noobishness. Would someone be able to straighten me out on how to make this work?

Mark
  • 183
  • 5
  • If you want to implement a message loop on your own, it has to be ONE and only one, that dispatches all events, be they from the keyboard, from the mouse or from whereever. That's why pyHook didn't seem to work for you. – Hyperboreus Dec 13 '12 at 07:00
  • This is precisely the problem, I don't know how to make it one loop as the messagePump and try loop seem exclusive of whatever I try to add to them. As soon as I get my code working, the global hotkeys won't work, and as soon as the global hotkeys work, the script sits there waiting for hotkeys rather than doing my functions. – Mark Dec 13 '12 at 07:54
  • Why not use something that already has a working message queue, like e.g. a qt application. – Hyperboreus Dec 13 '12 at 16:38
  • This seems like adding a lot of complexity, especially for me who is new to Python (and will be new to Qt as well). I will investigate though. – Mark Dec 13 '12 at 17:26
  • https://github.com/boppreh/keyboard – Andrew Aug 02 '17 at 20:56

1 Answers1

-1

Perhaps using msvcrt? I'm not sure if it's "global", and I can't test it right now, unfortunately, but here's an example of detecting the Escape key (taken from this question), integrated with your keyboard stuff:

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
Community
  • 1
  • 1
voithos
  • 68,482
  • 12
  • 101
  • 116
  • I wasn't able to make msvcrt work at all, I looked around for some more examples, it seems like you were bang on in implementation but it doesn't work for whatever reason on my system. Read [this](http://effbot.org/librarybook/msvcrt.htm), tried [this](http://effbot.org/librarybook/msvcrt-example-1.py), but all I got output was ÿ and never the keys I was pressing. – Mark Dec 13 '12 at 07:52
  • 1
    @user1900018: After some more experimentation, it seems that `msvcrt` doesn't handle global keypresses. Using the example code, hitting `Esc` successfully breaks out from the loop, but only if the window running Python has focus. – voithos Dec 18 '12 at 19:22