For reasons I don't understand, this ...
#!/usr/bin/env python3
from pynput import keyboard
from time import perf_counter, sleep
def on_press(key):
print(perf_counter()-start_time,'press',key)
def on_release(key):
print(perf_counter()-start_time,'release',key)
k_controller = keyboard.Controller()
with keyboard.Listener(on_press=on_press,on_release=on_release) as k_listener:
start_time = perf_counter()
sleep(.5)
k_controller.press(keyboard.Key.shift)
sleep(.2)
k_controller.release(keyboard.Key.shift)
... does this:
0.5045563999992737 press Key.shift
0.5057189999988623 press Key.shift
0.7054590469997493 release Key.shift
0.7060685199994623 release Key.shift
That is, the key press & release events are duplicated. How can I prevent this happening?
The use-case is a keystroke record-and-replay utility. It'll play a transcript of keystrokes until an actual physical key press is detected, overwriting the transcript with physical key presses thereafter. But, at the moment, I can't even play the transcript without duplicates as shown above.
question from:
https://stackoverflow.com/questions/65870249/pynput-duplicate-keyboard-events 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…