python监听键盘输入_Python监听键盘和鼠标事件

#coding=utf-8

from ctypes import *

from ctypes importwintypesimportwin32con

SetWindowsHookEx=windll.user32.SetWindowsHookExA

UnhookWindowsHookEx=windll.user32.UnhookWindowsHookEx

CallNextHookEx=windll.user32.CallNextHookEx

GetMessage=windll.user32.GetMessageA

GetModuleHandle=windll.kernel32.GetModuleHandleW#保存键盘钩子函数句柄

keyboard_hd =None#保存鼠标钩子函数句柄

mouse_hd =NoneclassKBDLLHOOKSTRUCT(Structure):

_fields_=[

('vkCode',c_int),

('scanCode', c_int),

('flags', c_int),

('time', c_int),

('dwExtraInfo', c_uint),

('',c_void_p)

]classPOINT(Structure):

_fields_=[

('x',c_long),

('y',c_long)

]classMSLLHOOKSTRUCT(Structure):

_fields_=[

('pt',POINT),

('hwnd',c_int),

('wHitTestCode',c_uint),

('dwExtraInfo',c_uint),

]defwait_for_msg():

msg=wintypes.MSG()

GetMessage(msg, 0, 0, 0)defkeyboard_pro(nCode, wParam, lParam):"""函数功能:键盘钩子函数,当有按键按下时此函数被回调"""

if nCode ==win32con.HC_ACTION:

KBDLLHOOKSTRUCT_p=POINTER(KBDLLHOOKSTRUCT)

param=cast(lParam,KBDLLHOOKSTRUCT_p)print(param.contents.vkCode)returnCallNextHookEx(keyboard_hd, nCode, wParam, lParam)defstart_keyboard_hook():"""函数功能:启动键盘监听"""HOOKPROTYPE=CFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p))

pointer=HOOKPROTYPE(keyboard_pro)

keyboard_hd=SetWindowsHookEx(

win32con.WH_KEYBOARD_LL,

pointer,

GetModuleHandle(None),

0)

wait_for_msg()defstop_keyboard_hook():"""函数功能:停止键盘监听"""UnhookWindowsHookEx(keyboard_hd)defmouse_pro(nCode, wParam, lParam):"""函数功能:鼠标钩子函数,当有鼠标事件,此函数被回调"""

if nCode ==win32con.HC_ACTION:

MSLLHOOKSTRUCT_p=POINTER(MSLLHOOKSTRUCT)

param=cast(lParam,MSLLHOOKSTRUCT_p)#鼠标左键点击

if wParam ==win32con.WM_LBUTTONDOWN:print("左键点击,坐标:x:%d,y:%d" %(param.contents.pt.x,param.contents.pt.y))elif wParam ==win32con.WM_LBUTTONUP:print("左键抬起,坐标:x:%d,y:%d" %(param.contents.pt.x, param.contents.pt.y))elif wParam ==win32con.WM_MOUSEMOVE:print("鼠标移动,坐标:x:%d,y:%d" %(param.contents.pt.x, param.contents.pt.y))elif wParam ==win32con.WM_RBUTTONDOWN:print("右键点击,坐标:x:%d,y:%d" %(param.contents.pt.x, param.contents.pt.y))elif wParam ==win32con.WM_RBUTTONUP:print("右键抬起,坐标:x:%d,y:%d" %(param.contents.pt.x, param.contents.pt.y))returnCallNextHookEx(mouse_hd, nCode, wParam, lParam)defstart_mouse_hook():"""函数功能:启动鼠标监听"""HOOKPROTYPE=CFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p))

pointer=HOOKPROTYPE(mouse_pro)

mouse_hd=SetWindowsHookEx(

win32con.WH_MOUSE_LL,

pointer,

GetModuleHandle(None),

0)

wait_for_msg()defstop_mouse_hook():"""函数功能:停止鼠标监听"""UnhookWindowsHookEx(mouse_hd)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值