Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

    Enabling special buttons (keys) on laptops under Linux

    8th May 2008

    Note, that under Gnome I only had to use a single system-wide utility to bind volume buttons to volume control. Under KDE things were a tad tougher.

    First, you may want to look for kmilo (if you don’t have it yet). It has several plugins e.g. for ThinkPad and Vaio laptops, as well as “generic” plugin. I didn’t try this solution, though.

    If you need more than kmilo can offer: keyTouch could help. keyTouch should be good also for non-laptop multimedia keyboards. However, this also wasn’t the solution I used.

    If you want to know how to map your special keys to functions yourself – then read on.

    (Main source used: mapping unsupported keys with xmodmap – it’s easy and clear, but didn’t work exactly as expected for me).

    Let’s go step-by-step. (Note: I’m on Debian Etch on LG LM70 Express laptop.)

    1. run this command (copy and paste – should be single line) in the Konsole or whatever terminal window you have open; you don’t need to be root to run this command
    1. xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'
    1. xev window will appear. Put mouse cursor over that window, and press the buttons you need to enable under Linux, memorizing/writing down the codes you get. In my example, I pressed “vol. -”, “vol. +”, and “mute” buttons of the laptop, and observed the following output in the console window:

      174 NoSymbol
      176 NoSymbol
      160 NoSymbol

      Those numbers on the left are keycodes, and the NoSymbol tells us that none of keysyms was bound to those keycodes

    2. now we need to create keycode-keysym bindings. For that, open with your favourite text editor (such as pico, nano, vi, kate, mcedit etc) the file ~/.Xmodmap (e.g. nano ~/.Xmodmap in the console)
    3. now you need to decide how to bind those special keys. For my case, the X window system has reserved key symbol names, namely X86AudioLowerVolume, X86AudioRaiseVolume, and X86AudioMute. For your case, you may need to lookup if there’s a pre-defined name for the functionality you are enabling – it would be best if there is such a name. Otherwise, you can use such keys as F13, F14, F15 etc (which are not physically present on the keyboard)
    4. write the bindings to the Xmodmap file. Format is simple, as in my case:

      Keycode 160 = XF86AudioMute
      keycode 174 = XF86AudioLowerVolume
      keycode 176 = XF86AudioRaiseVolume

    5. to apply the change, save Xmodmap file, exit the editor, and then from the console execute xmodmap ~/.Xmodmap. Now the bound keys must be visible to KDE’s “global shortcuts” (and any other “keyboard shortcut input” fields). To check, I right-clicked KMix, “Show mixer window”, “Configure global shortcuts”, and then just assigned respective keys to volume control options. It worked for me. Later I disabled those shortcuts, and everything still works fine (might be because of the installed kmilo, but I didn’t check)
    6. on Debian Etch, when starting KDE from kdm, the .Xmodmap file isn’t automatically applied at KDE startup. This can be easily fixed by creating the xmodmap.sh file in the ~/.kde/Autostart/ directory, with a single line: /usr/bin/xmodmap $HOME/.Xmodmap. Dont forget to chmod u+x xmodmap.sh. The xmodmap.sh script will bind codes to symbols at each KDE start. (Alternatively, the same script could be put into ~/.kde/env/, but according to startkde man-page, that folder is used for scripts which set KDE-wide variables, whereas we only run a command and do not assign any variables. Also, I wouldn’t recommend putting xmodmap invocation into .bashrc or .bash_profile – those are executed whenever you login, but presented model of xmodmap usage seems to have sense for KDE only.)

    This is it.

    For the method of using DCOP in KDE for hotkeys please see my primary source, which has a simple but clear KMix DCOP example.

    Share

    4 Responses to “Enabling special buttons (keys) on laptops under Linux”

    1. emsi Says:

      does not work here :(
      $ xev | grep -A2 –line-buffered ‘^KeyRelease’ | sed -n ‘/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p’
      bash: syntax error near unexpected token `(‘
      $bash –version
      GNU bash, version 3.2.17(1)-release (i686-pc-linux-gnu)
      Copyright (C) 2005 Free Software Foundation, Inc.

    2. Bogdan Says:

      Emsi, you are right, code was messed up upon publishing. I fixed that line, please try again.

    3. mark Says:

      This is one great guide. Helped me a lot. Thanks!
      May I please cite this complete how-to on the archlinux wiki? https://wiki.archlinux.org/index.php/Main_Page

    4. Bogdan Says:

      Mark,

      I’m glad this helped. Feel free to cite on ArchLinux wiki (hadn’t time to find some license to my liking, but citing is OK).

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>