Enabling the Multimedia Keys on your Keyboard
From Zenwalk Wiki
| Installing/Setting Up | Internationalisation | Networks | Software | Package Management | Misc |
|
Multimedia keyboards are all over nowadays. Most of them tend to work out of the box on that other OS. However, under linux, it's not always like that. However, there's an easy remedy! You just use this Wiki article.
When you press (or release) any key on your keyboard, a keycode is sent to the X server. But the X server can't do anything with that keycode, unless it is mapped to a keysym. When the X server starts it looks in the files and folders under '/etc/X11/xkb' and creates the initial mappings with the data it finds there. Later on these mappings can be altered with the xmodmap utility. On a fresh Zenwalk install, the keyboard model used is most likely pc105. This model has no multimedia keys.
Mapping the keycodes to keysyms is not enough. You also have to tell the X server what to do for each keysym. Most desktop environments (DE's) have some control panel to set this. If the DE of your choice doesn't have one, then there is a generic tool that can do this: XBindKeys. Finally there is XMacro. With the help of this utility, you can turn one key press into a series of key presses.
[edit] Xkb Supported Keyboards
First check if your keyboard/notebook model is mentioned in '/etc/X11/xkb/rules/xorg.lst'. If it is, then just edit the Core keyboard's InputDevice section in your '/etc/X11/xorg.conf' file. That is all you need to do, to map the extra keys on your supported keyboard to keysyms. The following example is for a Logitech Internet Navigator Keyboard with a US-international layout. As an extra, the otherwise useless left Windows key is made to work as an additional AltGr key.
Section "InputDevice"
Identifier "Keyboard1"
Driver "kbd"
Option "XkbRules" "xorg"
Option "XkbModel" "logicink"
Option "XkbLayout" "us"
Option "XkbVariant" "intl"
Option "XkbOptions" "lv3:lwin_switch"
EndSection
[edit] Finding Keycodes and Keysyms
If your keyboard is not supported by Xkb, then you want to find the keycodes for your multimedia keys. If it is supported, then you may want to find out to which keysym names the keys are mapped. This can both be done with the same tool: xev. Open a terminal, and just do:
$ xev
Now a tiny white box appears on the right, with a black rectangle in it. This white box needs to be active if you want the key detection to go correctly, so make sure it is always on top and activated. Then press the first of your multimedia keys. You will see some output on the terminal, similar to this:
KeyPress event, serial 31, synthetic NO, window 0x2800001,
root 0x4c, subw 0x0, time 25465626, (612,-81), root:(1163,364),
state 0x10, keycode 230 (keysym 0x1008ff30, XF86Favorites), same_screen YES,
XLookupString gives 0 bytes:
XmbLookupString gives 0 bytes:
XFilterEvent returns: False
KeyRelease event, serial 31, synthetic NO, window 0x2800001,
root 0x4c, subw 0x0, time 25465742, (612,-81), root:(1163,364),
state 0x10, keycode 230 (keysym 0x1008ff30, XF86Favorites), same_screen YES,
XLookupString gives 0 bytes:
The part we need is: keycode 230 (keysym 0x1008ff30, XF86Favorites). This can be found on the third line of each block. For keys that are already mapped, the keysym and keysym name are right behind the keycode. For keys that are not yet mapped, there is no keysym and thus no keysym name. The example above shows that the Favorites key of a supported keyboard was pressed.
Press all your multimedia keys one by one and write down their captions, keycodes and keysym names (if any).
[edit] Xmodmap
The next thing to do, is to create a text file from which the xmodmap utility can read the keycodes and the keysym names you want to map them to. If you have a keyboard that is supported by Xkb, then you can skip this step, unless you want to map some keycodes to a different keysym than the default. If you name this text file .Xmodmap and place it in your home dir, then the settings in it will be loaded each time you log in. (This is done by a call to xmodmap on line 51 of the '~/.xinitrc' script.)
You must use existing keysym names, so you can't make up your own names for the keys. You can use Fx for example, where x is a number between 1 and 35 (no, not 36). But if you use F1 to F12, than you just end up having an extra copy of the function key of that name. Some programs see F13 to F24 as Shift+F1 to Shift+F12. Likewise F25 to F35 is sometimes seen as Control+F1 to Control+F11. So if you have one of those programs or you just want more meaningful names for your keys, then you'll need an alternative. '/usr/share/X11/XKeysymDB' contains a bunch of valid keysym names. The second half of the file has the more useful alternatives for multimedia keys, e.g. XF86AudioMute and XF86Mail.
Below is a partial example of an '~/.Xmodmap' file with some web browser related keys. As you can see, comments have to be prefixed by a '!' in stead of the usual '#'. The first three keycodes are mapped to unused (but valid) keysyms. Further down in this wiki article we'll see how to bind them to actions. The third keycode is mapped to a keysym that is already in use, Escape. So now this keyboard has effectively two escape keys. This is done because the usual keyboard shortcut to stop downloading a web page is the escape key. So for this key it is not necessarry to bind it to get the right function. At the end is an alternative method to make one of the windows keys useful. If you want to have this, then you only need to use this method or the one above, not both at the same time.
! Special keys for my multimedia keyboard. ! ! Web keycode 178 = XF86WWW ! Back keycode 234 = XF86Back ! Forward keycode 233 = XF86Forward ! Stop keycode 232 = Escape ! Make the left windows key work as AltGr keycode 115 = ISO_Level3_Shift
To activate these mappings immediately, give the following command.
$ xmodmap ~/.Xmodmap
[edit] Binding Keys to Actions in Xfce
Once you have the keycodes mapped to keysyms using one of the two methods above, you'll want to bind the keys to actions. In Xfce this is done in the Keyboard Preferences dialog box. To open it, go to the menu and select 'Settings | Keyboard Settings'. On the second tab of this dialog box, Shortcuts, you can set the key bindings you want.
You can either add your own theme or modify the Zenwalk theme. If you select the Zenwalk theme before you press the Add button in the Themes column, then the new theme will include all settings from the Zenwalk theme. Next press the other Add button. A small pop-up box will appear. Type the command or name of the script you want executed in it and press the OK button. Finally you will be asked to press the key you want this command to be bound to. You don't even need to know it's name. It will be displayed in the dialog box though. So if you picked the more meaningful names from the XKeysymDB, then it will be clear which row to edit if you ever want to change anything. Repeat this for all the keys you want to bind.
[edit] XBindKeys
- Borromini made a package of xbindkeys 1.7.3, which is available via netpkg by selecting one of the linuxpackages mirrors.
If the desktop environment of your choice doesn't have a tool to bind key to actions, or, you wish to frequently change desktops and have consistent hotkey actions, then you need to use a generic tool for it: XBindKeys. Like so many tools under *nix XbindKeys needs a text configuration file. You can generate a standard one with some helpful hints by issuing the following command:
[edit] The .xbindkeysrc file
$ xbindkeys --defaults > ~/.xbindkeysrc
Open this file in a text editor, so you can customise it. Each key that you want to bind needs three lines in this file:
- For the sake of clarity, a line stating what it does.
- A line containing the command it will execute between double quotes (" ").
- The key or combination of keys that you want to bind the command to.
Keys can either be given by their keysym name or by their keycode.
If you still have the keycodes and keysym names for your keys from the earlier steps, then you can immediately start writing key binding rules. If you forgot them or you just want to double check, then xbindkeys can show them to you. Below is the output for the 'Mail' key on my keyboard.
$ xbindkeys -k
Press combination of keys or/and click under the window.
You can use one of the two lines after "NoCommand"
in $HOME/.xbindkeysrc to bind a key.
"NoCommand"
m:0x10 + c:236
Mod2 + XF86Mail
Most people with a full size keyboard will have NumLock turned on like in the example above. With it's default settings XBindKeys ignores the setting of the NumLock, CapsLock and ScrollLock, so you can ignore them in the rules for your key bindings too. The output above shows two lines after "NoCommand"; one with a code for the combined modifiers + a keycode and one with keysym names for all pressed keys and modifiers. Your rule only needs one row, but you can mix and match parts from both lines to create it. Here is an example a '~/.xbindkeysrc' file:
# This is the xbindkeys configuration file. # =================== Misc HotKeys =============== # Launch default e-mail client. "thunderbird &" XF86Mail # Launch webmail. "firefox https://webmail.xs4all.nl/ &" Shift + XF86Mail # Launch spam trap. "firefox https://webmail.iae.nl/ &" Control + XF86Mail # Search the web. (Search key, looking glass) "firefox http://www.google.nl/ &" c:229 "gnome-search-tool" XF86Explorer # ================ XMMS HotKeys ================== "xmms --play-pause" XF86AudioPlay "xmms --stop" XF86AudioStop "xmms --fwd" XF86AudioNext "xmms --rew" XF86AudioPrev # ================ Sound HotKeys ================= # Vol down "amixer -q set Master 3%- unmute" XF86AudioLowerVolume # Vol up "amixer -q set Master 3%+ unmute" XF86AudioRaiseVolume # Vol mute/unmute "amixer -q set Master toggle" XF86AudioMute # ============== Sonata/MPD HotKeys ============== # Sonata next track "sonata next" Control + Alt + Next # Sonata previous track "sonata prev" Control + Alt + Prior # MPD seek forward 10 seconds "mpc seek +10" Control + Shift + Right # MPD seek back 10 seconds "mpc seek -10" Control + Shift + Left
Once you're finished with the configuration, you can start xbindkeys as a daemon using one of the following commands. Use the first one if you put the configuration in '~/.xbindkeysrc'. Use the second one if you used another name or location for the file.
$ xbindkeys $ xbindkeys -f /path/to/my/configuration/file
[edit] Starting XBindkeys Automatically
Within XFCE
- Open Settings->Session and Startup
- Click on Application Autostart tab
- Click the 'Add' button and fill in name as xbindkeys and command xbindkeys
- Click okay
Within GDM Starting xbindkeys in gdm means that you will have access to your keybindings from any user logged into X, and in fact before a user even logs in! This is handy if you have GDM greeter sounds and you are in a public place and need to mute sound before logging in to avoid an embarassing login noise.
We must do the following:
- Load Xbindkeys after xmodmap in the /etc/X11/gdm/init/default.sh file
Then, create a startup script for it in ~/Desktop/Autostart:
#!/bin/sh # Script to set the multimedia keys of the keyboard xbindkeys exit 0
Now you should be set. If your keys don't work as they should (you can check in the CLI if they work), then check your '.xbindkeysrc' settings and check the paths to and permissions on the applications. The terminal will be very clear about the function of the keys: if they do work, the cursor stalls for a second, if they don't, it puts out an ~ every time you press it.
[edit] XMacro
- Here is Sourceforge page download xmacro pre0.3.
We've already seen how to make a key press result in the execution of a command and how to make a key behave like one other key. The next step is to make the X server believe multiple keys are pressed when in reality only a single key is pressed. For this we use yet another utility: XMacro. It can record series of key presses (and mouse movements) into a file. Later this file can be replayed. The command to start recording is as follows.
$ xmacrorec2 > ~/testrecording
You can of course pick another name for the output file. Don't forget the '2' at the end of xmacrorec2. There is also a xmacrorec command, but that needs two running X servers and is out of scope for this article. The first key that you press after giving the command, is the key that will terminate the recording if it is pressed a second time. So don't use a key you want to record for it! Below are the contents of the resulting file for recording Alt-Right Alt-Left Esc.
MotionNotify 1153 338 KeyStrPress Alt_L KeyStrPress Right KeyStrRelease Right KeyStrRelease Alt_L KeyStrPress Alt_L KeyStrPress Left KeyStrRelease Left KeyStrRelease Alt_L KeyStrPress Escape KeyStrRelease Escape
The first line has to do with mouse movements. If you are not interested in them, then you can safely delete it. A line of 'KeyStrPress <somekey>' followed by a line of 'KeyStrRelease <somekey>', where <somekey> is the same on both lines, can be consolidated into a single line 'KeyStr <somekey>'. It is not mandatory to do this, but it results in better readable macro files. If the lines are reversed, then they can't be consolidated, obviously. Finally, if you want the computer to have enough time to execute something between two key presses, then you can insert a line like 'Delay 2'. This means wait two seconds. The number can be changed to something appropriate for your situation. Cleaning up the file above results in the next three files.
'~/.xmacro/back.xmacro'
KeyStrPress Alt_L KeyStr Left KeyStrRelease Alt_L
'~/.xmacro/forward.xmacro'
KeyStrPress Alt_L KeyStr Right KeyStrRelease Alt_L
'~/.xmacro/stop.xmacro'
KeyStr Escape
With the help of the command xmacroplay and XBindKeys we can now bind these macros to single key strokes. To do so, add the lines below to the '~/.xbindkeysrc' file. I think that the comments make it clear what these commands are meant to do.
# Show previous page in Firefox/Thunar. "xmacroplay :0.0 < ~/.xmacro/back.xmacro" Release + XF86Back # Show next page in Firefox/Thunar. "xmacroplay :0.0 < ~/.xmacro/forward.xmacro" Release + XF86Forward # Stop loading the current page in Firefox. "xmacroplay :0.0 < ~/.xmacro/stop.xmacro" Release + XF86Stop
The Release modifier used here is crucial for the macros to have any effect. It makes the commands execute when the key is released, in stead of when it is pressed. The Xfce way of binding keys doesn't offer this option, so you can't set xmacroplay commands in the Keyboard Preferences dialog box. As you can see in the example of the stop macro, you can of course also "remap" a single key to a single key with the XMacro utility. This is an alternative for the method described in the paragraph about xmodmap. Don't use both methods at the same time for the same key.
If XBindKeys was already running before you changed the '~/.xbindkeysrc' file, then restart the program.
$ kill `/sbin/pidof xbindkeys` $ xbindkeys
[edit] Feedback
Feedback is welcome online via the forum.
Tibors

