Python 技术篇-pyhook暂停键盘鼠标监听事件,停止键盘鼠标监听事件且不关闭程序
很多时候只是要暂停一下或只想关闭监听,并不想把程序关闭。
但是网上搜了半天,没有找到,然后自己就去翻了翻源码,发现了。
开启监听是:
HookMouse()
和HookKeyboard()
暂停监听是:
UnhookMouse()
和UnhookKeyboard()
给大家展示一下源码:
def HookMouse(self):
'''Begins watching for mouse events.'''
cpyHook.cSetHook(HookConstants.WH_MOUSE_LL, self.MouseSwitch)
self.mouse_hook = True
def HookKeyboard(self):
'''Begins watching for keyboard events.'''
cpyHook.cSetHook(HookConstants.WH_KEYBOARD_LL, self.KeyboardSwitch)
self.keyboard_hook = True
def UnhookMouse(self):
'''Stops watching for mouse events.'''
if self.mouse_hook:
cpyHook.cUnhook(HookConstants.WH_MOUSE_LL)
self.mouse_hook = False
def UnhookKeyboard(self):
'''Stops watching for keyboard events.'''
if self.keyboard_hook:
cpyHook.cUnhook(HookConstants.WH_KEYBOARD_LL)
self.keyboard_hook = False
喜欢的点个赞❤吧!
各位好,我是csdn的小蓝枣,现在我的博客已经同步到博客园啦,欢迎大家关注哈!也欢迎大家关注我的公众号Py_JSQY!每天会更新有趣的科技文!哈哈!