gpt4 book ai didi

python - 在 Python 中按 esc 键停止,按任何其他键继续

转载 作者:太空宇宙 更新时间:2023-11-04 04:49:15 24 4
gpt4 key购买 nike

现在借助 raw_input,我可以在每次用户按下 Enter 时调用一个方法。

if __name__ == '__main__':
while True:
raw_input("Press Enter to continue...")
_start()
def _start():
print("HelloWorld")

有一个问题,因为只有Ctrl + C,程序才能停止。如您所见,我让我的程序等待用户按键。

opencv,我发现有类似的需求。

# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break

只是我想按 esc 键退出程序,然后按任何其他键继续。那么有什么办法可以做到这一点吗?

此外

我的操作系统是 OSX。

最佳答案

你可以使用pynput,它更容易使用。

from pynput import keyboard

def _start():
print("HelloWorld")
def on_press(key):
if key == keyboard.Key.esc:
# Stop listener
return False
else:
_start()

# Collect events until released
with keyboard.Listener(
on_press=on_press) as listener:
listener.join()

关于python - 在 Python 中按 esc 键停止,按任何其他键继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48787563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com