python monitor keyboard events pyHook

 

Coding = UTF-# 8 
Import pyHook 
Import PythonCOM 

# to listen to mouse events call 
DEF onMouseEvent ( Event ):
     IF ( Event .MessageName =! " Mouse the Move " ): # because the mouse a move will have a lot mouse move, so this filter under 
        Print ( event .MessageName)
     return True # would normally call is True, False if so, this incident was intercepted 

# to listen to the keyboard event calls 
DEF onKeyboardEvent ( event ): 
    Print ( event .Key) # returns pressed key
     return True 

DEF main (): 
    # create a manager 
    HM = pyHook.HookManager () 
    # monitor keyboard
    hm.KeyDown= OnKeyboardEvent    
    hm.HookKeyboard ()   
        # monitor mouse 
        # hm.MouseAll = onMouseEvent    
        # hm.HookMouse () 
    # loop monitor 
    pythoncom.PumpMessages () 
 
IF __name__ == " __main__ " : 
    main ()

effect

 

You can also monitor the mouse! ! !

 

Guess you like

Origin www.cnblogs.com/sea-stream/p/10989766.html