Ubuntu – remap the Caps Lock key to play audio


I'm trying to re-use the useless Caps lock key for something useful on 16.04 MATE. I have come across

setxkbmap -option ctrl:nocaps

to disable it, but could it be remapped to play/pause VLC for example?
Thanks for your answers!

Best Answer

evtest and dbus

Here is a version involving a small script running in the background. A few easy steps need to be taken:

  • Install the evtest program: sudo apt install evtest. This program is able to decode and print raw events from the /dev/input/ hierarchy of devices.

  • Add your user account to the group input. Logout and login again. (This is needed because we are going to read from some device under /dev/input/, which normally can only be done by root or users in group input. We could run the script as root, but then we would have to find a way to tell it which user dbus instance it needs to talk to.)

  • Disable the capslock key like you did or use the corresponding Mate preference. It would also work if you map the capslock key to ctrl, like many people do.

  • Store the following script somewhere, e. g. $HOME/bin/capshack and make it executable:

    EVENT="type 1 (EV_KEY), code 58 (KEY_CAPSLOCK), value 1"
    toggleVLC () {
            dbus-send \
                --type=method_call \
                --dest=org.mpris.MediaPlayer2.vlc \
                /org/mpris/MediaPlayer2 \
    evtest "$KEYBOARD" | \
    while read line
        if [[ "$line" == *"$EVENT" ]]

The above script will listen to your keyboard events and execute a command when the capslock key is pressed. The command that is run is using dbus to send a play/pause event to a running VLC instance. You might have to tweak the KEYBOARD variable to point to your actual keyboard, but it is likely the above value will work.

If you prefer the toggleVLC() function to be called when you release the capslock key, you can change the EVENT variable in the script such that it ends with value 0 instead of value 1.

Also, it is possible that for you the keycode of the capslock key is different from 58. You can check this by running evtest, look at the output when you press capslock, and adjust the EVENT variable accordingly. You do not even have to match by the keycode and set EVENT simply to (KEY_CAPSLOCK), value 1.

Instead of using evtest and above script you could use thd from the triggerhappy package (something like thd --dump /dev/input/* would give similar output) but I found that a little bit less educational.

Now we create a user systemd unit to start the script automatically in the background:

  • Create the user systemd hierarchy: mkdir -p $HOME/.config/systemd/user

  • Add the following text to the file $HOME/.config/systemd/user/capshack.service

    Description=Abuse capslock key
  • Enable the service unit you just made: systemctl --user enable capshack

  • Start the service: systemctl --user start capshack

From now on you should be able to toggle play/pause in VLC using the capslock key. The background script should be automatically started when you log in.


As mentioned already you could also use the triggerhappy package to do this. This is the preferred method for people who want to fiddle less with the system and rather like a more solid solution.

  • Add user to the input group like above

  • Install the package: sudo apt install triggerhappy

  • Add a configuration for triggerhappy to ~/.config/thd/capslock.conf:

    KEY_CAPSLOCK    1   dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
  • Run thd like this: thd --triggers ~/.config/thd/capslock.conf /dev/input/event*

  • Make thd run for your user in a similar way like described for capshack already.

  • Make sure you have only one of the mentioned methods (capshack or thd) running, to not double toggle which each key press.

I tested all this on 18.04 under the standard session and 16.04 mate. But it should theoretically also work under wayland or with other desktop environments.

Related Question