[英]Record (get) mouse click position while key is pressed and stop recording when same key is released in python
[英]record while key is pressed, stop when key is released
我目前正在尝试制作一个简单的Python脚本来创建语音数据。
该脚本的想法是,按住并按住某个键时,使用pyaudio
开始录制,而释放该键时,则停止录制。
我目前对应该如何实现while key hold / stop at release mechanism.
有些困惑while key hold / stop at release mechanism.
我找到了这个库keyboard
,但是它是否包含这种形式的机制却无法理解?
根据这个代码库“键盘”的来源,它确实提供了这样的机制,以检测是否有键按下当前。 因此,您可以进行一轮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.