Pyhook is a python-based "Hook" library that listens primarily to events on the mouse and keyboard on the current computer. This library relies on another Python library PyWin32, as the name shows, PyWin32 can only run on the Windows platform, so Pyhook can only run on the Windows platform.
With regard to the use of Pyhook, there is a simple tutorial on its official home page, which is generally used to
#-*-Coding:utf-8-*-# 3import pythoncom 4import pyhook 5def onmouseevent (event): # Monitor mouse events print ' Me Ssagename: ", 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 "---" # returns True to pass events to other handlers # Note that if you return False here, the mouse event will be blocked all # that means your mouse looks stiff and seems to lose its response. return True 23def onkeyboardevent (event): # Listening for 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 "---" # return value of the same mouse event listener function returns True 42def main (): # Create a "hook" management Object HM = Pyhook.hoo Kmanager () # Listens for all keyboard events hm. KeyDown = onkeyboardevent # set Keyboard "Hook" HM. Hookkeyboard () # listens for all mouse events hm. Mouseall = onmouseevent # set Mouse "Hook" hm. Hookmouse () # into the loop, if not manually shutdown, the program will always be in a listening state pythoncom.
Pumpmessages () 56if __name__ = = "__main__": Main ()
Run the script above, try to move the mouse, or click on the keyboard (such as opening a notepad program, write something), and see what the script output. You will find that every movement of your mouse and keyboard is captured and printed.
Also, pay attention to the return values of the two listening functions above. These two functions can have no return value (actually return none), if there is a return value, if the return value is True, then the script will continue to pass the event after capturing, processing the corresponding event, and if the return value is False, the event will be blocked here, specifically, Is that your mouse or keyboard will no longer respond.
With Pyhook, we can do a lot of interesting things: for example, record the trajectory of your mouse one day, and then combine another program to draw a picture, or record your daily key record, see which keys you click on the keyboard, and if you're bad, you can quietly put it in someone else's computer, Take a look at what that person has done 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 have on the web, how much time you chat, how much time you really work. Oh.