boppreh / keyboard

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

long key press will trigger multi onPress event. can I only trigger once when a key press? #494

Closed solookin closed 3 years ago

solookin commented 3 years ago
def onPress(e):
    print("on press.....")

if __name__ == '__main__':
    keyboard.on_press(onPress)
    keyboard.wait()
boppreh commented 3 years ago

The events themselves are currently indistinguishable. There might be an easier way, but a trivial fix is to set a flag that is cleared on release.

is_first_press = True
def onPress(e):
    global is_first_press
    if is_first_press:
            print("on press.....")
    is_first_press = False

def onRelease(e):
    global is_first_press
    is_first_press = True

if __name__ == '__main__':
    keyboard.on_press(onPress)
    keyboard.on_release(onRelease)
    keyboard.wait()

This might be a useful option in the on_press/on_release functions, but I'm not sure how to name it. Suggestions welcome.

solookin commented 3 years ago

@boppreh I had test you code, but found that onRelease function not work. example:

def onRelease(e):
    global is_first_press
    print("on release...")
    is_first_press = True

on release... will not print.

solookin commented 3 years ago

@boppreh I fix it like this. and everything runs ok. thanks!

pressedKeys = set()  

def KeyEvent(e):
    global pressedKeys
    name = e.name
    event_type = e.event_type
    if event_type == 'down':
        if name in pressedKeys:
            return
        pressedKeys.add(name)
        # do something...
    elif event_type == 'up':
        pressedKeys.remove(name)

if __name__ == '__main__':
  keyboard.hook(KeyEvent)
  keyboard.wait()