Raspberry Pi single-board computers have an excellent GUI called Raspberry Pi OS (formerly Raspbian). This makes getting started easy, but what if you want to launch an often-used program with a certain keyboard combination, rather than dragging your mouse to the corresponding icon? Or what if your keyboard is missing a certain button that you need, like “print screen?”
The good news is that assigning custom Raspberry Pi keyboard shortcuts is definitely possible, though you’ll need to do a bit of text file editing. Open up your terminal by clicking the appropriate icon in Raspberry Pi OS, or better yet with Ctrl-Alt-t, to get started on your custom input journey.
The example below shows one custom input example. How it works is discussed further in the sections below:
<!-- Custom Keyboard Shortcuts -->
<keybind key="A-p">
<action name="Execute">
<command>scrot</command>
</action>
</keybind>
<keybind key="C-A-c">
<action name="Execute">
<command>chromium-browser</command>
</action>
</keybind>
Advanced Settings: XML Modification
In the terminal, enter sudo nano /etc/xdg/openbox/lxde-pi-rc.xml. Scroll down to the line that says <keyboard>, or you can search for it by pressing Ctrl-w. On a new line following the </chainQuitKey> tag, enter the comment <!-- Custom Keyboard Shortcuts --> (or similar) to let you keep track of your new modifications.
Before actually making any modifications, scroll down through the lines following, noting the shortcut format. Breaking things down, the <keybind key="XXYY"> tag triggers a comand. The <action name="AABB"> tag then specifies what kind of action will be taken. Here we'll be using Execute as the action, in order to launch a program. Finally, the <Command> tag specifies that actual command to be executed.
Custom Keybind for Raspberry Pi
For the keybind key= tag, the format is "key1-key2-key3..." in whatever combination you desire. Note that the number of keys can be more or fewer than the three shown there, depending on your needs. Special keys have their own terminology, including:
- A for Alt,
- C for Ctrl,
- S for shift
- W for the "Windows" key
So Alt-p would be A-p, or if you want to use Ctrl as well, you'd add C-A-p here.
Command
The "%U" or other similar text following the main command can often be omitted.
Once triggered, the command line executes whatever comes after. If you need an alternate to printscreen, you'd type in scrot between the tags—the same command that you'd use to launch the printscreen program from the terminal. Other commands can be found by right-clicking on a taskbar icon, selecting properties, then the Desktop Entry tab. To launch VLC media player, for instance, you'd enter the line: <command>/usr/bin/vlc</command>, with the launch command between the opening and closing tags.
Exit and save, then reboot to activate your custom keyboard shortcuts.
Pi Keyboard Shortcut Application
It’s possible that you may never need to use a custom shortcut command, as the Raspberry Pi Operating System has several built-in, and you’ll often be using the terminal anyway. I got started down this road when working on the Raspberry Pi/Analog Discovery 2 oscilloscope. My Bluetooth keyboard didn’t have an actual print screen button, and I needed a way to get screenshots for the documentation found here. At the time, I just plugged in another keyboard, but this is a rather inelegant solution.
Whether you’re missing an important (to you) key or need to run a certain program in a flash, having the ability to make custom Raspberry Pi OS shortcuts is a great option to have at your disposal.