当前位置:网站首页>pyhook3简单应用(1)--实现截图保存功能

pyhook3简单应用(1)--实现截图保存功能

2022-08-09 13:07:00 论一个测试的养成

1.安装
【1】首先安装swging :http://www.swig.org/download.html
【2】安装vc++相关:https://blog.csdn.net/qq_38161040/article/details/88203864
【3】pip3 install pyHook3
2.应用

from  PIL import ImageGrab
import pythoncom
import PyHook3
import sys
from ctypes import *

QUIT_WORD="EXIT"
QUIT_CONT=QUIT_WORD
N = 0
current_window = None

def OnKeyboardEvent(event):
    global current_window, QUIT_WORD, QUIT_CONT, path, N
    # 检测目标窗口是否转移(换了其他窗口就监听新的窗口)
    if event.Window != current_window:
        current_window = event.Window

        # 所以调用底层API喊来获取窗口标题
        windowTitle = create_string_buffer(512)
        windll.user32.GetWindowTextA(event.Window,byref(windowTitle),512)
        windowName = windowTitle.value.decode('gbk')
        print("窗口名:%s" % windowName)
        print("窗口ID:%s" % event.Window)
    # 检测击键是否常规按键(非组合键等)
    if event.Key == "Escape":
        im = ImageGrab.grab()
        filename = str(N) + '.png'
        im.save(filename)
        N += 1
    # 判断退出监听指令符
    if (event.Key == QUIT_WORD[0]):
        QUIT_WORD = QUIT_WORD[1:]
        if (len(QUIT_WORD) == 0):
            sys.exit()
            return False
    else:
        QUIT_WORD = QUIT_CONT
    # 循环监听下一个击键事件
    return True

if __name__ == '__main__':

    ph = PyHook3.HookManager()
    ph.KeyDown = OnKeyboardEvent   #绑定键盘事件
    # 注册hook并执行
    ph.HookKeyboard()
    pythoncom.PumpMessages()

原网站

版权声明
本文为[论一个测试的养成]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44517891/article/details/106762391