20

Is it possible to allow a keyboard's volume keys to continue working when your desktop is locked? Currently, the default behavior disables all special-function keys until the desktop's unlocked. I like to use my PC as a music player and leave it locked when I'm not directly at the console, but I still want to let people to control the volume if a song's too loud.

4
  • +1! I bet that a side effect of this (if it is possible) is to not re-awaken the sleeping display, which is something I've wondered about before. Apr 17, 2012 at 18:05
  • 1
    I'd like to have this functionality too. Not only for volume, but also for the music control keys (play, pause;next;previous).
    – jplatte
    Apr 18, 2012 at 20:46
  • As an aside, as I was upgrading I turned my screen down to the lowest setting. Halfway through the install I couldn't turn it back up. Made for an interesting experience.. May 23, 2012 at 10:10
  • I wonder if the media keys could be made into some kind of fake LIRC device, pretending to be a remote control ... At least conceptually it makes sense, right?
    – taneli
    May 24, 2012 at 12:04

2 Answers 2

9

Somewhat of a shameful plug, but since there didn't seem to be any existing solution, and since the task seemed relatively straight-forward, I wrote a simple Python daemon to fix the problem. It uses the python-xlib API to capture system-wide key presses and then runs custom shell commands. It assumes a basic Debian/Ubuntu setup, but would probably work on most Linux systems with a few tweaks.

For my case, the volume up/down keys map to the code 122/123, so the corresponding commands to lower or raise volume only when the desktop is locked are:

gnome-screensaver-command -q | grep "is active" && bash -c '/usr/bin/pactl -- set-sink-volume `pacmd list-sinks | grep -P -o "(?<=\* index: )[0-9]+"` -10%'
gnome-screensaver-command -q | grep "is active" && bash -c '/usr/bin/pactl -- set-sink-volume `pacmd list-sinks | grep -P -o "(?<=\* index: )[0-9]+"` +10%'

Admittedly, that's a bit verbose. The second grep is to find the active sound interface on systems that might have several (e.g. my laptop has a Master and Headphone interface, allowing these commands to control either).

Edit: This no longer works as of Ubuntu 16. All X functions seem to become disabled, even from the terminal, once the screen locks.

3
  • 1
    I just got this working on Ubuntu 18.04. I had to replace the "is locked test" with gdbus call -e -d com.canonical.Unity -o /com/canonical/Unity/Session -m com.canonical.Unity.Session.IsLocked | grep -ioP "true" based on askubuntu.com/a/890802/627247 because I'm not using the gnome-screesaver but after that, no problems at all.
    – Fabian N.
    Jul 6, 2018 at 2:42
  • I can confirm that it works on Ubuntu 16.04 as well. To avoid confusion, the "is locked test" mentioned by Fabian N. means replacing gnome-screensaver-command -q | grep "is active" on lines 160--165 of freekey.py or directly in the configuration file by what he suggests. Aug 3, 2018 at 14:33
  • on my system 171 gdbus call -e -d com.canonical.Unity -o /com/canonical/Unity/Session -m com.canonical.Unity.Session.IsLocked | grep -ioP "true" && bash -c 'rhythmbox-client --next' works perfectly for next song on rhythmbox. I'm still working on previous and other controls but it'll get there. Feb 5, 2019 at 10:22
0

I wrote this script which works for me on Ubuntu 18.04:

#!/bin/bash

EVFILE=/dev/input/event3
USR=flurl


evtest $EVFILE \
    | grep --line-buffered -E "KEY_MUTE|KEY_VOLUMEDOWN|KEY_VOLUMEUP" \
    | grep --line-buffered "value 1" \
    | stdbuf -oL awk -F '[()]' '{print $4}' \
    | while read KEY; do
        if [ "$KEY" = "KEY_MUTE" ]; then
            su - $USR -c "amixer -D pulse sset Master +1 toggle"
        elif [ "$KEY" = "KEY_VOLUMEUP" ]; then
            su - $USR -c "amixer -D pulse sset Master 5%+"
        elif [ "$KEY" = "KEY_VOLUMEDOWN" ]; then
            su - $USR -c "amixer -D pulse sset Master 5%-"
        fi  
      done

You have to run it as root and adapt the variables EVFILE and USR

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .