python:使用pyWinhook监听和记录键鼠操作


hook监听的代码

import pythoncom
import win32gui
import pyWinhook as pyHook


class InputHook:
    def __init__(self):
        self.isListen=False
        self.MouseEvents=[]
        self.KeyEvents = []
        # 创建一个“钩子”管理对象
        self.hm = pyHook.HookManager()
        print('asd1')
        # 监听所有键盘事件
        # hm.KeyDown = onKeyboardEvent
        self.hm.KeyDown = self.ListenKeybroadDown
        print('asd2')
        # 监听所有鼠标事件
        # hm.MouseAll = onMouseEvent
        self.hm.MouseLeftDown = self.ListenMouseLeftDown
        print('asd3')

        # 进入循环,如不手动关闭,程序将一直处于监听状态
        #pythoncom.PumpMessages()


        print('asd4')

    def RegisterMouseEvent(self,event):
        self.MouseEvents.append(event)

    def RegisterKeyEvent(self,event):
        self.KeyEvents.append(event)

    def BeginListen(self):
        self.isListen = True
        # 设置鼠标“钩子”
        self.hm.HookMouse()
        # 设置键盘“钩子”
        self.hm.HookKeyboard()
        win32gui.PumpMessages()

    def StopListen(self):
        self.isListen = False
        # 设置鼠标“钩子”
        self.hm.UnhookMouse()
        # 设置键盘“钩子”
        self.hm.UnhookKeyboard()
        #win32gui.PostQuitMessage()

    def ListenMouseLeftDown(self,event):
        #print('/鼠标点击的位置为:', event.Position)
        if self.isListen:
            for mevent in self.MouseEvents:
                mevent(event)

    def ListenKeybroadDown(self,event):
        #print('/Key:', event.Key)
        if self.isListen:
            for kevent in self.KeyEvents:
                print('ss')
                kevent(event)


def onMouseEvent(event):
    # 监听鼠标事件
    print("MessageName:", event.MessageName)
    print ("Message:", event.Message)
    print ("Time:", event.Time)
    print ("Window:", event.Window)
    print ("WindowName:", event.WindowName)
    print ("Position:", event.Position)
    print ("Wheel:", event.Wheel)
    print ("Injected:", event.Injected)
    print ("---")

def onKeyboardEvent(event):
    # 监听键盘事件
    print( "MessageName:", event.MessageName    )
    print ("Message:", event.Message)
    print ("Time:", event.Time   )
    print ("Window:", event.Window  )
    print ("WindowName:", event.WindowName )
    print ("Ascii:", event.Ascii, chr(event.Ascii))
    print ("Key:", event.Key   )
    print ("KeyID:", event.KeyID  )
    print ("ScanCode:", event.ScanCode )
    print ("Extended:", event.Extended )
    print ("Injected:", event.Injected  )
    print ("Alt", event.Alt )
    print ("Transition", event.Transition )
    print ("---")


'''
def main():
    # 创建一个“钩子”管理对象
    hm = pyHook.HookManager()
    # 监听所有键盘事件
    #hm.KeyDown = onKeyboardEvent
    hm.KeyDown=ListenKeybroadDown
    # 设置键盘“钩子”
    hm.HookKeyboard()
    # 监听所有鼠标事件
    #hm.MouseAll = onMouseEvent
    hm.MouseLeftDown=ListenMouseLeftDown
    # 设置鼠标“钩子”
    hm.HookMouse()
    # 进入循环,如不手动关闭,程序将一直处于监听状态
    #pythoncom.PumpMessages()
    win32gui.PumpMessages()
    
    '''

UI代码

from tkinter import *
import InputHook
import DataMg

global root,t1
global inputhook
global inputList
global dataMg

'''GUI界面'''
def table():
    global root
    root = Tk()
    root.title('mouse-key-hook')
    root.geometry('200x350+400+300')
    root['bg'] = 'blue'
    root.resizable(False, False)
    widgetInit(root)
    root.mainloop()

def widgetInit(root):
    global  t1
    b1 = Button(root, text='开始录入键鼠操作', width=5, command=lambda: ListenMK())
    t1 = Text(root, width=20)  # 信息框
    b1.place(x=150, y=10)
    t1.place(x=0, y=0)

def MouseEvent(event):
    #print('鼠标位置:',event.Position)
    global inputList
    console(t1, '\n鼠标位置:'+str(event.Position[0])+','+str(event.Position[1]))
    inputList.append('m='+str(event.Position[0])+','+str(event.Position[1]))

def KeybEvent(event):
    global dataMg
    global inputhook
    global inputList
    if event.Key=='Escape':
        dataMg.addInputKey('测试输入',inputList)
        inputhook.StopListen()
        return

    console(t1, '\n键盘输入:'+str(event.Key))
    inputList.append('k='+event.Key)


def ListenMK():
    global inputhook
    global inputList
    inputList=[]
    console(t1,'__正在监听操作__')
    if inputhook==None:
        InitInputHook()
    inputhook.BeginListen()

