【发布时间】:2018-11-15 22:34:53
【问题描述】:
我正在尝试使用库 pyautogui 在 python3 中模拟 ctrl+c 键盘事件。不幸的是,该库不会生成此事件。有没有其他方法可以生成这个?
以下代码不起作用,
from pyautogui import hotkey
hotkey('ctrl', 'c')
我想为以下代码执行此任务。该代码可以录制任意持续时间的实时音频,并且可以通过按“Ctrl+c”随时停止录制。我想生成这个事件,以便我可以添加一些额外的功能。
import os
import sys
import time
import queue
import soundfile as f
import sounddevice as sd
def callback(indata, frames, time, status):
"""
This function is called for each audio block from the record function.
"""
if status:
print(status, file=sys.stderr)
q.put(indata.copy())
def record(filename):
try:
# Make sure the file is open before recording begins
with sf.SoundFile(filename, mode='x', samplerate=48000, channels=2, subtype="PCM_16") as file:
with sd.InputStream(samplerate=48000, channels=2, callback=callback):
print('START')
print('#' * 80)
"""
Here is the place I want to generate the event (Ctrl+c)
after n minutes
"""
print('press Ctrl+c to stop the recording')
while True:
file.write(q.get())
except KeyboardInterrupt:
print('The recording is over.')
if __name__ == "__main__":
q = queue.Queue()
record('filename.wav')
【问题讨论】:
标签: python-3.x keyboard-events pyautogui