How to Create Custom Keyboard Shortcuts for Raspberry Pi

(Image credit: Chalitsa Hongtong / Shutterstock)

Every keystroke or mouse movement is sacred. So if you can, for example, hit CTRL+ALT+C to launch Chrome instead of rolling your mouse across the screen and clicking on an icon, why wouldn't you? In Windows, it's incredibly easy to make custom key combinations that open your favorite apps, but in Raspbian, the official OS of the Raspberry Pi, the process is a bit more complicated. Here's how to create custom keyboard shortcuts for Raspberry Pi.

Keep in mind that these shortcuts only work on the Raspbian desktop. If you're booting to the command prompt or logging into the command prompt via SSH, these won't be active.

1. Navigate to the command prompt. You can get there by hitting CTRL+ALT+T from the desktop, clicking the terminal icon, connecting via SSH or booting to the command prompt.

2. Open the /etc/xdg/openbox/lxde-pi-rc.xml file for editing by typing.

sudo nano /etc/xdg/openbox/lxde-pi-rc.xml

3. Scroll down to the <keyboard> section.  You can also get there by hitting CTRL+W to search and entering <keyboard> at the prompt. If you look around the <keyboard> section, you'll see all of the Raspberry Pi's built-in keyboard shortcuts listed. For example, the code that designates ALT+F4 as the combo for closing a window is in this block.

4. Type a comment tag such as <!-- My Custom Keyboard Shortcuts --> under the </chainQuitKey> tag.. This step isn't absolutely necessary, but is helpful for finding your shortcuts later on.


5. Enter keybind code that looks like this under the comment tag.

<keybind key="[KEYBOARD COMBO]"><action name="Execute"><command>[LAUNCH COMMAND]</command></action></keybind>

Obviously, you need to replace [KEYBOARD COMBO] with an actual set of keys and [LAUNCH COMMAND] with the command you need to start the app. Here's an example that launches the Chromium browser when you hit CTRL+ALT+c.

<keybind key="C-A-c"><action name="Execute"><command>chromium-browser</command></action></keybind>

6. Save the file and reboot. Your keyboard shortcuts should now work.

Keybind Key Names

The keyboard combo must be some combination of keys with a hyphen separating them. For example, C-S-a would be CTRL+SHIFT+a and A+F2 would be ALT+F2. Special keys like CTRL and ALT have abbreviated names. Here's a list:

  • A - ALT
  • C - CTRL
  • S - Shift
  • W - Windows Key
  • Print - Print Screen
  • Up / Down / Right / Left - arrow keys
  • Prior - Pg Up
  • Next - Pg Dn
  • space - spacebar
  • Home - home
  • End - end
  • Return - enter
  • BackSpace - backspace

If there are any other keys on your keyboard (an extra function key, etc), you can find out what the system calls them by opening a terminal window (from the Raspbian desktop) and typing in "xev -event keyboard" at the prompt. You will then see the name of every key you press in a format like this.

The screen shot above shows the xev output for hitting the Enter key, at least on the laptop we used for this article. You can see that it gives the name "Return" and it also gives a hexcode 0xff0d. You can use either one in your XML. So CTRL+ALT+Enter would be either C-A-Return or C-A-0xff0d.

Commands

To start a program, you need to know its execution command, the one you'd use if you wanted to launch it directly from a terminal window. You can find these commands for any installed program by right clicking its icon in the start menu and selecting properties and then viewing the Command field on the Desktop Entry tab. <

You can usually ignore the extra arguments after the command so "chromium-browser" works and does not actually require the %U in the keybind XML. One exception is the Gnome screen shot app which has to be launched as "gnome-screenshot --interactive." You can test any command by typing it into a terminal window. Also, sometimes there's a simpler command than shown here. For example, you can lunch the GIMP image editor with the command "gimp," but the command shown in the properties box is "gimp-2.10." Both work.


Here's a list of commands for popular apps.

  • chromium-browser - Chromium
  • geany - Geany
  • scratch3 - Scratch 3
  • gimp - GIMP
  • vlc - VLC
  • pcmanfm - File Manager
  • lxterminal - Terminal
  • galculator - Calculator
  • gnome-screenshot --interactive - Gnome Screenshot

Actions

For launching programs, the most common reason for creating a keyboard shortcut, you'll always use "Execute" as the action. But there are a few other actions that you can use for manipulating elements of the Raspbian desktop UI or its windows. There's a complete list of possible actions available at the Openbox website (Openbox is the window manager that Raspbian uses by default). Here are some common ones:

  • Execute - the main and probably only action you'll need for your keyboard macros
  • ToggleFullScreen - Makes a window full screen. This is already assigned to ALT+F11
  • Close - close window. Already assigned to ALT+F4
  • Iconify - minimize window
  • ToggleMaximize - maximize or unmaximize window
  • NextWindow - go to next window. Already assigned to ALT+TAB
  • PrevWindow - go to previous window. Already assigned to ALT+SHIFT+TAB

Some of the actions also have subtag options for them. For example, NextWindow has an option called allDesktops, which lets you choose whether you're going to cycle through windows on all desktops (assuming you have more than one) or only the current one. Openbox's site details all of the options.

Avram Piltch
Avram Piltch is Tom's Hardware's editor-in-chief. When he's not playing with the latest gadgets at work or putting on VR helmets at trade shows, you'll find him rooting his phone, taking apart his PC or coding plugins. With his technical knowledge and passion for testing, Avram developed many real-world benchmarks, including our laptop battery test.
  • jordantsap
    I changed the shortcuts exactly as you said but after reboot the old shortcuts are working
    Reply
  • Barry_IA
    jordantsap: Some things to try –



    At the top of /etc/xdg/openbox/lxde-pi-rc.xml it says not to modify that file (in that location) but to copy it to $HOME/.config/openbox, so:

    At your home directory (/home/pi or whatever)

    cd .config (notice the leading dot – it’s a hidden directory)

    sudo mkdir openbox (I needed to create this subdir on my RPi 400)

    cd openbox

    sudo cp /etc/xdg/openbox/lxde-pi-rc.xml lxde-pi-rc.xml



    You should now be able to ls $HOME/.config/openbox/lxde-pi-rc.xml and see the working copy there.



    sudo nano $HOME/.config/openbox/lxde-pi-rc.xml to edit.



    Some caveats:

    Watch your indent spacing; don’t tab – copy a section and edit it might be easier.



    If your new text is red it’s in the wrong section. I originally put my keybinding at the end of the file so I could find it easier. Found out later the end is commented for examples.



    So I put my keybinding in, rebooted so it would take effect, and it worked. Yeah! Oddly when I brought it up from a cold boot it didn’t – did a different function. Found out this other function was listed later so it was overwriting mine; no idea why it worked right the other day. I commented the ‘duplicate’ – the ‘#’ does not work for this filetype; at the beginning of the section you need <!-- and at the end --> . (Hopefully still works tomorrow!!)
    Reply