Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] 使用 Python 监控键盘、鼠标输入 #41

Open
yangruihan opened this issue Jun 15, 2021 · 0 comments
Open

[Python] 使用 Python 监控键盘、鼠标输入 #41

yangruihan opened this issue Jun 15, 2021 · 0 comments

Comments

@yangruihan
Copy link
Owner

yangruihan commented Jun 15, 2021

使用 Python 监控键盘、鼠标输入

这里使用PyHook库实现功能

安装

Windows 可以在这里下载对应版本

如果找不到对应版本,也可以选择 pyWinhook

使用方法

#创建hook句柄
hm = pyHook.HookManager()

#监控键盘
hm.KeyDown = onKeyboardEvent # 设置键盘事件回调
hm.HookKeyboard() # 开始监听
hm.UnhookKeyboard()  # 停止监听

#监控鼠标
hm.MouseAll = onMouseEvent # 设置鼠标事件回调
hm.HookMouse() # 开始监听
hm.UnhookMouse() # 停止监听

完整实例

#!/usr/bin/env python
# -*- coding: utf-8 -*-


import pythoncom
import pyHook
import time


def onMouseEvent(event):
    "处理鼠标事件"
    fobj.writelines('-' * 20 + 'MouseEvent Begin' + '-' * 20 + '\n')
    fobj.writelines("Current Time:%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))
    fobj.writelines("MessageName:%s\n" % str(event.MessageName))
    fobj.writelines("Message:%d\n" % event.Message)
    fobj.writelines("Time_sec:%d\n" % event.Time)
    fobj.writelines("Window:%s\n" % str(event.Window))
    fobj.writelines("WindowName:%s\n" % str(event.WindowName))
    fobj.writelines("Position:%s\n" % str(event.Position))
    fobj.writelines('-' * 20 + 'MouseEvent End' + '-' * 20 + '\n')
    return True


def onKeyboardEvent(event): 
    "处理键盘事件"   
    fobj.writelines('-' * 20 + 'Keyboard Begin' + '-' * 20 + '\n')
    fobj.writelines("Current Time:%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))
    fobj.writelines("MessageName:%s\n" % str(event.MessageName))
    fobj.writelines("Message:%d\n" % event.Message)
    fobj.writelines("Time:%d\n" % event.Time)
    fobj.writelines("Window:%s\n" % str(event.Window))
    fobj.writelines("WindowName:%s\n" % str(event.WindowName))
    fobj.writelines("Ascii_code: %d\n" % event.Ascii)
    fobj.writelines("Ascii_char:%s\n" % chr(event.Ascii))
    fobj.writelines("Key:%s\n" % str(event.Key))
    fobj.writelines('-' * 20 + 'Keyboard End' + '-' * 20 + '\n')
    return True




if __name__ == "__main__": 
    '''
    Function:操作SQLITE3数据库函数
    Input:NONE
    Output: NONE
    author: socrates
    blog:http://blog.csdn.net/dyx1024
    date:2012-03-1
    '''  
        
    #打开日志文件
    file_name = "D:\\hook_log.txt"
    fobj = open(file_name,  'w')       


    #创建hook句柄
    hm = pyHook.HookManager()


    #监控键盘
    hm.KeyDown = onKeyboardEvent
    hm.HookKeyboard()


    #监控鼠标
    hm.MouseAll = onMouseEvent
    hm.HookMouse()
    
    #循环获取消息
    pythoncom.PumpMessages()
    
    #关闭日志文件
    fobj.close() 

输出结果

参考链接

@yangruihan yangruihan changed the title [Python] 使用 PyHook 监控键盘、鼠标输入 [Python] 使用 Python 监控键盘、鼠标输入 Jun 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant