[英]How to make on_release events truly non-blocking inpynput?
My use case is, I'm using pynput/Python to make a reaction time registration application.我的用例是,我正在使用 pynput/Python 来制作反应时间注册应用程序。 The essence is that someone will press a control key to mark the first time epoch, and then the reactor hits any other key, and the time difference between the two keystrokes will be computed and displayed.
本质是有人会按下一个控制键来标记第一个纪元,然后反应器击中任何其他键,然后计算并显示两次击键之间的时间差。
In order to alert the reactor to press a key, I am playing a tone that is about 1 second long when the initiator releases their key.为了提醒反应堆按下一个键,当发起者释放他们的键时,我正在播放大约 1 秒长的音调。 However, since reaction time is on the order of milliseconds, I don't want them to have to wait for the tone to complete to press the button as they currently must, or even initiate a new set of two keystrokes.
但是,由于反应时间大约为毫秒,我不希望他们必须等待音调完成才能按当前必须的按钮,甚至不希望启动一组新的两次击键。 As soon as the sound fires, you should be free to press more keys.
一旦声音响起,您应该可以自由按更多键。
In this sense, I can't figure out how to make the events non-blocking.从这个意义上说,我无法弄清楚如何使事件成为非阻塞的。 Using the
listener.start()
paradigm doesn't seem to help me here.在这里使用
listener.start()
范例似乎对我没有帮助。 Code is below:代码如下:
from pynput import keyboard
from datetime import datetime
from playsound import playsound
INITIATE = {keyboard.Key.enter}
def on_press(key):
global initiated_time
pressed_time = None
reaction_time = None
if key in INITIATE:
initiated_time = datetime.now()
return initiated_time
else:
pressed_time = datetime.now()
reaction_time = pressed_time - initiated_time
reaction_time_ms = int(reaction_time.seconds) * 1000 + int(reaction_time.microseconds)/1000
print('Reaction time %s ms' % (reaction_time_ms))
def on_release(key):
if key in INITIATE:
playsound('bong.mp3')
print('')
with keyboard.Listener(
on_press=on_press,
on_release=on_release
) as listener:
listener.join()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.