Closed solookin closed 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.
@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.
@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()