您现在的位置是:首页 > Python 当前栏目 python键盘、鼠标控制 Python 文件 日志 代码 2023-03-02 11:00:47 时间 python键盘、鼠标控制 使用pyhook模块可以很快地完成键盘及鼠标事件捕获。 网站上提供了个使用的例子,改写了下,将信息记录到文件中,本来想使用python的logging模块,但测试时发现,因为鼠标事件频率太高,导致写时报I/O错误的异常,所以使用了自己写文件记录日志的方式。 代码: Python代码 1. \#!/usr/bin/env python 2. \# -\*- coding: utf-8 -\*- 3. 4. 5. import pythoncom 6. import pyHook 7. import time 8. 9. 10. def onMouseEvent(event): 11. "处理鼠标事件" 12. fobj.writelines('-' \* 20 + 'MouseEvent Begin' + '-' \* 20 + '\\n') 13. fobj.writelines("Current Time:%s\\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())) 14. fobj.writelines("MessageName:%s\\n" % str(event.MessageName)) 15. fobj.writelines("Message:%d\\n" % event.Message) 16. fobj.writelines("Time\_sec:%d\\n" % event.Time) 17. fobj.writelines("Window:%s\\n" % str(event.Window)) 18. fobj.writelines("WindowName:%s\\n" % str(event.WindowName)) 19. fobj.writelines("Position:%s\\n" % str(event.Position)) 20. fobj.writelines('-' \* 20 + 'MouseEvent End' + '-' \* 20 + '\\n') 21. return True 22. 23. 24. def onKeyboardEvent(event): 25. "处理键盘事件" 26. fobj.writelines('-' \* 20 + 'Keyboard Begin' + '-' \* 20 + '\\n') 27. fobj.writelines("Current Time:%s\\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())) 28. fobj.writelines("MessageName:%s\\n" % str(event.MessageName)) 29. fobj.writelines("Message:%d\\n" % event.Message) 30. fobj.writelines("Time:%d\\n" % event.Time) 31. fobj.writelines("Window:%s\\n" % str(event.Window)) 32. fobj.writelines("WindowName:%s\\n" % str(event.WindowName)) 33. fobj.writelines("Ascii\_code: %d\\n" % event.Ascii) 34. fobj.writelines("Ascii\_char:%s\\n" % chr(event.Ascii)) 35. fobj.writelines("Key:%s\\n" % str(event.Key)) 36. fobj.writelines('-' \* 20 + 'Keyboard End' + '-' \* 20 + '\\n') 37. return True 38. 39. 40. 41. 42. if \_\_name\_\_ == "\_\_main\_\_": 43. ''' 44. Function:操作SQLITE3数据库函数 45. Input:NONE 46. Output: NONE 47. author: socrates 48. blog:[http://blog.csdn.net/dyx1024][http_blog.csdn.net_dyx1024] 49. date:2012-03-1 50. ''' 51. 52. \#打开日志文件 53. file\_name = "D:\\\\hook\_log.txt" 54. fobj = open(file\_name, 'w') 55. 56. 57. \#创建hook句柄 58. hm = pyHook.HookManager() 59. 60. 61. \#监控键盘 62. hm.KeyDown = onKeyboardEvent 63. hm.HookKeyboard() 64. 65. 66. \#监控鼠标 67. hm.MouseAll = onMouseEvent 68. hm.HookMouse() 69. 70. \#循环获取消息 71. 72. \#关闭日志文件 73. fobj.close() *复制代码* 测试: Plain 代码 1. \--------------------Keyboard Begin-------------------- 2. Current Time:Thu, 01 Mar 2012 15:07:01 3. MessageName:key down 4. Message:256 5. Time:6376015 6. Window:66926 7. WindowName:淘宝网 - 淘我喜欢! - Windows Internet Explorer 8. Ascii\_code: 103 9. Ascii\_char:g 10. Key:G 11. \--------------------Keyboard End-------------------- 12. \--------------------MouseEvent Begin-------------------- 13. Current Time:Thu, 01 Mar 2012 15:07:01 14. MessageName:mouse move 15. Message:512 16. Time\_sec:6376078 17. Window:132584 18. WindowName:None 19. Position:(724, 344) 20. \--------------------MouseEvent End-------------------- 21. \--------------------MouseEvent Begin-------------------- 22. Current Time:Thu, 01 Mar 2012 15:07:01 23. MessageName:mouse move 24. Message:512 25. Time\_sec:6376109 26. Window:132584 27. WindowName:None 28. Position:(724, 344) 29. \--------------------MouseEvent End-------------------- 30. \--------------------Keyboard Begin-------------------- 31. Current Time:Thu, 01 Mar 2012 15:07:01 32. MessageName:key down 33. Message:256 34. Time:6376625 35. Window:66926 36. WindowName:淘宝网 - 淘我喜欢! - Windows Internet Explorer 37. Ascii\_code: 111 38. Ascii\_char:o 39. Key:O 40. \--------------------Keyboard End-------------------- 41. \--------------------Keyboard Begin-------------------- 42. Current Time:Thu, 01 Mar 2012 15:07:02 43. MessageName:key down 44. Message:256 45. Time:6376781 46. Window:66926 47. WindowName:淘宝网 - 淘我喜欢! - Windows Internet Explorer 48. Ascii\_code: 111 49. Ascii\_char:o 50. Key:O 51. \--------------------Keyboard End-------------------- 52. \--------------------Keyboard Begin-------------------- 53. Current Time:Thu, 01 Mar 2012 15:07:02 54. MessageName:key down 55. Message:256 56. Time:6377000 57. Window:66926 58. WindowName:淘宝网 - 淘我喜欢! - Windows Internet Explorer 59. Ascii\_code: 103 60. Ascii\_char:g 61. Key:G 62. \--------------------Keyboard End-------------------- 63. \--------------------Keyboard Begin-------------------- 64. Current Time:Thu, 01 Mar 2012 15:07:02 65. MessageName:key down 66. Message:256 67. Time:6377140 68. Window:66926 69. WindowName:淘宝网 - 淘我喜欢! - Windows Internet Explorer 70. Ascii\_code: 108 71. Ascii\_char:l 72. Key:L 73. \--------------------Keyboard End-------------------- 74. \--------------------Keyboard Begin-------------------- 75. Current Time:Thu, 01 Mar 2012 15:07:02 76. MessageName:key down 77. Message:256 78. Time:6377187 79. Window:66926 80. WindowName:淘宝网 - 淘我喜欢! - Windows Internet Explorer 81. Ascii\_code: 101 82. Ascii\_char:e 83. Key:E 84. \--------------------Keyboard End-------------------- 85. \--------------------MouseEvent Begin-------------------- 86. Current Time:Thu, 01 Mar 2012 15:07:07 87. MessageName:mouse move 88. Message:512 89. Time\_sec:6382093 90. Window:132584 91. WindowName:None 92. Position:(725, 344) 93. \--------------------MouseEvent End-------------------- 94. *复制代码* 由上面的记录可以看出,当时我通过IE上淘宝,并且输入了google这个单词,有可能这是商品名,用户名,或者密码,呵呵。 查看Ascii\_char字段即可看出输入的字母。如果没有解析出来,可通过Ascii\_code字段的值到ASCII表中查找即可 [http_blog.csdn.net_dyx1024]: http://blog.csdn.net/dyx1024 本文地址: python键盘、鼠标控制 相关文章 基于Amazon CloudFront Extensions开发的CDN监控API Python 运算符 Python运算符 python colormap python自学 Python运算符 python 安装 python安装 python列表 Python爬虫 python知识 python-----贪心 python 作业 Python 简介 Python简介 python-Mcmc 04_python Python Challenge python list Python简介