U
U
underalmaty2019-03-28 03:39:10
Python
underalmaty, 2019-03-28 03:39:10

The hotkey catching function hangs the application, how to avoid it?

It is necessary to catch pressing the hotkey when the application is not in focus. The Internet gave the only instruction, it, in principle, works, the done print is displayed, but the program freezes and does not react to anything other than pressing the hotkey. And it hangs as usual - with painting over and a Windows request to stop or wait. However, all this time, pressing works fine even with a frozen program.

COMBINATIONS = [
            {keyboard.Key.shift, keyboard.KeyCode(char='~')},
            {keyboard.Key.shift, keyboard.KeyCode(char='`')}
        ]

        current = set()

        def on_press(key):
            if any([key in COMBO for COMBO in COMBINATIONS]):
                current.add(key)
                if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
                    print('done')

        with keyboard.Listener(on_press=on_press) as listener:
            listener.join()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-03-28
@underalmaty

You can't stop the event loop, which listener.join()is exactly what it does. Run the handler in a separate one QThreadand send signals from it to the main thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question