Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

python - pynput duplicate keyboard events

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...