Monitor mouse and keyboard events with Python

Source: Internet
Author: User
Listening to mouse and keyboard events with Python Author: oldj | link: http://oldj.net/article/python-hook/

Pyhook is a python-based "Hook" library used to listen to 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.

 

1 #-*-coding: UTF-8 -*-#
2 # By oldj http://oldj.net /#
3 Import pythoncom

4 Import pyhook
5def onmouseevent (event ):
6
7 # monitor mouse events
8
Print "messagename:", event. messagename
9 print "message:", event. Message
10 print "time:", event. Time
11 print "window:", event. Window
12 print "windowname:", event. windowname
13 print "position:", event. Position
14 print "Wheel:", event. Wheel
15 print "injected:", event. injected
16 print "---"
17
18 # Return true to pass the event to another handler
19
# Note: if false is returned, all mouse events are blocked.
20
# That is to say, your mouse seems to be stiff there and it seems that you have lost response.
21
Return true
22
23def onkeyboardevent (event ):
24 # Listen to Keyboard Events
25
Print "messagename:", event. messagename
26 print "message:", event. Message
27 print "time:", event. Time
28 print "window:", event. Window
29 print "windowname:", event. windowname
30 print "ASCII:", event. ASCII, CHR (event. ASCII)
31 print "key:", event. Key
32 print "keyid:", event. keyid
33 print "scancode:", event. scancode
34 print "extended:", event. Extended
35 print "injected:", event. injected
36 print "Alt", event. ALT
37 print "transition", event. Transition
38 print "---"
39 # return values of the same mouse event listening Function
40
Return true
41
42def main ():
43 # create a "Hook" management object
44 Hm = pyhook. hookmanager ()
45 # Listen to all Keyboard Events
46 hm. keydown = onkeyboardevent
47 # Set the keyboard "hook"
48 hm. hookkeyboard ()
49 # Listen to all mouse events
50 hm. mouseall = onmouseevent
51 # Set the mouse "Hook"
52 hm. hookmouse ()
53 # enter the loop. If you do not close the loop manually, the program will remain in the listening state.
54 pythoncom. pumpmessages ()
55
56if _ name _ = "_ main __":
57 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.