Python监听键盘和⿏标事件的⽰例代码
我们可以利⽤windows提供的api函数来实现对系统键盘事件和⿏标事件的监听,主要利⽤的是SetWindowsHookEx函数,这个函数可以允许调⽤者传⼊⼀个钩⼦函数也叫回调函数,当指定的事件发⽣时,你
传⼊的函数就会被触发,有些事件是进程级的,有些事件是系统级的,这⾥我们⽤WH_KEYBOARD_LL来监听系统键盘事件,⽤WH_MOUSE_LL来监听⿏标事件,注意不是WH_KEYBOARD,使⽤WH_KEYBOARD是⽆法监听到外部
程序的事件的,这个可以在windows官⽅⽂档⾥⾯看到说明,要⽤WH_KEYBOARD来监听系统键盘必须写dll去实现。
因为⽤到了⼀些宏定义在win32con⾥⾯已经有了,所以需要安装⼀下pypiwin32,当然如果你将这些宏定义写在⾃⼰的⽂件中,那么就不⽤安装pypiwin32了。
安装pypiwin32
pip install pypiwin32
# coding=utf-8
mousemove是什么键from ctypes import *
from ctypes import wintypes
import win32con
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 = None
class KBDLLHOOKSTRUCT(Structure):
_fields_ = [
('vkCode',c_int),
('scanCode', c_int),
('flags', c_int),
('time', c_int),
('dwExtraInfo', c_uint),
('',c_void_p)
]
class POINT(Structure):
_fields_ = [
('x',c_long),
('y',c_long)
]
class MSLLHOOKSTRUCT(Structure):
_fields_ = [
('pt',POINT),
('hwnd',c_int),
('wHitTestCode',c_uint),
('dwExtraInfo',c_uint),
]
def wait_for_msg():
msg = wintypes.MSG()
GetMessage(msg, 0, 0, 0)
def keyboard_pro(nCode, wParam, lParam):
"""
函数功能:键盘钩⼦函数,当有按键按下时此函数被回调
"""
if nCode == win32con.HC_ACTION:
KBDLLHOOKSTRUCT_p = POINTER(KBDLLHOOKSTRUCT)
param=cast(lParam,KBDLLHOOKSTRUCT_p)
ts.vkCode)
return CallNextHookEx(keyboard_hd, nCode, wParam, lParam)
def start_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()
def stop_keyboard_hook():
"""
函数功能:停⽌键盘监听
"""
UnhookWindowsHookEx(keyboard_hd)
def mouse_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" % (ts.pt.ts.pt.y))
elif wParam == win32con.WM_LBUTTONUP:
print("左键抬起,坐标:x:%d,y:%d" % (ts.pt.x, ts.pt.y))
elif wParam == win32con.WM_MOUSEMOVE:
print("⿏标移动,坐标:x:%d,y:%d" % (ts.pt.x, ts.pt.y))
elif wParam == win32con.WM_RBUTTONDOWN:
print("右键点击,坐标:x:%d,y:%d" % (ts.pt.x, ts.pt.y))
elif wParam == win32con.WM_RBUTTONUP:
print("右键抬起,坐标:x:%d,y:%d" % (ts.pt.x, ts.pt.y))
return CallNextHookEx(mouse_hd, nCode, wParam, lParam)
def start_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()
def stop_mouse_hook():
"""
函数功能:停⽌⿏标监听
"""
UnhookWindowsHookEx(mouse_hd)
测试代码:
import HookApi
if __name__== '__main__':
HookApi.start_keyboard_hook()
运⾏效果
以上就是Python监听键盘和⿏标事件的⽰例代码的详细内容,更多关于Python监听键盘和⿏标事件的资料请关注其它相关⽂
章!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。