How to allow key functions while desktop locked

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.