Control a mouse, create a virtual keyboard and monitor keyboard input with this handy Python module.
One of the earliest Tuesday Tooling posts was a keyboard simulator but this post covers a much better implementation, that does not require root access!
So what is it?
Pynput is a module which can control a keyboard and mouse, or it can monitor those devices and act accordingly.
With Pynput we can have Python simulate a keypress, move the mouse to a specific point on the screen and monitor the inputs.
Hey can I make a keylogger with this?
Yes you could, but don't. Using this project to monitor a co-worker, friend or anyone else is immoral and wrong! Don't be silly, and use this tool for useful and good projects.
So what can I do with it?
I recently made a flight controller for a Raspberry Pi and I used this library to simulate keypresses when the controller, connected to the GPIO was used.
So how can I install it?
Via the pip package manager
To install on Linux / macOS
sudo pip3 install pynput
pip3.exe install pynput
So how do I use it?
Controlling the keyboard
A quick intro is to create something that will type a message to the screen.
from pynput.keyboard import Key, Controller from time import sleep keyboard = Controller() sleep(5) keyboard.type("Hello World")
We import the
pynput module, specific the Key and Controller classes which are used to access the keyboard. We then create an object
keyboard which creates a connection from our code to the virtual keyboard. To type a sentence or paragraph we use
This code will type Hello World into any open text editor. I added a pause of five second to give me chance to open the text editor window.
To press a specific key, in this case to type
@ b i g l e s p
I wrote this code.
from pynput.keyboard import Key, Controller from time import sleep keyboard = Controller() sleep(5) letters = ["@","b","i","g","l","e","s","p"] for letter in letters: keyboard.press(letter) keyboard.release(letter) sleep(0.1) keyboard.press(Key.enter) keyboard.release(Key.enter) sleep(0.1)
I store the letters inside a list and then use a for loop to iterate over each letter, typing each letter into the open editor. Note how there is a
release function which emulates a quick tap of the key or if used with a
sleep() between them it can emulate a long press.
To use special keys such as Enter, ESC, Shift etc we need to pass the name of the key but with the
Key prefix to say that we are using the keyboard. In this case I press Enter
(Key.enter)at the end of each line to create a new line.
Controlling the mouse
If you ever need to control the mouse using code then this is the module for you. In this example I use the
randint function from
random to generate random number between 0 and 1920 and 0 and 1080, using these ranges I can position the mouse at any point on my 1080p laptop screen. The random numbers generated move the mouse in a crazy pattern.
from pynput.mouse import Button, Controller from random import randint from time import sleep mouse = Controller() for i in range(50): x = randint(0,1920) y = randint(0,1080) mouse.position = (x,y) print(x, y) sleep(0.2)
I only do this 50 times using a for loop. If I tried this with a
while True loop then I would never get control of the mouse!