arminb
arminb

Reputation: 2093

How to trigger pynput's GlobalHotkeys when all of the keys are released?

With pynput we can listen for global hotkeys like this:

from pynput import keyboard

def on_activate_i():
    print('<ctrl>+<alt>+i pressed')

with keyboard.GlobalHotKeys({
        '<ctrl>+<alt>+i': on_activate_i}) as h:
    h.join()

This will call on_activate_i() when Ctrl+Alt+i is pressed. Now how can we trigger an event when all of the Ctrl+Alt+i keys are pressed and released again?

Upvotes: 1

Views: 2033

Answers (1)

Elton Clark
Elton Clark

Reputation: 156

PyPI has keyboard. I was looking through their git repo and saw this:

def add_hotkey(hotkey, callback, args=(), suppress=False, timeout=1, trigger_on_release=False):
    """
    Invokes a callback every time a hotkey is pressed. The hotkey must
    be in the format `ctrl+shift+a, s`. This would trigger when the user holds
    ctrl, shift and "a" at once, releases, and then presses "s". To represent
    literal commas, pluses, and spaces, use their names ('comma', 'plus',
    'space').
    - `args` is an optional list of arguments to passed to the callback during
    each invocation.
    - `suppress` defines if successful triggers should block the keys from being
    sent to other programs.
    - `timeout` is the amount of seconds allowed to pass between key presses.
    - `trigger_on_release` if true, the callback is invoked on key release instead
    of key press.

So maybe leave the pynput library for one that has the desired behavior built it. FYSA, I have not used either library yet, just doing research on how to implement keyboard hotkeys to control an OBS overlay. Hopefully this is helpful! Now I just need to pick which library I want to start working with for my project...

Upvotes: 2

Related Questions