【发布时间】:2018-07-23 18:38:43
【问题描述】:
我编辑了我的问题,因为现在当我在 Pycharm (IN Powershell) 之外运行我的代码时,键盘中断工作正常,但现在我正在努力终止 Escape 按键上的代码。
from PIL import ImageGrab
import numpy as np
import cv2
import ctypes
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
def record_screen():
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('ResultFile.avi', fourcc, 25.0, screensize)
while True:
try:
img = ImageGrab.grab()
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
out.write(frame)
print('recording....')
except KeyboardInterrupt:
break
out.release()
cv2.destroyAllWindows()
record_screen()
【问题讨论】:
-
为什么不使用 Ctrl+C? (这会引发
KeyboardInterrupt) -
你能演示一下吗?
-
我试图使用转义键来终止录制,但它根本不会终止循环
-
当一个程序正在运行并且用户按下 Ctrl+C 时,
KeyboardInterrupt就会被触发。在 Python IDE 中试试这个:while True: print(1)。按回车,等待几秒钟,然后按 Ctrl+C 看看会发生什么。