Talk:Multimedia Keys

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search

MythTV: Keyboard Special Keys

Media Keys Multimedia Keyboard

Modern PC keyboards are available with extra keys, many of which are of interest to MythTV users with functions printed on them like "Vol +", "Vol -". There are more keyboards out there than are likely to ever be documented in a wiki like this... So, if you have a keyboard with the "special" keys that you would like to put to use, how do you do that, with examples drawn from Muthbuntu 11.04 "Natty Narwhal". Further while the focus here is on keyboards, the same rules/instructions apply to other devices that look to the system as "keyboards", such as PC cases with media buttons.

First program we need is "xev" which you can get through the package manager or by pulling up a terminal under X-windows and running "sudo apt-get install xev". You will want pen and paper (or maybe a second computer) to make notes for the next step. We run "xev" this will display in the terminal window what events it sees. Events to xev include keyboard keys being pressed, keyboard keys being release, mouse movements, etc.. Once xev is running pressing and releasing one of the special keys will generate something like the following on screen:

KeyPress event, serial 36, synthetic NO, window 0x4a00001,
    root 0x1ad, subw 0x0, time 20577657, (1029,913), root:(1033,938),
    state 0x10, keycode 164 (keysym 0x1008ff30, XF86Favorites), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False
KeyRelease event, serial 36, synthetic NO, window 0x4a00001,
    root 0x1ad, subw 0x0, time 20577669, (1029,913), root:(1033,938),
    state 0x10, keycode 164 (keysym 0x1008ff30, XF86Favorites), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

In our case all we care about is the number that follows the word "keycode", in the above example 164. Once you have your list of keys (or while your collecting your list if you are using a second computer) you can create the /home/{mythtv user}/.Xmodmap file. In the .Xmodmap file you will tie the special keys to what you want them to mean, so, for the above we might do something like:

! Keymap for Brand XYZ Model 123 keyboard
! Skip Back
keycode 164 = Left

Any line that starts with an exclamation mark is treated as a comment and ignored. A full list of the codes that xmodmap knows about (like "Left" as in left arrow key) can be seen in the file keysymdef.h (which you will likely have to dig-up on line). When looking through the keysymdef.h file remember to ignore "XK_". A typical .Xmodmap file when done will look something along the likes of the following:

! Keymap for Brand XYZ Model 123 keyboard  
! Skip Back
keycode 999 = Left
! Skip Forward
keycode 999 = Right
! Stop
keycode 999 = Escape
! PlayPause
keycode 999 = P
! VolUp
keycode 999 = bracketright
! VolDn
keycode 999 = bracketleft
! Mute
keycode 999 = bar

To start the above immediately type:

xmodmap /home/{mythtv user}/.Xmodmap

Otherwise, the script /etc/gdm/Xsession is run during the the Mythbuntu start-up and if it sees the file "/home/{user name}/.Xmodmap" it will auto-run "xmodmap /home/{user name}/.Xmodmap".

Once the above is up and running satisfactorily, the last step is to add what you have learned back to this wiki, so nobody else will have to go through the same grunt work of mapping keys if they have the same make / model of keyboard as you. Retrieved from "http://www.mythtv.org/wiki?title=Keyboard_Special_Keys&oldid=52707"

related links

To configure keys with xmodmap, you need a configuration file. I have provided two examples that work on my Hardy and Jaunty systems. Hopefully, one of them will work for you without change. At least they should serve as a starting point to get the multimedia keys working on your system.

Copy the following text to $HOME/xmodmap.hardy

keycode 160 = XF86AudioMute NoSymbol XF86AudioMute NoSymbol XF86AudioMute
keycode 174 = XF86AudioLowerVolume NoSymbol XF86AudioLowerVolume NoSymbol XF86AudioLowerVolume
keycode 176 = XF86AudioRaiseVolume NoSymbol XF86AudioRaiseVolume NoSymbol XF86AudioRaiseVolume
!keycode 153 = XF86AudioNext NoSymbol XF86AudioNext NoSymbol XF86AudioNext
keycode 153 = Z
!keycode 162 = XF86AudioPlay XF86AudioPause XF86AudioPlay XF86AudioPause XF86AudioPlay XF86AudioPause
keycode 162 = P
!keycode 144 = XF86AudioPrev NoSymbol XF86AudioPrev NoSymbol XF86AudioPrev
keycode 144 = Q
!keycode 164 = XF86AudioStop XF86Eject XF86AudioStop XF86Eject XF86AudioStop XF86Eject
keycode 164 = T
! Internet Explorer key
keycode 178 = XF86HomePage NoSymbol XF86HomePage NoSymbol XF86HomePage
! Power/Standby key
keycode 223 = XF86Sleep NoSymbol XF86Sleep NoSymbol XF86Sleep

Very informative info on keycodes scancodes and key bindings

Ok, found this at https://help.ubuntu.com/community/MultimediaKeys

   When you hit a key on your keyboard, the linux kernel generates a raw scancode for it (if it is assigned). Each scancode can be mapped to a keycode. This is at kernel level. X has a (quasi) total independent way of mapping keys: X reads the kernel keycode table at startup, then maps the keycode to its independent keycode table (it is the same as the kernel keycodes but different :)). Then each keycode can be mapped to a keysym, i.e. a string which represent a key or suggest an action. Thus to have our keys fully functional, they need a kernel scancode/keycode plus a X keycode/keysym. It may seem weird, but X developers have their reason to keep a separate keyboard mapping from the kernel. It is not difficult at all, just a quite tedious procedure.

So keycodes's are mapped to keysym's, So where are the keysym's? I found and answer from this question: Where do I find a list of all X keysyms these days? Since we are talking about the volume keys it would be found in XF86keysym.h in the source-code mentioned in the answer.

In that file on my computer I found the following for volume:

#define XF86XK_AudioLowerVolume 0x1008FF11   /* Volume control down        */
#define XF86XK_AudioMute    0x1008FF12   /* Mute sound from the system */
#define XF86XK_AudioRaiseVolume 0x1008FF13   /* Volume control up          */

Strange... different values from anything else, maybe there are multiple systems for handling keys? http://crunchbang.org/forums/viewtopic.php?id=16656

I am using Xubuntu, and to control the keys, I need to map the actions manually(like this How do I change my keyboard shortcuts in xubuntu?). However the notifications seem independent as if they are picking up on the key-press and acting accordingly, It may mean that other programs in Ubuntu are set up this way, so there is no need to map scripts to the keys.

So I'm quite sure that programs are now picking up on the key (so no scrips to be found).

In Xubuntu I had this problem with Pulse Audio and using custom scripts for changing the volume, It seemed that Pulse was intercepting the Mute key, Mute key mutes Alsa and PulseAudio, but unmutes only Alsa made for interesting workarounds.

Check this out about NotifyOSD https://wiki.ubuntu.com/NotifyOSD#Volume_changes

If you look a These diagrams: https://wiki.ubuntu.com/NotifyOSD#Architecture

Especially this one: enter image description here

It shows that there is a "hardware keys listener" which receives form DBus or HAL? It then "retrieves visual element from system" which the sound and brightness icons are in the source of Notify-OSD, and then makes the bubble from there.

SOURCE: http://askubuntu.com/questions/243751/how-are-key-codes-mapped-to-the-appropriate-action