venerdì 6 agosto 2010

Microsoft Wireless Comfort Keyboard 1.0a on Ubuntu Lucid Lynx 10.04

Many of the multimedia keys of my Microsoft Wireless Comfort Keyboard 1.0a are not predefined on Ubuntu Lucid Lynx 10.04


 


Unfortunately  KeyTouch doesn't work for me. Fortunately there are a number of good tutorials to fix this problem. I followed this one:


and it's a very good howto that I don't want to copy here. I want to report here my own parameters to fully take advantage of my keyboard.
Pressing  my multimedia keys one by one and using sudo dmesg -c in a gnome-terminal, I discovered the scancodes from outputs like this:

atkbd.c: Unknown key pressed (translated set 2, code 0xe002 on isa0060/serio0).
atkbd.c: Use 'setkeycodes e002 ' to make it known.

Now, before going on, a little explanation: my keyboard has a special button, called "F Bloc" (Function blocks), to enable/disable particular functions for F1, F2...F12  keys and I want to fully take advantage of it.
Here it is, my /etc/rc.local, you can see the new functions
for F1, F2...F12  keys in case of F Bloc disabled :

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing

setkeycodes e00b 180 # Zoom in
setkeycodes e011 181 # Zoom out
setkeycodes e005 182 # Messenger
setkeycodes e015 183 # Calendar
setkeycodes e016 184 # Disconnect
setkeycodes e073 185 # Favorites 1
setkeycodes e074 186 # Favorites 2
setkeycodes e075 187 # Favorites 3
setkeycodes e076 188 # Favorites 4
setkeycodes e077 189 # Favorites 5
setkeycodes e078 190 # Favorites - Star
setkeycodes e03b 191 # Help  -> F1
setkeycodes e008 192 # Undo  -> F2
setkeycodes e007 193 # Redo  -> F3
setkeycodes e03e 194 # New   -> F4
setkeycodes e03f 195 # Open  -> F5
setkeycodes e040 196 # Close -> F6
setkeycodes e041 197 # Reply -> F7
setkeycodes e042 198 # Fwd   -> F8
setkeycodes e043 199 # Send  -> F9
setkeycodes e023 200 # Spell -> F10
setkeycodes e057 201 # Save  -> F11
setkeycodes e058 202 # Print -> F12
exit 0

after that, I added the missing keysyms to the right keycodes in /etc/xmodmap.conf:

clear Mod1
add Mod1 = Alt_L
clear Mod4
add Mod4 = Tab
clear Mod5
add Mod5 = Alt_R
keycode 188 = XF86ZoomIn NoSymbol XF86ZoomIn
keycode 189 = XF86ZoomOut NoSymbol XF86ZoomOut
keycode 190 = XF86Messenger NoSymbol XF86Messenger
keycode 191 = XF86Calendar NoSymbol XF86Calendar
keycode 192 = XF86LogOff NoSymbol XF86LogOff
keycode 193 = XF86Launch0 NoSymbol XF86Launch0
keycode 194 = XF86Launch1 NoSymbol XF86Launch1
keycode 195 = XF86Launch2 NoSymbol XF86Launch2
keycode 196 = XF86Launch3 NoSymbol XF86Launch3
keycode 197 = XF86Launch4 NoSymbol XF86Launch4
keycode 198 = XF86Launch5 NoSymbol XF86Launch5
keycode 199 = XF86Support NoSymbol XF86Support
keycode 200 = Undo NoSymbol Undo
keycode 201 = Redo NoSymbol Redo
keycode 202 = XF86New NoSymbol XF86New
keycode 203 = XF86Open NoSymbol XF86Open
keycode 204 = XF86Close NoSymbol XF86Close
keycode 205 = XF86Reply NoSymbol XF86Reply
keycode 206 = XF86Forward NoSymbol XF86Forward
keycode 207 = XF86Send NoSymbol XF86Send
keycode 208 = XF86Spell NoSymbol XF86Spell
keycode 209 = XF86Save NoSymbol XF86Save
keycode 210 = XF86Launch6 NoSymbol XF86Launch6

Note 1: If I put xmodmap.conf in my home directory, it doesn't work.
Note 2: I added some extra commands at the top of the file (clear ..., add ...): clear is used to remove all entries in the modifier map for the given modifier,  where valid name are: Shift, Lock, Control, Mod1, Mod2, Mod3, Mod4, and Mod5
add is a command to reassign all  keys containing the given keysyms to the indicated modifier map. 
If I don't use these commands, some functions keys (F5, F7, F8, F9) don't work as expected and gnome-keybindind-properties cannot detect them correctly.

Now I can use gnome-keybinding-properties or xbindkeys-config to bind actions to my fresh configured keys: 


I took a screenshot of xbindkeys-config to show my settings, but actually I'm using gnome-keybinding-properties because it allows me to zoom in and zoom out without extra configurations.
As for F1, F2...F12  keys in case of F Bloc disabled, they are used with an email client, according to their labels and Microsoft docs, so I thought to use xdotool to send predefined and existing shortcuts to Evolution.
I'm writing a script to handle these shortcuts. It's possible to pass arguments to the script in this way: "myEvolutionShortcuts new" to start a new message, "myEvolutionShortcuts reply", "myEvolutionShortcuts send" and so on:

#!/bin/sh

# Search and store Evolution - Mail window id
myEvolutionID=`xdotool search --title "In arrivo"`
# Search and store Evolution - Compose message window id
myComposeMessageID=`xdotool search --title "Componi messaggio"`

case $1 in

# New message
new)
sleep .3
xdotool windowactivate $myEvolutionID
xdotool windowfocus $myEvolutionID
xdotool key "Shift+Control+M" ;;

# Open message
open)
sleep .3
xdotool windowactivate $myEvolutionID
xdotool windowfocus $myEvolutionID
xdotool key "Control+O" ;;

# Close Evolution
close)
sleep .3
xdotool windowactivate $myEvolutionID
xdotool windowfocus $myEvolutionID
xdotool key "Control+W" ;;

# Reply message
reply)
sleep .3
xdotool windowactivate $myEvolutionID
xdotool windowfocus $myEvolutionID
xdotool key "Control+R" ;;

# Forward message
forward)
sleep .3
xdotool windowactivate $myEvolutionID
xdotool windowfocus $myEvolutionID
xdotool key "Control+F" ;;

# Send message
send)
sleep .3
xdotool windowactivate $myComposeMessageID
xdotool windowfocus $myComposeMessageID
xdotool key "Control+F" ;;

# Spell check
abc)
sleep .3
xdotool windowactivate $myComposeMessageID
xdotool windowfocus $myComposeMessageID
xdotool key "F7" ;;

# Save message
save)
sleep .3
xdotool windowactivate $myComposeMessageID
xdotool windowfocus $myComposeMessageID
xdotool key "Shift+Control+S" ;;

# Print message
print)
sleep .3
xdotool windowactivate $myComposeMessageID
xdotool windowfocus $myComposeMessageID
xdotool key "Control+P" ;;

esac

exit 0

The very early version of the script works in this way: it searches the evolution's window id using the first words of its window title, activates and gives focus to the window, finally send the window a predefined shortcut. It's easy to get info about a particular window using the xprop command.
At this time the script doesn't handle exceptions but it works fine for now and I can use all of my multimedia keys.

Best regards.