I am trying to simulate ctrl+c keyboard event in the python3 using the library pyautogui. Unfortunately, the library doesn't generate this event. Is there any other way to generate this?
The following code is not working,
from pyautogui import hotkey
hotkey('ctrl', 'c')
I want to do this task for the following code. The code can record live audio for the arbitrary duration and the recording can stop anytime by pressing 'Ctrl+c'. I want to generate this event so that I can add some additional features.
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')