python 鼠标/键盘 监听事件

1.根据python版本下载对应的 “ PyWinHook ” whl包

2.pip 安装whl包 “ pip install pyWinhook-1.6.2-cp37-cp37m-win_amd64.whl  ”

import pyWinhook
import pythoncom

def onMouseEvent(event):
	# 监听鼠标事件
    print( "MessageName:", event.MessageName)
    print( "Message:", event.Message)
    print( "Time:", event.Time)
    print( "Window:", event.Window)
    print( "WindowName:", event.WindowName)
    print( "Position:", event.Position)
    print( "Wheel:", event.Wheel)
    print( "Injected:", event.Injected)
    print( "---")
    # 返回 True 以便将事件传给其它处理程序
    # 注意,这儿如果返回 False ,则鼠标事件将被全部拦截
    # 也就是说你的鼠标看起来会僵在那儿,似乎失去响应了
    return True

# 监听键盘输入
def onKeyboardEvent(event):
    print(event.Key)
    return True
   
if __name__ == '__main__':
    hookmonitor = pyWinhook.HookManager()
    # 监听所有键盘事件
    hookmonitor.KeyDown = onKeyboardEvent
    hookmonitor.HookKeyboard()

    # 监听所有鼠标事件
    hookmonitor.MouseAll = onMouseEvent
    hookmonitor.HookMouse()
    pythoncom.PumpMessages()

你可能感兴趣的:(python,python,pywinhook,鼠标键盘监听)