Categories
Linux Raspberry Pi

How to create a software keyboard

Intro

This example will probably get used in my advanced Raspberry Pi photo frame treatment. I use image display software, fbi, which is designed to take key presses in order to take certain actions, such as, advance to the next picture, display information about the current picture, etc. qiv works similarly. But I am running an automated picture display, so no one is around to type into the keyboard. hence the need for software emulation of the physical keyboard. I’ve always believed it must be possible, but never knew how until this week.

I am not a python programmer, but sometimes you gotta use whatever language makes the job easier, and I only know how to do this in python, python3 specifically.

This will probably only make sense for Raspberry Pi owners.

Setup

I believe this will work best if your Raspberry Pi has only a keyboard and not a mouse hooked up because in that case your keyboard ought to be mapped to /dev/input/event0. But it’s easy enough to change that. To see whether your keyboard is /dev/input/event0 or /dev/input/event1 or some other, just cat one of those files and start typing. You should see some junk when you’ve selected the right /dev/input file.

The program

I call it keyinject.py.

                    
#!/usr/bin/python3
# inject a single key, acting like a software keyboard
# DrJ 12/20
import sys
from evdev import UInput, InputDevice, ecodes as e
from time import sleep
# set DEBUG = True to print out more information
DEBUG = False
sleepTime = 0.001 # units are secs
# dict of name mappings. key is how we like to enter it, value is what is after KEY_ in evdev ecodes
d = {
‘1’ : ‘1’,
‘2’ : ‘2’,
‘3’ : ‘3’,
‘4’ : ‘4’,
‘5’ : ‘5’,
‘6’ : ‘6’,
‘7’ : ‘7’,
‘8’ : ‘8’,
‘8’ : ‘8’,
‘9’ : ‘9’,
‘0’ : ‘0’,
‘a’ : ‘A’,
‘b’ : ‘B’,
‘c’ : ‘C’,
‘d’ : ‘D’,
‘e’ : ‘E’,
‘f’ : ‘F’,
‘g’ : ‘G’,
‘h’ : ‘H’,
‘i’ : ‘I’,
‘j’ : ‘J’,
‘k’ : ‘K’,
‘l’ : ‘L’,
‘m’ : ‘M’,
‘n’ : ‘N’,
‘o’ : ‘O’,
‘p’ : ‘P’,
‘q’ : ‘Q’,
‘r’ : ‘R’,
‘s’ : ‘S’,
‘t’ : ‘T’,
‘u’ : ‘U’,
‘v’ : ‘V’,
‘w’ : ‘W’,
‘x’ : ‘X’,
‘y’ : ‘Y’,
‘z’ : ‘Z’,
‘.’ : ‘DOT’,
‘,’ : ‘COMMA’,
‘/’ : ‘SLASH’,
‘E’: ‘ENTER’,
‘S’: ‘RIGHTSHIFT’,
‘C’: ‘LEFTCTRL’
}

# https://python-evdev.readthedocs.io/en/latest/tutorial.html
inputchars = sys.argv
ltrs = inputchars[1]
# get rid of program name

keybd = InputDevice(“/dev/input/event0”)

for ltr in ltrs:
ui = UInput.from_device(keybd, name=”keyboard-device”)
if DEBUG: print(ltr)
mappedkey = d[ltr]
key = “KEY_” + mappedkey
if DEBUG: print(key)
if DEBUG: print(e.ecodes[key])

ui.write(e.EV_KEY, e.ecodes[key], 1) # KEY_ down
ui.write(e.EV_KEY, e.ecodes[key], 0) # KEY_ up
ui.syn()
sleep(sleepTime)
ui.close()

And it gets called like this:

$ sudo ./keyinject.py my.injected.letters

or

$ sudo ./keyinject.py ./m2.plE

to run the m2.pl script in the current directory and have it behave as though it were launched from a console terminal. The “E” is the ENTER key.

Interesting observations

There really is no such thing as a separate “k” key and “K” key (lower-case versus upper-case). There is only a single key labelled “K” on a keyboard. It’s a physical layer versus logical layer type of thing. The k and K are characters.

In the above program I did some of the keys – the ones I will be needing, plus a few bonus ones. I do need the ENTER key, and I can’t think of a way to convey that to this program, so to send ENTER you would do

$ sudo ./keyinject.py ENTER

But I was able to have these characters represent themselves: . , / so that’s not bad.

Prerequisites

You will need pyhon3 version > 3.5, I think. And the evdev package. I believe you get that with

$ sudo pip3 install evdev

And if you don’t have pip3 you can install that with

$ sudo apt-get update python3-pip

Reading keyboard input

Of course the opposite of simulating key presses is reading what’s been typed from an actual keyboard. That’s possible too with this handy evdev package. The following program is not as polished as the writing program, but it gives you the feel for what to do. I call it evread.py.

                    
#!/usr/bin/python3
# https://python-evdev.readthedocs.io/en/latest/tutorial.html
import asyncio
from evdev import InputDevice, categorize, ecodes

dev = InputDevice(‘/dev/input/event0’)
# following line is optional – it takes away the keybd from fbi!
# there is also a dev.ungrab()
dev.grab()

async def helper(dev):
async for ev in dev.async_read_loop():
print(repr(ev))

loop = asyncio.get_event_loop()
loop.run_until_complete(helper(dev))

Note the presence of the dev.grab(). That permits your program to be the exclusive reader of keyboard input, shutting out fbi. It can be commented out if you want to share.

Conclusion

We have created an example program, mostly for Raspberry Pi, though easily adapted to other linux environments, that injects keyboard presses, via a python3 program, as though those keys had been typed by someone using the physical keyboard such that, graphics programs which rely on this, such as fbi or qiv (and probably others – vlc?), can be controlled through software.

We have also provided a basic python program for reading key presses from the actual keyboard. I plan to use these things for my advanced RPi photo frame project.

References and related

The python evdev tutorial is really helpful: https://python-evdev.readthedocs.iogeoh/en/latest/tutorial.html

Raspberry Pi advanced photo frame article does not exist yet. The basic RPi photo frame article is here.

Another piece to the puzzle is turning GPS coordinates into a town name. That brief write-up is here.

Leave a Reply

Your email address will not be published. Required fields are marked *