NOTE: This is orientated towards Linux Python 2.6, the Windows Python solution is different.
How to I get Python to simulate keyboard key presses and releases? For me this came up while trying to make a custom capacitive touch controller with a on a Raspberry Pi for use with MAME or Minecraft Pi edition.
For this we going to use a Python Module called Uinput. This module can simulate keyboard, mouse or custom device. It can also migrate happily between programs in the desktop environment as well as the command line.
All of the following need to be completed on the command prompt or in a terminal window so if you are on the desktop go ahead and open your terminal program.
Prerequisites
There are a couple of extra development libraries that need to be installed, before installing the python module otherwise Uinput will refuse to compile and install.First of all it is always worthwhile making sure your apt repositories are up to date it is a good rule to do this before installing any new software, so to do this you need to
sudo apt-get updateThen the development libraries that you need to install are libxtst-dev
sudo apt-get install libxtst-dev
Installing the module
Make sure that you have the python pip installer installed this will make it easier to build an install on your Linux system.
sudo apt-get install python-pipThen to install the module using the python pip installer.
sudo pip install python-uinputIf all the prerequisites are satisfied this will download compile and install the python module. :)
The module requires Uinput kernel module to be running this can easily be done with
sudo modprobe uinputThis will load the module this time round. I you want it to load every time you reboot your system then it will need to be added to the /etc/modules file. To edit the file from your command line.
sudo nano /etc/modulesAdd an extra time to the line to the file with.
uinputThen.
Ctrl + xThis will save and exit just hit Y to confirm overwriting the file.
Using the module
So now that is all complete its only a case of writing some code.
This is a little sketch to setup a two button device I'm just using this as an example but any device can be made with any number of keys so lets have a look at the code.
Lets have a look at what the code is doing.
Hope this helps out a few people as I found it tricky to get up and running in the first place hopefully I have covered that so people don't have the same difficulty.
import uinput
device = uinput.Device((uinput.KEY_W,uinput.KEY_S))
import time
while 1:
device.emit(uinput.KEY_W,1)
time.sleep(0.5)
device.emit(uinput.KEY_W,0)
time.sleep(0.5)
This is a little sketch to setup a two button device I'm just using this as an example but any device can be made with any number of keys so lets have a look at the code.
Lets have a look at what the code is doing.
import uinputThis imports the required module into your python sketch.
device = uinput.Device((uinput.KEY_W,uinput.KEY_S))This line creates a uinput device with two keys W and S all the letter keys emit as there lower case version unless it is modified with a shift.
imports the time module for handling delays
import time
while 1:Repeat forever
device.emit(uinput.KEY_W,1)Keydown w key
time.sleep(0.5)wait 0.5 seconds
device.emit(uinput.KEY_W,0)Keyup w key
time.sleep(0.5)
This module could be used for making custom combos for games in MAME like street fighter or just allow automated control of some command line tasks. I am currently using it to make a custom game controller on with a Raspberry Pi's GPIO connection.
Hope this helps out a few people as I found it tricky to get up and running in the first place hopefully I have covered that so people don't have the same difficulty.
Hi Gee I tried to install it my self put I ran into some problems. This thread helped me.
ReplyDeletehttp://stackoverflow.com/questions/29766818/cannot-install-uinput-on-raspberry-pi
I also had to install sudo apt-get install libudev-dev
Thanks for the heads up. are you running Raspbian Jessie? I'll have a go over this tutorial again see if I've missed anything :)
DeleteGee
yes I was running jessie
ReplyDeleteThis comment has been removed by the author.
ReplyDeletehi guys, im now trying an small project, i want to control a presentation with Ultrasonic sensors, i have my script in PYTHON, but, somehow, i have read a lot of post, all with almost the same, and i got nothing working, i see no Keyboard Inputs on de Console, i tried usins "sudo showkey -a" to see whats happening with all the keyboard input, but still have no clue, i made everything on this guide and anothers, how can a test o check it out? do i need an special permission on linux (im new using linux),
ReplyDelete