Raspberry Pi is a popular single-board computer (SBC) with a diverse set of applications. It conventionally runs on Raspberry Pi OS (formerly Raspbian), but you can install other operating systems on it as per your project requirements.
Like any other operating system, Raspberry Pi OS also has built-in keyboard shortcuts to help you perform operations quickly. Additionally, you get the ability to create custom keyboard shortcuts for running programs or performing certain actions on your Raspberry Pi.
Why Should You Create Keyboard Shortcuts on Raspberry Pi?
With the Raspberry Pi OS (desktop environment), you typically run a program by locating it on the system and double-clicking it. Although this is totally acceptable, if you want to save yourself a few steps, you can assign a keyboard shortcut to the program/script you wish to run to execute it efficiently.
Likewise, you might also want to consider creating custom keyboard shortcuts if you have missing/broken keys on your keyboard. To this end, you can create a custom keybinding for the action linked to the broken/missing key and assign it to some other key on your keyboard.
For instance, if you use Scrot to take screenshots on Raspberry Pi and your keyboard doesn’t have the PrtScr (Print Screen) key, you can assign the program execution to your preferred key.
How to Create Custom Keyboard Shortcuts on Raspberry Pi OS
Openbox is the easiest way to create custom keyboard shortcuts on the Raspberry Pi OS. It is essentially a window manager for the X Window System — present on the Raspberry Pi OS (LXDE) — that lets you configure the behavior of almost every aspect of Pi OS’s desktop environment.
One thing to note here, however, is that the keybindings you create with Openbox only work on Raspberry Pi OS desktop. So, you can’t trigger programs with these keybindings in the Terminal, be it in headless mode or over SSH (Secure SHell).
Following are the steps to create a keybinding in Openbox:
- Open the Terminal. You can either click on the Terminal icon (in the top bar) or use the CTRL + ALT + T shortcut to do this.
- Type sudo nano /etc/xdg/openbox/lxde-pi-rc.xml to open the configuration file that holds all the keybindings for the Raspberry Pi OS.
- Scroll down to the section that reads Keybindings for running applications and use the following syntax to add a keybinding:
<keybind key="keyboard_shortcut">
<action name="action_to_perform">
<command>command_name</command>
</action>
</keybind>Here’s how you can use the command:
<keybind key="C-A-s">
<action name="Execute">
<command>gnome-screenshot --interactive</command>
</action>
</keybind>In the above example, we’ve assigned the CTRL + ALT + s shortcut to execute the GNOME Screenshot program. You can modify the above command to suit your requirements. Following are the key declarations you can use to create keybindings in Openbox:
- A – ALT
- C – CTRL
- S – Shift
- W – Super
- M – Meta
- space – Spacebar
- Home – Home
- End – End
- Return – Enter
- BackSpace – Backspace
- Print – Print Screen
- Hit CTRL + O to write lines to the configuration file and CTRL + X to exit editing.
- Enter sudo reboot to reboot Raspberry Pi and apply your keybindings.
Aside from the key declarations, there are two more essential components in a keybinding command: actions and commands. By leveraging the two effectively, you can create keybindings for more complex operations on Raspberry Pi OS.
Actions
Actions are essentially operations that you tell the system to perform when their associated keyboard shortcuts are triggered. There are different kinds of actions to manipulate the Raspberry Pi OS desktop or its program windows.
One of the most commonly used actions is Execute, which lets you create keyboard shortcuts to run/launch programs. If you want to perform some other window action on a program, below are some more useful actions you can create keybindings for:
- ToggleFullScreen: Makes the program run in full-screen view.
- Iconify: Minimizes a program window.
- ToggleMaximize: Maximizes/minimizes a window.
- Close: Closes a program window.
- PrevWindow: Cycles focus to the previous window.
- NextWindow: Cycle focus to the next window.
- ToggleShowDesktop: Hides all windows on the desktop.
These are, of course, only a handful of actions you can use with Openbox, and there are numerous other options for complex tasks that you can learn about over at Openbox’s wiki.
Commands
Commands are the most crucial component in creating keybindings. These are basically instructions that desktop shortcuts use to run their associated programs. You can enter these commands into the Terminal to verify if they work before using them in your keybindings.
Openbox keybindings leverage commands to perform your desired operations when the assigned keyboard shortcuts are triggered. To find the command for a program:
- Right-click on it (from the start menu) and go to Properties.
- Click on the Desktop Entry tab and select the value for the Command field.
Most programs have a simple command for their execution, but there are a few exceptions. For example, GNOME Screenshot requires you to use gnome-screenshot — interactive to run it with a custom keyboard shortcut.
Other commands for some of the commonly-used programs on Raspberry Pi:
- pcmanfm: File Manager
- lxterminal: Terminal
- chromium-browser: Chromium browser
- geany: Geany IDE
- gnome-screenshot: GNOME Screenshot
- gimp: GIMP
Similarly, in some programs, you may see commands with additional parameters like %U, %F in them. These parameters are for the Exec key in the .desktop files as part of the desktop entry specifications, and they determine how programs handle arguments.
But, unless you want to open programs in specific states, you won’t need to specify these parameters in their keybindings.
Using Custom Keyboard Shortcuts to Perform Tasks Efficiently
Using Openbox, you can create custom keyboard shortcuts to simplify a lot of your operations on the Raspberry Pi. If you prefer using your keyboard over the mouse for controlling and navigating operating systems, this is a great way to create key bindings that suit your similar needs on the Raspberry Pi OS.
But, a word of advice before you get too excited about custom keybindings: always ensure that the keybinding you’re creating isn’t already present in the Openbox configuration file. Also, verify that the action and the keyboard shortcut you intend to use in your keybinding aren’t already associated with another keybinding.