python获取键盘监听的方法是:1、使用“pip install pypiwin32”命令安装pypiwin32;2、用win32con的WH_KEYBOARD_LL属性设置监听系统键盘事件。
我们可以利用windows提供的api函数来实现对系统键盘事件和鼠标事件的监听,主要利用的是SetWindowsHookEx函数,这个函数可以允许调用者传入一个钩子函数也叫回调函数,当指定的事件发生时,你
传入的函数就会被触发,有些事件是进程级的,有些事件是系统级的,这里我们用WH_KEYBOARD_LL来监听系统键盘事件,用WH_MOUSE_LL来监听鼠标事件,注意不是WH_KEYBOARD,使用WH_KEYBOARD是无法监听到外部
程序的事件的,这个可以在windows官方文档里面看到说明,要用WH_KEYBOARD来监听系统键盘必须写dll去实现。
因为用到了一些宏定义在win32con里面已经有了,所以需要安装一下pypiwin32,当然如果你将这些宏定义写在自己的文件中,那么就不用安装pypiwin32了。
输入:pip install pypiwin32 安装pypiwin32。
# coding=utf-8 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) print(param.contents.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" % (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)) 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)
打开一个txt文件,在里面输入文字,
import HookApiif __name__== '__main__': HookApi.start_keyboard_hook()
推荐课程: