boppreh / keyboard

Hook and simulate global keyboard events on Windows and Linux.
MIT License
3.8k stars 433 forks source link

on_press and on_release not working together #541

Open ghost opened 2 years ago

ghost commented 2 years ago

I am sorry if I am being stupid but can I use keyboard.on_release(foo) and keyboard.on_press(bar) at the same time? I tried the multiprocessing module but nothing seems to work. I tried doing it like this Process(target=keyboard.on_press(handle_key_down)) keyboard.on_release(handle_key_up) I tried doing it like this too Process(target=keyboard.on_press(handle_key_down)) Process(target=keyboard.on_release(handle_key_up)) in both the above keyboard.on_press() works perfectly but keyboard.on_release() doesn't work.

Please tell me how it can be done.

lovettchris commented 1 year ago

Looking at the code in init.py, isn't this backwards?

def on_press(callback, suppress=False):
    """
    Invokes `callback` for every KEY_DOWN event. For details see `hook`.
    """
    return hook(lambda e: e.event_type == KEY_UP or callback(e), suppress=suppress)

def on_release(callback, suppress=False):
    """
    Invokes `callback` for every KEY_UP event. For details see `hook`.
    """
    return hook(lambda e: e.event_type == KEY_DOWN or callback(e), suppress=suppress)

Why is on_press looking for KEY_UP and on_release looking for KEY_DOWN? That seems backwards?