Ubuntu – Problem binding a shortcut to a function / multimedia key


I want to connect a batch script to a shortcut. When I bind it under System Settings > Keyboard > Shortcuts it works with every key, except my brightness keys from my external Apple keyboard.

The brightness keys are recognized in showkey with the keycode 224 and 225.

xev output:

FocusOut event, serial 41, synthetic NO, window 0x4000001,
mode NotifyGrab, detail NotifyAncestor

FocusIn event, serial 41, synthetic NO, window 0x4000001,
    mode NotifyUngrab, detail NotifyAncestor

KeymapNotify event, serial 41, synthetic NO, window 0x0,
    keys:  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   
           0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 

Any suggestions what I can do?

Best Answer

Solution using halevt

According to the man pages halevt is a generic handler for HAL events. It is deprecated and replaced by udev, but since I don't know enough about udev I will give a halevt solution.

EDIT: after some serious sweat, I managed to do this in udev. See my other answer.

I will use vim to edit files, but if you don't know vim you can replace it by nano or gedit.

Installing halevt

sudo apt-get update && sudo apt-get install halevt

Determining which events you want to bind to a script
Stop the halevt daemon which is already running:

sudo /etc/init.d/halevt stop

Now see if halevt can recognize the events of the keys you want to use, start the listener:

sudo -u halevt halevt -fig:plugdev

Now press the function key on your keyboard you want to bind the script to. I know that the OP wants to get his brightness keys working, so let's go with that. The output for the brightness keys should look something like this:

Condition: /org/freedesktop/Hal/devices/platform_i8042_i8042_KBD_port_logicaldev_input,ButtonPressed (brightness-down)
Condition: /org/freedesktop/Hal/devices/platform_i8042_i8042_KBD_port_logicaldev_input,ButtonPressed (brightness-up)

You can see that the brightness-down and brightness-up events are transmitted.

Bind the event to a script
Now edit the file /etc/halevt/halevt.xml:

sudo vim /etc/halevt/halevt.xml

and add the following lines ( I did it at the bottom, just before </halevt:Configuration>):

<halevt:Device match="hal.info.category = input">

    <halevt:Condition name="ButtonPressed" value="brightness-up" exec="sudo /home/user/brightness-script.sh up"/>

    <halevt:Condition name="ButtonPressed" value="brightness-down" exec="sudo /home/user/brightness-script.sh down"/>


where of course you should change value to the event that you got from the listener, and exec by the command you want to execute.

Give the halevt user permission to do the command or script
Since the halevt daemon is run as the halevt user you have to give it permission to do what you specified in exec.

Run (remember to replace vim by your editor of choice)

sudo EDITOR=vim visudo

and add the following lines at the bottom

halevt ALL=(root) NOPASSWD: /home/user/brightness-script.sh 

and save and quit.

Make sure your script is executable

sudo chmod +x /home/user/brightness-script.sh

Start the halevt daemon again

sudo /etc/init.d/halevt start

And it should be working!

Related Question