# 消息框显示
def console(t, txt):
    t.insert(END, txt)
    t.focus_force()
    

def InitInputHook():
    global inputhook
    inputhook = InputHook.InputHook()
    inputhook.RegisterMouseEvent(MouseEvent)
    inputhook.RegisterKeyEvent(KeybEvent)

def InitDataMg():
    global dataMg
    dataMg=DataMg.DataMg()

InitDataMg()
InitInputHook()
table()

监听数据存储代码:

import os

import win32gui


class DataMg:

    def __init__(self):
        #self.__init__('C:\\Users\\14486\\Desktop\\测试文件\\左\\新建文本文档.txt')
        self.dataPath = 'C:\\Users\\14486\\Desktop\\测试文件\\左\\新建文本文档.txt'


    def readTxtFile(self):
        fo = open(self.dataPath, "r+",encoding='utf-8')
        result=fo.read()
        fo.close()
        return result

    def writeTxtFile(self,putin):
        fo = open(self.dataPath, "a+",encoding='utf-8')
        fo.write(putin)
        fo.close()

    def addInputKey(self,namenum,inputList):
        str = namenum
        str += ':'
        for s in inputList:
            str = str + s + '|'
        self.writeTxtFile(str + '\n')

    def getInputPot(self,namenum):
        txt=self.readTxtFile()
        list=txt.split('\n')
        for pos in list:
            tmp = pos.split(':')
            if namenum==tmp[0]:
                inputList=tmp[1].split('|')
                return inputList
        return None

main

import win32gui  #安装包名是pypiwin32
from PIL import ImageGrab
import win32con
import win32api
import time

keysCode = {'Enter': 13, 'A': 65, 'B': 66, 'C': 67}

class GameAssist:
    def __init__(self,wdname):

        #取得窗口句柄
        # 从顶层窗口向下搜索主窗口,无法搜索子窗口
        # FindWindow(lpClassName=None, lpWindowName=None)  窗口类名 窗口标题名
        self.handle=win32gui.FindWindow(0,wdname)
        if not self.handle:
            print("找不到")
            exit()

        # 获取窗口位置
        self.left, self.top, self.right, self.bottom = win32gui.GetWindowRect(self.handle)
        # 获取某个句柄的类名和标题
        self.titlename = win32gui.GetWindowText(self.handle)
        self.classname = win32gui.GetClassName(self.handle)
        print(self.titlename)
        print(self.classname)

        # 检查窗口是否最小化,如果是最大化
        if (win32gui.IsIconic(self.handle)):
            #     win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
            print('aa')
            win32gui.ShowWindow(self.handle, win32con.SW_MAXIMIZE)
            time.sleep(0.5)
        #窗口显示在最前面
        win32gui.SetForegroundWindow(self.handle)

        # PyMouse对象,鼠标点击
        #self.mouse = PyMouse()

    #截图
    def ScreenShot(self):
        self.ScreenShot((self.left, self.top, self.right, self.bottom))
    def ScreenShot(self,leftToptorightBottom):
        # 1、用grab函数截图,参数为左上角和右下角左标
        # image = ImageGrab.grab((417, 257, 885, 569))
        image = ImageGrab.grab(leftToptorightBottom)
        return image

#点击事件  左上角的鼠标中点+偏移量
    def clickAndSetZero(self,x1,y1,x2,y2):
        return None
        #p1_x=int(self.screen_left_and_right_point[0] +)

    #模拟鼠标点击
    # 鼠标单击事件
    # 鼠标定位到(30,50)
    #win32api.SetCursorPos([30, 150])
    # 执行左单键击,若需要双击则延时几毫秒再点击一次即可
    #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    # 右键单击
    #win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
    def clickOnPos(self,x,y):
        win32api.SetCursorPos((x, y))
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)

    def setMousePot(self,x,y):
        win32api.SetCursorPos((x, y))

    def inputKey(self,key):
        if key not in keysCode.keys():
            print('inputkey():按键不存在')
            return
        #13是回车
        #win32api.keybd_event(13, 0, 0, 0)
        #win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(keysCode[key], 0, win32con.KEYEVENTF_EXTENDEDKEY, 0)  # 按下enter,第一个元素13为enter的键位码
        win32api.keybd_event(keysCode[key], 0, win32con.KEYEVENTF_KEYUP, 0)  # 松开enter

    def closePlane(self):
        # 关闭窗口
        win32gui.PostMessage(win32gui.findWindow(self.classname, self.titlename), win32con.WM_CLOSE, 0, 0)




if __name__=='__main__':
    wdname='文件资源管理器'#u'[Python] 用python做一个游戏辅助脚本,完整思路 - 蜗牛噢 - 博客园 -- Mozilla Firefox'

    demo=GameAssist(wdname)
    demo.setMousePot(100,100)
    demo.inputKey('A')
    demo.inputKey('A')
    demo.inputKey('A')
    k=input()


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM