最近嚐試著使用python寫一個keylogger,編寫的過程中使用到UNC放出來的pyHook模組,pyHook提供Windows用戶把一些低階的keyboard & mouse hook功能。當我在try的時候發現除了ALT以外的每個按鍵都可以被正常擷取,唯有ALT鍵每按兩次就會跳出一個runtime warning,訊息如下:
C:\work\code\code_python\svn\nws\keylogger>python keylogger.py
Lmenu
C:\Python25\lib\site-packages\pyHook\HookManager.py:353: RuntimeWarning: tp_comp
are didn't return -1 or -2 for exception
func = self.keyboard_funcs.get(msg)
Traceback (most recent call last):
File "C:\Python25\lib\site-packages\pyHook\HookManager.py", line 353, in Keybo
ardSwitch
func = self.keyboard_funcs.get(msg)
TypeError: an integer is required
Lmenu
Traceback (most recent call last):
File "C:\Python25\lib\site-packages\pyHook\HookManager.py", line 353, in Keybo
ardSwitch
func = self.keyboard_funcs.get(msg)
TypeError: an integer is required
當我trace進HookManager.py之後發現一件相當有趣的問題,那就是msg本身就已經是integer類別,不知道為何還會產生上面的警告訊息,於是我乾脆一不做二不休,將code由
func = self.keyboard_funcs.get(msg)
改為(ALT鍵屬於WM_SYSKEYDOWN)
if msg == HookConstants.WM_SYSKEYDOWN:
func = self.keyboard_funcs.get(HookConstants.WM_SYSKEYDOWN)
else:
func = self.keyboard_funcs.get(msg)
沒想到問題就這麼迎刃而解!
C:\work\code\code_python\svn\nws\keylogger>python keylogger.py
Lmenu
Lmenu
Rmenu
Lmenu
Lmenu
Lmenu
Lmenu
Lmenu
Lmenu
Lmenu
Lmenu
Lmenu
隨後已寫信給作者反應此問題,如此也算是替open source community盡了點心力。
1 comment:
看例子的時候發現如果組合鍵為alt+A的方式可以做如下判斷:
if event.Alt and event.Key == 'J':
return False #block
請問一下pyhook如何block如下組合按鍵:ctrl+alt+F1,謝謝!
Post a Comment