環境:win7 + Python3.6
程序代碼使用了pyHook處理鍵盤快捷鍵事件,普通的鍵盤輸入程序正常工作。一旦使用切換窗口快捷鍵:ALT + Tab,程序關閉,控制台提示錯誤:
TypeError: KeyboardSwitch() missing 8 required positional arguments: 'msg', 'vk_code', 'scan_code', 'ascii', 'flags', 'time', 'hwnd', and 'win_name'
有兩篇文章比較有幫助:
Python的KeyboardSwitch() missing 8解決方法: https://blog.csdn.net/u014563989/article/details/77247659
pyHook在Python3上面的問題? https://www.zhihu.com/question/58201895
解決方案:pyHook本身的bug,需要升級到PyHook3。
執行命令:pip install PyHook3
報錯:error: command 'swig.exe' failed: No such file or directory
這是在提示swig.exe沒有被安裝。
SWIG(http://www.swig.org/)指的是簡單包安裝器和接口生成器( simple wrapper and interface generator ),是一個適用於多種語言的工具。
一方面,它讓你能夠使用C或C++編寫擴展代碼;另一方面,它自動包裝這些代碼,讓你能夠在Tcl、Python、Perl、Ruby和Java等高級語言中使用它們。
可以在這里下載:https://sourceforge.net/projects/swig/ ,下載有點慢,多試幾次。 解壓縮swigwin-3.0.12.zip,將其路徑設置到path環境變量中,重新執行,安裝成功。
繼續執行,沒有報swig的錯誤,但還繼續報錯:
這里說的是需要安裝: Microsoft Visual C++ Build Tools 2015 ,下載這個鏈接:http://go.microsoft.com/fwlink/?LinkId=691126 一路默認安裝,程序不再報錯,安裝成功。
修改python代碼:import PyHook3 as pyHook ,重新執行,程序不再報錯。Great!