是否有反正我可以讓我的腳本執行我的一個功能,當腳本運行時點擊Ctrl+c
?如何覆蓋鍵盤中斷? (Python)
6
A
回答
18
看看signal handlers。 CTRL-C對應於SIGINT(posix系統上的信號#2)。
例子:
#!/usr/bin/env python
import signal
import sys
def signal_handler(signal, frame):
print 'You pressed Ctrl+C - or killed me with -2'
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
print 'Press Ctrl+C'
signal.pause()
3
使用在except
塊KeyboardInterrupt exception和調用你的函數。
5
當然。
try:
# Your normal block of code
except KeyboardInterrupt:
# Your code which is executed when CTRL+C is pressed.
finally:
# Your code which is always executed.
相關問題
- 1. 如何覆蓋Android鍵盤
- 2. 軟鍵盤覆蓋
- 3. 鍵盤中斷 - PYTHON
- 4. 如何覆蓋默認鍵盤佈局?
- 5. 如何覆蓋鍵盤ondown按鈕?
- 6. 覆蓋Android原生鍵盤
- 7. 鍵盤覆蓋部分edittext
- 8. Android軟鍵盤覆蓋EditText?
- 9. Revmob橫幅覆蓋鍵盤
- 10. 覆蓋軟鍵盤佈局
- 11. 用python gtk鍵盤中斷?
- 12. Python計數鍵盤中斷
- 13. 如何覆蓋Android中的軟鍵盤按鍵事件?
- 14. 覆蓋emacs中的鍵盤輸入
- 15. 軟鍵盤覆蓋PopupWindow中的EditText
- 16. 如何覆蓋Android鍵盤上的語音輸入鍵?
- 17. 鍵盤中斷
- 18. iOS:如何調整iOS中鍵盤覆蓋的區域高度
- 19. 如何防止鍵盤覆蓋UITextView中的文本?
- 20. 覆蓋收盤
- 21. 全局覆蓋emacs鍵盤綁定
- 22. UITableViewCell單元格是鍵盤覆蓋
- 23. 移動鍵盤覆蓋輸入字段
- 24. Android鍵盤覆蓋文本框問題
- 25. wpf擴展控件與鍵盤覆蓋
- 26. Android的軟鍵盤覆蓋EDITTEXT景觀
- 27. 滾動文本框覆蓋鍵盤
- 28. Android軟鍵盤覆蓋WebView內容
- 29. Android的webview鍵盤覆蓋輸入
- 30. 防止鍵盤覆蓋文本框
請參閱http://stackoverflow.com/questions/4205317/capture-keyboardinterrupt-in-python-without-try-except若干選項。 – DSM