Friday, January 8, 2016

Configuring function (Fn) keys in Linux under Openbox

http://thatlinuxthing.blogspot.rs/2015/11/configuring-function-fn-keys-in-linux.html

Often, special function keys (e.g. for controlling volume, brightness, sleep etc.) will not be automatically configured in many Linux distros. Luckily, in most cases it is easy to manually set this up. As with most other settings, Openbox allows custom key bindings to be added via entries in ~/.config/openbox/rc.xml. This way, functionality can be assigned to Fn keys not recognized out-of-the-box.
This is especially easy for standard functions (like volume-up and down, mute, sleep, brightness-up and down etc.) as these already have special key-codes assigned to them. For example, brightness-up button will be detected as XF86MonBrightnessUp allowing functionality to be directly bound to this code.

Discovering the key code to use

The best way to discover what happens when a key is pressed is using xev. Fire it from the terminal, and it will give you a window that captures events and logs them to the console. There, you'll be able to see the code for each button or button combination you press. Later on, you can use these codes to bind commands to them.

Configure keys using a graphical interface

Instead of editing Openbox's rc.xml manually, you can use obkey (Openbox Key Editor), which can automate the procedure of capturing the key codes for you and binding commands to them. See below for example commands you can bind to keys.

List of common function keys and example configurations

  • Increase/decrease brightness

    Brightness can be controlled using xbacklight, so assigning the following bindings will make the brightness function keys work:

    1. <keybind key="XF86MonBrightnessUp">  
    2.   <action name="Execute">  
    3.     <command>xbacklight -inc 40</command>  
    4.   </action>  
    5. </keybind>  
    6.   
    7. <keybind key="XF86MonBrightnessDown">  
    8.   <action name="Execute">  
    9.     <command>xbacklight -dec 40</command>  
    10.   </action>  
    11. </keybind>  
  • Sleep and Hibernate

    Assuming a SystemD setup, systemctl can be used to suspend the system to RAM (a.k.a. sleep), to disk (a.k.a hibernate) or both (a.k.a. hybrid sleep). Add the following to make the sleep function key work:
    1. <keybind key="XF86Sleep">  
    2.   <action name="Execute">  
    3.     <command>systemctl suspend</command>  
    4.   </action>  
    5. </keybind>  
    If you have additional keys (apart from sleep), you can try the following binding (make sure hibernation is properly configured and working on your system, and your user has the permissions to use it first):
    1. <keybind key="XF86Standby">  
    2.   <action name="Execute">  
    3.     <command>systemctl hibernate</command>   
    4.   </action>  
    5. </keybind>  
  • Search

    Many keyboards have a dedicated search key. To bind a command appropriate for your system (exemplified using SpaceFM) use the following:
    1. <keybind key="XF86Search">  
    2.   <action name="Execute">  
    3.     <command>spacefm --find-files %F</command>  
    4.   </action>  
    5. </keybind>  
  • Screen lock

    Bind any screen-locking (screensaver) utility, like slock or gnome-screensaver-command, as exemplified below to make the lock-screen button work:
    1. <keybind key="XF86ScreenSaver">  
    2.   <action name="Execute">  
    3.     <command>slock</command>   
    4.   </action>  
    5. </keybind>  
  • Volume up/down/mute

    There's a few options for dealing with volume keys.
    • Let volumeicon manage the function keys
      volumeicon is often used with Openbox and can be configured to manage volume buttons. Additionally, it can display OSD notifications nicely, and it supports multiple back-ends (GTK+ popups, libnotify, and possibly more). If you want to go this route, edit your ~/.config/volumeicon/volumeicon to contain the following:
      1. [Hotkeys]  
      2. up_enabled=true  
      3. down_enabled=true  
      4. mute_enabled=true  
      5. up=XF86AudioRaiseVolume  
      6. down=XF86AudioLowerVolume  
      7. mute=XF86AudioMute  
    • Bind volume buttons to amixer commands
      amixer can be called directly to control the volume. If you're using Alsa only, you may have to first find out the name of the mixer control your sound card exposes by using amixer scontrols. It is usually called Master, but not always. With PulseAudio, it is always called Master.
      1. <keybind key="XF86AudioRaiseVolume">      
      2.   <action name="Execute">  
      3.       
      4.     <command>amixer set Master 5%+ unmute</command>   
      5.   </action>  
      6. </keybind>  
      7.   
      8. <keybind key="XF86AudioLowerVolume">  
      9.   <action name="Execute">  
      10.     <command>amixer set Master 5%- unmute</command>  
      11.   </action>  
      12. </keybind>  
      13.   
      14. <keybind key="XF86AudioMute">  
      15.   <action name="Execute">  
      16.     <command>amixer set Master toggle</command>  
      17.   </action>  
      18. </keybind>  
  • Projector/Presentation mode

    The projector/presentation mode button will most often simply be understood as Super (Windows key) + P, and not as a separate key code, so you can bind it as such.
    1. <keybind key="XF86Search">  
    2.   <action name="Execute">  
    3.     <command>xrandr --auto</command>   
    4.   </action>  
    5. </keybind>  

Adding OSD notifications to commands

Most DEs come with a notification server you can use to display on-screen messages. Find out which one your DE/distro uses and simply wrap the commands used above in a script that also fires notifications. notify-send is a simple utility that comes with libnotify itself and can be used in most setups. It should be noted though that it doesn't offer a universal way to re-draw or replace a notification, making it hard to display a progress bar (e.g. for displaying volume or brightness level) and will keep creating new notifications each time it is called.
Some back-ends, like notify-osd, implement an extension supporting this scenario, but you'll have to know what you have in your distro. A back-end-agnostic drop-in replacement called notify-send.sh can also be used to work around the issue with back-ends that don't support this feature themselves.
  • Simple notify-send example

    For back-ends supporting the extension, a commands similar to the following could be used to increment the volume and show the level on screen: notify-send " " -i notification-audio-volume-medium -h int:value:$(amixer set Master 5%+ unmute | grep -m 1 "%]" | cut -d "[" -f2|cut -d "%" -f1) -h string:synchronous:volume
    This, of course, could be enriched to choose the correct icon based on the volume level etc.
  • Using notify-send.sh

    For back-ends without the extension, notify-send.sh can be used with a command similar to the above, but with adding --print-id as an argument. This will make the command return the notification ID that can later to be used to replace the notification with a new one by providing --replace=$ID. Of course, the ID would have to persisted somewhere between the calls, like a global variable of a file.

No comments:

Post a Comment