We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
这里使用PyHook库实现功能
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()
输出结果
The text was updated successfully, but these errors were encountered:
No branches or pull requests
使用 Python 监控键盘、鼠标输入
这里使用
PyHook
库实现功能安装
Windows 可以在这里下载对应版本
如果找不到对应版本,也可以选择 pyWinhook 库
使用方法
完整实例
输出结果
参考链接
The text was updated successfully, but these errors were encountered: