Python hook listening event and pythonhook listening
Python hook listening event
Author: vpoet
Date: Summer
#-*-Coding: UTF-8-*-# by oldj http://oldj.net/# import pythoncom import pyHook def onMouseEvent (event): # Listen to mouse event 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 the event to another handler # Note: if False is returned here, the mouse event is intercepted. # That is to say, your mouse seems to be frozen. It seems that you have lost the response. return True def onKeyboardEvent (event): # Listen to the keyboard event 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 "---" # return True def main (): # create a "Hook" management object hm = pyHook. hookManager () # Listen to all Keyboard Events hm. keyDown = onKeyboardEvent # Set the keyboard "hook" hm. hookKeyboard () # Listen to all mouse events hm. mouseAll = onMouseEvent # Set the mouse "Hook" hm. hookMouse () # enters the loop. If you do not close it manually, the program will remain in the listening status pythoncom. pumpMessages () if _ name _ = "_ main _": main ()
Run: