Long story short, I have a 'dumb' laptop keyboard matrix and I want to make it into a functional keyboard wired directly to my Pi via shift registers (for cycling the matrix inputs and sampling the outputs on fewer I/O pins). I have built a USB interface for it in the past using an Arduino Leonardo/Micro but for efficiency I'd like to read the shift registers with Python (or another language, I suppose, but so far I've only used Python on my Pi) and get it to emulate the keystrokes. Ideally I'd like it to function exactly the same as if it were a USB keyboard, i.e. in both the Pi's CLI and GUI with all the keys, modifiers and so on intact, if I can't do that then I'll stick with the Arduino interface.
So far I've found Pexpect but I really can't work it out, I've also tried os.system and subprocess.call but I can't figure out what to tell them to do in order to get the desired result, so far I can only think to tell them to echo a variable but that naturally won't 'type' the variable into the prompt. I've also found xsendkey and xsendkeycode but they're written in C, I think, and apparently only work in X; I want this more for use in the CLI since the project I'm using it for will have a PiTFT as the main display so X isn't something I want to use much, but of course I need the keyboard to work in both the CLI and GUI anyway.
I'm using a Model B with Raspbian, I assume that matters. Any and all pointers, tips and suggestions are appreciated, thanks for reading.
Edit: It looks like python-uinput (http://tjjr.fi/sw/python-uinput/) will do what I want, I'm not sure I completely understand it yet but I think it'll do the job once I figure it out. You need to install libudev-dev first, run 'modprobe uinput' after you install (and add 'uinput' to /etc/modules if you want it to run at boot) and then run the Python examples (and presumably any program using uinput) with superuser privileges to get it to work, none of which was made particularly clear in the documentation. I need to look into what it can do, mainly with regards to modifiers and 'invisible' keys, but it looks good.
Edit again: After digging a little deeper, I'm struggling with python-uinput. I can do this just fine:
Code: Select all
include uinput
device = uinput.Device([
uinput.KEY_A #let's just use 'a' as an example
)]
device.emit_click(uinput.KEY_A)The problem I have is that I can't work out how to replace the KEY_A in that last line with a variable, which is going to make it quite tricky to use it to interface a keyboard... Poking around in the __init__.py file (here: https://github.com/tuomasjjrasanen/pyth ... _init__.py) brings this up:
Code: Select all
def emit_click(self, event, syn=True):
"""Emit click event. Only KEY and BTN events are accepted,
otherwise ValueError is raised.
`event` - event identifier, for example uinput.KEY_A
`syn` - if True, Device.syn(self) is called before returning.
"""
ev_type, ev_code = event
if ev_type != 0x01:
raise ValueError("event must be of type KEY or BTN")
_libsuinput.suinput_emit_click(self.__uinput_fd, ev_code)
if syn:
self.syn()Again, thanks for reading, I know this post is getting a little long...