[英]record while key is pressed, stop when key is released
I am currently trying to make a simple python script for creating speech data. 我目前正在尝试制作一个简单的Python脚本来创建语音数据。
The idea with the script is that is starts recording using pyaudio
, when a key is pressed and hold down, and stops recording when the key is released. 该脚本的想法是,按住并按住某个键时,使用
pyaudio
开始录制,而释放该键时,则停止录制。
I am currently a bit confused on how i should implement the while key hold / stop at release mechanism.
我目前对应该如何实现
while key hold / stop at release mechanism.
有些困惑while key hold / stop at release mechanism.
i found this library keyboard
, but can't make sense whether it incorporates this form of mechanism? 我找到了这个库
keyboard
,但是它是否包含这种形式的机制却无法理解?
According to this code in library "keyboard"'s source, it do provide such mechanism to detect if a key is currently pressed. 根据这个代码库“键盘”的来源,它确实提供了这样的机制,以检测是否有键按下当前。 so you can just do a while loop to check whether user has released that key.
因此,您可以进行一轮while循环来检查用户是否已释放该密钥。
#/usr/bin/python
# file: __init__.py
# ...
def is_pressed(key):
"""
Returns True if the key is pressed.
is_pressed(57) -> True
is_pressed('space') -> True
is_pressed('ctrl+space') -> True
"""
_listener.start_if_necessary()
if is_number(key):
return key in _pressed_events
elif len(key) > 1 and ('+' in key or ',' in key):
parts = canonicalize(key)
if len(parts) > 1:
raise ValueError('Cannot check status of multi-step combination ({}).'.format(key))
return all(is_pressed(part) for part in parts[0])
else:
for event in _pressed_events.values():
if matches(event, key):
return True
return False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.