Transforming A Keyboard To A Mouse In Software

You’ve probably heard the old saying that if it looks like a duck, and it quacks like a duck… So when is a keyboard a mouse? When software makes it quack like a mouse — that is, if mice quacked. [Blackle Mori] took a cheap USB keypad and, using the libevdev Linux system, made it impersonate a mouse.

The code on GitHub isn’t complex, but the details can take some time to get right. The code takes over all input events from the device. [Blackle] dumped out events sent from the keypad, but the stock evtest program would probably have done just as well.

Armed with the knowledge of what events belong to what keys, it is simple to replace them with messages that generally come from a mouse. This involves creating a virtual device that takes a surprisingly large number of lines of code, but none are particularly difficult to understand.

There are a few neat tricks that aren’t directly related. For example, [Blackle] wanted to have some keys launch programs. However, grabbing ownership of the keyboard requires you to run as root. You don’t want to launch your browser as root. Not only is it unsafe, but you’ll have the wrong configuration files. Luckily, Linux capabilities let it work the way you would like.

Overall, it is a neat approachable example of a technique you could use for many things. It isn’t hard to imagine a microcontroller on a serial port feeding mouse and keyboard events to the computer using a scheme like this. Sure, if you have the right USB micro, you could emulate a HID device, but that’s not always possible or desirable.

You never know what you might want to connect to a modern PC. Maybe even something as simple as a foot pedal.

8 thoughts on “Transforming A Keyboard To A Mouse In Software

  1. IIRC Windows had that feature since forever, it’s enabled somwehre in accessibility settings and can be toggled on/off with NumLock key. Another way would be to use AutoHotkey script.

    1. Linux has xkeynav which IMO is awesome. It breaks the screen down into 4 quadrants, you keep pressing a button for the half of the screen you want (right/left or up/down) so you always eliminate half of the remaining nav window until the mouse is exactly where you want it.

  2. I feel like the tl;dr crowd is going to have a field day with this, so to get ahead of them:

    yes, most GUIs have had a built-in way to use a keyboard as a mouse since the 80’s.
    and yes, you *can* just do something like “setxkbmap -option keypad:pointerkeys” if all you need is to use a keyboard as a mouse under X.

    that’s not really the point – the article is an excellent overview of how simple it is to create custom input device behaviors in linux using libevdev and how flexible they can be.

  3. There was a way to do this with DOS. I remember setting it up when I couldn’t figure out how to get the mouse driver to coexistent with the weird-ass memory management system requirements of Ultima 7.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.