As you may know, i have a Hackberry Pi, which has a trackpad. The trackpad it self, is excellent to use in GUI but it has no use at plain TTY terminal, which is what i use most, while operating the Hackberry. So, i had to found a way, to emulate the trackpad movements into keyboard keys, to use in terminal/cli applications. Nothing fancy, just to move the cursor keys, press ENTER, ESC, is enough for me... for now.
So, Python to the rescue. Using the mouse/trackpad in the terminal/TTY is not an easy task and it needs to use sudo
to do so. If you know another way, plz tell me at the comments section. By using the evdev
module, we can capture the trackpad/mouse movement and with the keyboard
module, we can emulate keystrokes. The complete project can be found at my Trackpad2Keyboad repository. All the details are there. Here we will discuss on how to use it.
The idea is to launch the t2k.py
script whenever you need it and kill it, when you don't. The same idea is implemented in the Retropie project, with a similar script, that emulates gamepad keys to keyboard keys. So, in any BASH script you want to use the trackpad, launch the trackpad emulator and then kill it on exit or other stage of your script.
The settings, for the key presses are stored in a file, which you can declare, when you run the program. This way, you can have multiple configurations, based on what you want to do at the time and run t2k
with the appropriate configuration. For example, in some occasions, you may want to emulate just the cursor keys, but in other occasions, maybe you want the Page Up/Down or Home/End keys or any other combination, based on the cli program you run at the time.
A simple example, while using fzf
would be this:
sudo ./t2k.py &
ls | fzf
sudo killall t2k.py
If you apply it, you will be able to navigate in fzf
with the trackpad and choose a selection, by pressing the Left click button or just exit by hitting the Right click button. Of course you can make things more complicated if you want. In my scripts, i use a piece of code, to detect if t2k.py
is present and if so, run it, thus providing the ability to use the trackpad to the user (me).
Trackpad2Keyboard, can also be used with a mouse or could even be used with a joystick. You just have to point, which device to use, in the settings file, at the dev
line. To find the correct device, type the command below:
python -m evdev.evtest
If you also use a Hackberry device, you will see something like this:
ID Device Name Phys Uniq
--------------------------------------------------------------------------------------------------------------------
0 /dev/input/event0 ZitaoTech HACKBerryPiQ20 usb-3f980000.usb-1.3/input0 vial:f64c2b3c
1 /dev/input/event1 ZitaoTech HACKBerryPiQ20 Consumer Control usb-3f980000.usb-1.3/input2 vial:f64c2b3c
2 /dev/input/event2 ZitaoTech HACKBerryPiQ20 Mouse usb-3f980000.usb-1.3/input2 vial:f64c2b3c
3 /dev/input/event3 ZitaoTech HACKBerryPiQ20 System Control usb-3f980000.usb-1.3/input2 vial:f64c2b3c
4 /dev/input/event4 vc4-hdmi vc4-hdmi/input0
5 /dev/input/event5 vc4-hdmi HDMI Jack ALSA
In which the mouse/trackpad is at /dev/input/event2
, so at the configuration file, just point the dev
parameter to that path, which is also used by default. Hope you find it useful. If you have other ideas to implement on it, leave a comment below.