PyHook is used in Python to listen for instances of mouse and keyboard events

  • 2020-04-02 13:53:32
  • OfStack

PyHook is a python-based "hooks" library that listens for mouse and keyboard events on your current computer. This library relies on another Python library, PyWin32. As the name indicates, PyWin32 can only run on Windows platforms, so PyHook can only run on Windows platforms.

There is a simple tutorial on the use of PyHook on its official home page, and in general, you can use it this way


# -*- coding: utf-8 -*- # 
 3import pythoncom 
 4import pyHook  
 5def onMouseEvent(event): 
  
  #  Monitor mouse events    
  print "MessageName:",event.MessageName   
  print "Message:", event.Message   
  print "Time:", event.Time   
  print "Window:", event.Window   
  print "WindowName:", event.WindowName   
  print "Position:", event.Position   
  print "Wheel:", event.Wheel   
  print "Injected:", event.Injected      
  print"---"
 
  #  return  True  To pass events to other handlers    
  #  Notice if I return here  False  , the mouse events are all intercepted    
  #  That means your mouse looks like it's going to freeze there, it's going to lose its responsiveness    
  return True
 
23def onKeyboardEvent(event):
 #  Monitor keyboard events    
  print "MessageName:", event.MessageName   
  print "Message:", event.Message   
  print "Time:", event.Time   
  print "Window:", event.Window   
  print "WindowName:", event.WindowName   
  print "Ascii:", event.Ascii, chr(event.Ascii)   
  print "Key:", event.Key   
  print "KeyID:", event.KeyID   
  print "ScanCode:", event.ScanCode   
  print "Extended:", event.Extended   
  print "Injected:", event.Injected   
  print "Alt", event.Alt   
  print "Transition", event.Transition   
  print "---"   
  #  The return value of the mouse event listener function    
  return True 

42def main():   
  #  Create a "hook" management object    
  hm = pyHook.HookManager()   
  #  Listen for all keyboard events    
  hm.KeyDown = onKeyboardEvent   
  #  Set keyboard "hooks"    
  hm.HookKeyboard()   
  #  Listen for all mouse events    
  hm.MouseAll = onMouseEvent   
  #  Set the mouse "hook"    
  hm.HookMouse()   
  #  Enter the loop, and if you do not close it manually, the program will always be listening    
  pythoncom.PumpMessages() 

56if __name__ == "__main__":   
  main()

Running the script above, try moving the mouse or pressing the keyboard (like starting a notepad program and writing something) to see what the script produces. You will find that it captures and prints every movement of your mouse and keyboard.

Also, notice the return values of the above two listener functions. These two functions can not return a value (in fact, the returned is None), if you have a return value, if the return value is True, then the script after the capture, processing, the corresponding event take events continue to pass, if the return value is False, the event will be blocked here, in particular, is your mouse or keyboard there will be no response.

With PyHook, we can do a lot of interesting things: for example, we can record our mouse track for a day, and then draw a picture with other programs. Or record your daily keystrokes to see which ones you press the most on your keyboard. If you're bad, you can sneak it into someone else's computer and see what they've been doing all day. Of course, since PyHook can also get the title of the current window (WindowName), you can also record how much time you spend on each program during the day to see how much time you spend on the web, how much time you spend chatting, and how much time you spend actually working.


Related articles: