python monitor, operate keyboard and mouse library pynput detailed tutorial

Section 0.0.0 preface

Monitor, operate mouse and keyboard are shortcut to realize automation, such as me Automatic check in Analog keyboard operation is used.

pynput is a cross platform third-party python library that monitors and controls mouse and keyboard.
You can install it through pip insnall pynput. The dependent libraries are downloaded automatically during installation.

pypi is linked here.

Next, I will press "mouse button", "monitor mouse" and "control mouse",
The order of "keyboard key", "monitor keyboard" and "control keyboard" introduces its usage.

  • Here is the text.

Section 1.0.0 Mouse button

Mouse button on pynput.mouse.Button Middle,
There are lift, right, middle and unknown.

Each key has two meaningful attributes: name and value.
Name is the name of the key, such as == 'left';
value is a tuple that records the location of the last click.

Section 1.1.0 Monitor mouse

There are two methods, one is functional, non blocking, the other is statement, non blocking.

First of all, this is the common method in the tutorial.

§1.1.1 pynput.mouse.Listener

The following is an example of the official website:

import pynput, time

def on_move(x, y):
    print('Pointer moved to {0}'.format((x, y)))

def on_click(x, y, button, pressed):
    print('{0} at {1}'.format(
        'Pressed' if pressed else 'Released',
        (x, y)))
    if not pressed:
        # Stop listener
        return False

def on_scroll(x, y, dx, dy):
    print('Scrolled {0} at {1}'.format(
        'down' if dy < 0 else 'up',
        (x, y)))

# Collect events until released
with pynput.mouse.Listener(
        on_scroll=on_scroll) as listener:

When you run this code, moving the mouse displays its coordinates,
When the mouse button is pressed and released, the program ends.

  • When any one of the three functions returns False (and also an Exception that releases or inherits from an Exception), the process ends.
  • It can be used listener.start() and listener.stop() in place of with statement.

§1.1.2 pynput.mouse.Events

Personally, this method is more intuitive than the previous one.

import pynput

with pynput.mouse.Events() as event:

    for i in event:
    #Iterative usage.
        if isinstance(i, pynput.mouse.Events.Move):
            #Mouse movement event.
            print(i.x, i.y)
            #Don't print 'i' directly. There is a problem with the module, and an error will be reported.

        elif isinstance(i, pynput.mouse.Events.Click):
            #Mouse click event.
            print(i.x, i.y, i.button, i.pressed)
            #this i.button It is one of the "mouse buttons" mentioned above, which can be judged by is statement.

        elif isinstance(i, pynput.mouse.Events.Scroll):
            #Mouse wheel.
            print(i.x, i.y, i.dx, i.dy)

    i = event.get(1)
    #Another use.
    #The default is None.
    #This' 1 'is the longest waiting time. There is no event beyond this time,
    #It's a mistake. The error type is Empty for the queue module, not TimeoutError.

Section 1.2.0 Control mouse

  • § 1.2.1
    Execute first pynput.mouse.Controller() get control.
    The following methods are the methods of the control.

As follows:

import pynput

ctr = pynput.mouse.Controller()
  • § 1.2.2

The dynamic attribute position returns a tuple of mouse position coordinates (like this: (x, y)),
Change the mouse position by definition, such as ctr.position = (500, 500).

  • § 1.2.3

Of course, there is also a move method for moving the mouse,
The usage is ctr.move(dx, dy).

  • § 1.2.4

To use the click method to simulate clicking, you need to provide the button to click,
Button in pynput.mouse.Button There are left, right and middle.
There is also an optional parameter count, which is the number of clicks. The default value is 1.


import pynput

ctr = pynput.mouse.Controller()
#Left click., 2)
#Double left click.
#Right click.
  • § 1.2.5

Press the button key with press(button);
Method release(button) release key. If the key is not pressed, no error will be reported.


import pynput

ctr = pynput.mouse.Controller()
#Press the left key.

ctr.move(50, 0)
#Move 50 units to the right.

ctr.move(0, 50)
#Move down 50 units.

#Release the left key.
  • § 1.2.6

Simulation roller, using scroll, provides parameters dx and dy.

For example:

import pynput

ctr = pynput.mouse.Controller()

ctr.scroll(0, 50)
#Scroll up 50 units.

ctr.scroll(0, -50)
#Scroll down 50 units.
  • These are mouse operations,
  • Here is the keyboard operation.

Section 2.0.0 Keyboard keys

It's more difficult to get the key of keyboard than mouse, but it's not used much.
So I'll talk about the common usage first, and then get it.

Section 2.0.1 usage method

First, after obtaining the event, it is necessary to determine whether the key is a special key or a common key,
You need to determine whether it has the name attribute. If yes, it is a special key.
This property records the name of the property. For example, ctrl corresponds to 'ctrl' and 'ctrl'_ L 'or' ctrl_r'.
In the ordinary keys, instead of. char.

Note: upper case letters and lower case letters have different keys.

There are other features. There's not much to say here.

Section 2.0.2 obtain

First, the special key pynput.keyboard.Key You can find it directly in "module".
For example, Ctrl corresponds to pynput.keyboard.Key.ctrl And. ctrl_l and. ctrl_r.

Then, ordinary keys can be pynput.keyboard.KeyCode.from_char (special key is not allowed, ArgumentError will appear when using).
If a can run pynput.keyboard.KeyCode.from_char('a ') obtained.

Both can be used pynput.keyboard.KeyCode.from_vk is obtained by the key mapping code.

Section 2.1.0 Monitor keyboard

There are mainly two methods, similar to the mouse, and my description order is the same as the previous article.
The other is the encapsulation of Listener, which is used for shortcut keys. I will put it in the last one.

§ 2.1.1 pynput.keyboard.Listener

Official website example:

from pynput import keyboard

def on_press(key):
    'Executed when the key is pressed.'
        print('alphanumeric key {0} pressed'.format(
    except AttributeError:
        print('special key {0} pressed'.format(
    #The key type is determined by the attribute.

def on_release(key):
    'Executed when the key is released.'
    print('{0} released'.format(
    if key == keyboard.Key.esc:
        # Stop listener
        return False

# Collect events until released
with keyboard.Listener(
        on_release=on_release) as listener:
  • When either of the two functions returns False (as well as an Exception that releases or inherits from an Exception), the process ends.
  • It can be used listener.start() and listener.stop() in place of with statement.

§ 2.1.2 pynput.keyboard.Events

import pynput

with pynput.keyboard.Events() as event:

    for i in event:
    #Iterative usage.
        key_event = i
    key_event = event.get()
    #get usage.
    #You can provide a real number as the maximum waiting time (in seconds). There is no event beyond this time,
    #It's a mistake. The error type is Empty for the queue module, not TimeoutError.

#Judge the event:

if isinstance(key_event, pynput.keyboard.Events.Press):
    print('Press the key', end = '')
elif isinstance(key_event, pynput.keyboard.Events.Release):
    print('Release the key', end = '')

#Judgment button:

#*The 'key' attribute * of this event corresponds to the key 'key' * obtained by the * Listener method.

except AttributeError:
    #This is a normal button.
    #There are two ways to judge. The first one is created by me, and the second one is on the official website.
    if ('ctrl'):
        #By name.
        print('It happened ctrl Key event.')
    elif key_event.key is pynput.keyboard.Key.esc:
        print('It happened esc Key event.')

§ 2.1.3 pynput.keyboard.GlobalHotKeys

(and ' pynput.keyboard.HotKey 'similar functions can be implemented, but it's troublesome)

For the official website example, I wrote esc.

from pynput import keyboard

def on_activate_h():
    print('<ctrl>+<alt>+h pressed')

def on_activate_i():
    print('<ctrl>+<alt>+i pressed')

def esc():
    print('<esc> pressed')
    return False

def esc_shift():
    print('<esc>+<shift> pressed')
    raise Exception

with keyboard.GlobalHotKeys({
        '<ctrl>+<alt>+h': on_activate_h,
        '<ctrl>+<alt>+i': on_activate_i,
        '<esc>':          esc,
        '<esc>+<shift>':  esc_shift}) as h:

When the esc key is pressed, the function is triggered but does not stop.
Observe the source code and find that although this class inherits from Listener, it will encapsulate the executed function, and only return None after encapsulation.
Therefore, the process cannot be ended by return False.

Section 2.2.0 Control keyboard

  • § 2.2.1

Get control first: Ctr= pynput.keyboard.Controller ().
The methods mentioned below refer to the methods of the control.

  • § 2.2.2

Press the key by pressing press,
You need to provide a "character of 1" or "key object as mentioned above".

Warm reminder, this method test has risks, if found that the computer typing, abnormal operation,
Most likely because the simulation pressed a key that was not released.
You can restart the console or the computer.

  • § 2.2.3

Release the button through release,
As with the press method, you need to provide a "character of 1" or "key object as mentioned above".


//This program will press "down ctrl+shilf+s" shortcut key, pause for 3 seconds and then press esc key.
//Simple simulation of the "save as" operation.

import pynput, time

ctr = pynput.keyboard.Controller()
#Press ctrl through the key's map code.
#Press the shift key through the key object.'s')
#Press the s key with a character of 1.

#Finish. Release the key you just pressed. I'll talk about a simpler and more elegant way later.

  • § 2.2.4

The pressed method is what I call "simpler and more elegant".
When using, provide the key to be pressed, and then "encapsulate" it with the statement.
The effect is to press the supply key in sequence when entering the statement block and release the key in reverse sequence when exiting the statement block.

As follows:

import pynput, time

ctr = pynput.keyboard.Controller()

with ctr.pressed(


with ctr.pressed(pynput.keyboard.Key.esc):
  • § 2.2.5

Type means "typing" in addition to "type" in English.
This method takes the string and types each character.
According to the test, providing it with a "key list" can also be used normally.

For example:

import pynput

ctr = pynput.keyboard.Controller()

ctr.type('Hello world!')

#Press esc and release.
  • The above is the text.

some suggestions

The monitor operation of mouse and keyboard in this module is realized by multithreading.
Therefore, multi thread shared memory feature programming can be used; second, pay attention not to start monitoring frequently, which is a great expense to the system.

Also, when the requirements for mouse position monitoring are not very high (such as real-time display of mouse position to users), you can cycle the sleep every time.


Before chatting with others, he said he pulled the computer's wheat out and blocked the camera when he didn't need it.
I said it's more convenient to disable the microphone with the Fn shortcut key.
Now think about it. Hackers can unblock microphones through analog buttons.
Although this module has no Fn key, what else?


This is the most complete. It's not easy to be original. It's just hand-made.

Reprint please indicate the author and link.

Thank you for reading!

Keywords: Python Attribute pip Programming

Added by pvraja on Sun, 17 May 2020 09:07:55 +0300