win7+py2.7+pyHook的安装,并解决Python version 2.7 required,which was notfound in the registry问题
- 1.下载 .exe 文件,网址链接如下: http://nchc.dl.sourceforge.net/project/pyhook/pyhook/1.5.1/pyHook-1.5.1.win32-py2.7.exe
- 2.点击安装.exe,在pycharm中 import pyHook,若没问题,则安装成功
- 3.要是报错Python version 2.7 required,which was not found in the registry,则进行如下操作:
- A: win+R键,输入regedit 打开注册表编辑器
- B:找到HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\,看看是否有文件夹InstallPath,要是没有,则新建
- C: 新建InstallPath文件夹,如下:
- D:双机右边的“默认”,并填入python2.7的安装途径,如下所示:
- E:点击确定,然后导入 import pyHook
#!/usr/bin/env python # -*- coding: utf-8 -*- # @version : 0.0.1 # @File : regpy.py # @Time : 2018/5/21 0021 上午 9:07 # @Site : # @Software: PyCharm # @Author : singebogo # @Author_email: singbogo@163.com # @description: # # script to register Python 2.0 or later for use with win32all # and other extensions that require Python registry settings # # written by Joakim Loew for Secret Labs AB / PythonWare # # source: # http://www.pythonware.com/products/works/articles/regpy20.htm # # modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html ''' 解决注册问题,pywin32安装存在问题 ''' import sys from _winreg import * # tweak as necessary version = sys.version[:3] installpath = sys.prefix regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version) installkey = "InstallPath" pythonkey = "PythonPath" pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % ( installpath, installpath, installpath ) def RegisterPy(): try: reg = OpenKey(HKEY_CURRENT_USER, regpath) except EnvironmentError as e: try: reg = CreateKey(HKEY_CURRENT_USER, regpath) SetValue(reg, installkey, REG_SZ, installpath) SetValue(reg, pythonkey, REG_SZ, pythonpath) CloseKey(reg) except: print "*** Unable to register!" return print "--- Python", version, "is now registered!" return if (QueryValue(reg, installkey) == installpath and QueryValue(reg, pythonkey) == pythonpath): CloseKey(reg) print "=== Python", version, "is already registered!" return CloseKey(reg) print "*** Unable to register!" print "*** You probably have another Python installation!" if __name__ == "__main__": RegisterPy()
如果运行报错ImportError: DLL load failed: %1 不是有效的 Win32 应用程序,请参考这篇博客:
http://blog.youkuaiyun.com/AcSuccess/article/details/77448397
首先安装第三方库成了一个问题,查了很多资料,也没有解决pyHook库的安装问题。
在安装pyHook库之前,首先安装pywin32,这个库很好装,大家搜一下教程即可。
接下来需要安装pyHook,这个库我装了几次,都没有装上。最后找到了解决办法
第一个是python2.7 32位下载地址,第二个是python2.7 64位下载地址。我的是64位,下载第二个
下载完成后,发现无法打开,这是用压缩工具打开,比如winrar,打开后解压到某目录下,就会出现pyHook的文件,将这个文件拷贝到
python27的安装的特定目录下python27\Lib\site-packages,然后导入pyHook就可以运行了。
如果安装了pyhook 则需要进行卸载,在控制面板中程序卸载即可,
C:\Users\Administrator>pip install C:\Users\Administrator\Desktop\pyHook-1.5.1-c
p27-cp27m-win_amd64.whl
Processing c:\users\administrator\desktop\pyhook-1.5.1-cp27-cp27m-win_amd64.whl
Installing collected packages: pyHook
Successfully installed pyHook-1.5.1
下面是一个简单的鼠标键盘监控器的小教程。
- #!/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__":
- #打开日志文件
- file_name = "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()
--------------------MouseEvent Begin-------------------- Current Time:Mon, 21 May 2018 01:44:11 MessageName:mouse move Message:512 Time_sec:15899309 Window:132330 WindowName:pyt - [D:\pyt] - ...\picc\moudle\Kadjust.py - PyCharm 2017.2.4 Position:(49, 789)