Use Pyhook to monitor mouse and keyboard event instances in Python

Source: Internet
Author: User
Tags event listener
Pyhook is a python-based "Hook" library that is used primarily to listen for mouse and keyboard events on the current computer. This library relies on another Python library PyWin32, as the name suggests, 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 homepage, which, by and large, can be used

#-*-Coding:utf-8-*-# 3import pythoncom 4import pyhook 5def onmouseevent (event): # Monitor Mouse event print "Messagena Me: ", 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 completely blocked # that means your mouse looks like it's going to freeze, it seems to be out of response. return True 23def onkeyboardevent (event): # Listen for 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 "---" # The return value of the same mouse event listener function return True 42def main (): # Create a "hook" management Object HM = Pyhook.hookmanage R () # listens for all keyboard events hm. KeyDown = onkeyboardevent # Sets the keyboard "hook" HM. Hookkeyboard () # listens for all mouse events hm. Mouseall = onmouseevent # Sets the mouse "hook" hm. Hookmouse () # into the loop, if not manually closed, the program will always be in the listening state pythoncom. Pumpmessages () 56if __name__ = = "__main__": Main ()

Run the script above, try moving the mouse, or click on the keyboard (such as open a Notepad program, write something) and see what the script is outputting. You will find that every movement of your mouse and keyboard is captured and printed.

Also, be aware of the return values of the above two listener functions. These two functions can have no return value (actually return is none), if there is a return value, if the return value is True, then the script will catch, handle the corresponding event after the event continues to pass, if the return value is False, then 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: such as recording one's own track of the mouse, and then draw a picture with other programs, or record your daily key record, to see which keys on the keyboard you press the most, if you are bad, you can also put it quietly in other people's computer, Take a look at what the man 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, see how much time you have spent watching the web, how much time you are chatting, how much time you really work Oh.

  • Related Article

    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.