Use PyHook to listen to mouse and keyboard event instances in Python

Source: Internet
Author: User
This article describes how to use PyHook to listen to mouse and keyboard event instances in Python. This library depends on another Python library, PyWin32, and can only run on Windows, for more information, see PyHook. It is a Python-based "Hook" library that monitors the mouse and keyboard events on the current computer. This library depends on another Python library PyWin32. As shown in the name, PyWin32 can only run on Windows platforms, so PyHook can only run on Windows platforms.

For the use of PyHook, there is a simple tutorial on its official homepage. In general, you can use it like this.

#-*-Coding: UTF-8-*-#3 import pythoncom 4 import pyHook 5def 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 23def 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 42def 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 () 56if _ name _ = "_ main _": main ()

Run the script above and try to move the mouse or press the keyboard (for example, open a Notepad program and write something casually) to see what the script outputs. You will find that every movement of your mouse and keyboard is captured and printed by it.

In addition, pay attention to the return values of the above two listening functions. These two functions do not return values (actually None is returned). If there is a return value, if the return value is True, the script will pass the event after capturing and processing the corresponding event. If the returned value is False, the event will be blocked here. Specifically, that is, your mouse or keyboard will no longer respond.

With PyHook, we can do a lot of interesting things: for example, record the path of your mouse for a day, and then draw a picture with other programs; or record your daily key records, check which keys on the keyboard you press the most. If you are very bad, you can also quietly install them in others' computers to see what the person has done on that day. Of course, because PyHook can also get the title of the current window (WindowName), you can also record how much time you spent on each program in a day, let's see how much time you have to watch the web page, how much time you have to chat with, and how much time you have to really work.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.