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

喜欢的点个赞❤吧!

posted on 2019-03-18 10:16  小蓝枣  阅读(70)  评论(0编辑  收藏  举报