个人工具

UbuntuHelp:MultimediaKeys

来自Ubuntu中文

Oneleaf讨论 | 贡献2007年5月13日 (日) 11:38的版本 (New page: {{From|https://help.ubuntu.com/community/MultimediaKeys}} {{Languages|php5}} === Keyboard Shortcuts Editor === '''In Ubuntu 5.10 (Breezy Badger)''', '''Ubuntu 5.04 (Hoary Hedgehog)''' and ...)

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航, 搜索

Keyboard Shortcuts Editor

In Ubuntu 5.10 (Breezy Badger), Ubuntu 5.04 (Hoary Hedgehog) and later Ubuntu versions please go to System menu > Preferences > Keyboard Shortcuts to find the keyboard shortcut editor. Many of the common multimedia & control keys should be pre-defined, and if they aren't you should be able to assign functions to them through Keyboard Shortcuts very easily.

If, after assigning as described above, your Volume``Up/Volume``Down/Mute keys seem to work (i.e. you see a popup window with a slider in the middle of the screen that reacts on those key presses), but the playback volume actually remains unchanged, you may also have to setup what mixer tracks (channels) these keys should be bound to. Refer to AssigningTracksToKeys this section below.

For Kubuntu, see KDEMultimediaKeys. For Xubuntu, see XfceMultimediaKeys.

Please note that if you are using this guide with laptops that have a button to switch off bluetooth/wireless, you must not assign them anything in order for these switches to work. You may also need acerhk if xev does not recognize you keys.

If that doesn't work, please read on.

You can also use keyTouch. KeyTouch is a program which allows you to easily configure the extra function keys of your keyboard. It is the first and only program of its kind that works perfectly together with kernel 2.6. If your keyboard is not supported yet, you can easily get it supported. You can do this by using keytouch-editor (documentation: http://keytouch.sourceforge.net/keytouch_editor/).

Another autosetup program found at http://lineak.sourceforge.net/

failing that


Quick recipe

0. See if your key works with gnome-keybinding-properties, it's under the menu: Desktop/Preferences/Keyboard Shortcuts. If it doesnt' work:
0. Go to a real console and press your multimedia keys one by one.
0. Look at the console output to discover which scancodes are generated, you should see something like this:
        atkbd.c: Unknown key pressed (translated set 2, code 0x9e on isa0060/serio0).
        atkbd.c: Use 'setkeycodes e01e <keycode>' to make it known.</code>you should find the same information with:<pre>
        dmesg</code>
 0. Use setkeycodes to set your keycodes as suggested. (but first use dumpkeys to see which keycodes are free to use).
 0. Put those commands in <code>/etc/init.d/bootmisc.sh</code>
 0. Open an X terminal:<pre>
        xmodmap -pke > xmodmap.conf</code>Then edit this file adding the missing keysyms to the right keycodes (use xev to see the keycodes, read the file <code>/usr/lib/X11/XKeysymDB</code> to see which keysyms are available).
 0. From the same terminal:<pre>
        sudo cp xmodmap.conf /etc 
        cd /etc/X11/gdm/PostLogin 
        sudo cp Default.sample Default</code>then open the file <code>/etc/X11/gdm/PostLogin/Default</code> with your editor of choice and add this line to it:<pre>
        xmodmap /etc/xmodmap.conf</code>
 0. Use gnome keybindings or metacity keybindings to bind actions to your fresh configured keys.

Woo, that was fast! ;)

=== In-depth instructions ===

attachment:IconsPage/IconNote.png A little background note: When you hit a key on your keyboard, the linux kernel generates a raw scancode for it (if it's 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 map the keycode to its independent keycode table (it's 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 could seem weird, and it is, but X developers have their reason to keep a separate keyboard mapping from the kernel.
It's not difficult at all, it's only a quite tedious procedure.

 0. Use gnome-keybinding-properties:
** Try to see if your key works with gnome-keybinding-properties, it's under the menu: Desktop/Preferences/Keyboard Shortcuts. If it doesn't work, or if gnome-keybinding-properties doesn't have a nice default action for your key, you have to read the whole howto ;)
 0. Assigning kernel keycodes:
** We are trying to see which keys have already a kernel scancode/keycode and which don't.
** Go to a real console by pressing <ctrl><alt>F1.
** Now if you press your multimedia keys one by one, you should see an output message like this:<pre>
        atkbd.c: Unknown key pressed (translated set 2, code 0x9e on isa0060/serio0).
        atkbd.c: Use 'setkeycodes e01e <keycode>' to make it known.</code>
** The same informations can be found typing:<pre>
        dmesg</code>
** This is all you need, but before assigning the missing keycode, you need to check which keycodes are available, to avoid conflicts. Type: <pre>
        sudo dumpkeys</code>
** The command returns a list of used scancodes. Just pick the ones without an unassigned keycode (usually from 120 to 255).
** Now you know:
*** which keys have missing keycodes
*** how to map the missing scancode/keycode to make it known
*** which keycodes are available to use
** So it's time to actually set up these codes, type:<pre>
        setkeycodes e01e 120</code> where e01e is the scancode suggested in dmesg, and 120 is a free keycode you have to choose.
** Repeat this passage for all your keys which don't generate a scancode/keycode.
** If you want these commands executed at system startup (you want it), add all those setkeycodes statements at the end of the file <code>/etc/init.d/bootmisc.sh</code>
** Now that you have all your kernel scancodes well generated, move on to:
 0. Assigning X keysyms
** This time you can stay under X ;) 
** X keysyms are a sort of descriptive string like: XF86AudioMedia, XF86WWW etc. but we can't use random names. A list of X keysyms can be found in the files <code>/usr/lib/X11/XKeysymDB</code> or <code>/usr/share/X11/XKeysymDB</code>
** We are going to set up these keys. Fire up a terminal and type:<pre>
        xev</code>
** Then press a multimedia key. If you are lucky it has already a keysym binded to it, so the output of xev for that key will be something like this:<pre>
        KeyRelease event, serial 28, synthetic NO, window 0x3200001,
        root 0xb7, subw 0x0, time 137010761, (693,138), root:(705,256),
        state 0x10, keycode 136 (keysym 0x1008ff27, XF86Forward), same_screen YES,
        XLookupString gives 0 bytes:</code>
** The third row is the one of interest: it says that you have a keycode for that key (136) as well as keysym (XF86Forward). If you have a keysym then it's all good, you can use that string to represent your key and use gnome keybindings or metacity keybindings to bind the relevant action to it. (See below).
** But probably, you'll find that the key doesn't have any keysym assigned to it, like this:<pre>
        KeyRelease event, serial 28, synthetic NO, window 0x3200001,
        root 0xb7, subw 0x0, time 137355697, (401,146), root:(413,264),
        state 0x10, keycode 136 (keysym 0x0, NoSymbol), same_screen YES,
        XLookupString gives 0 bytes:</code>
** In this case you have to assign your keysym to the relevant keycode (136) (it doesn't match the kernel keycode for that keys, but it doesn't matter, it's by design). This is done with xmodmap.
** First, create a file with your current X keyboard map, in a terminal type:<pre>
        xmodmap -pke > xmodmap.conf</code>
** Then you are going to add all the missing keysyms to this file: use xev to see which keycode to use, look in the <code>/usr/lib/X11/XKeysymDB</code> or <code>/usr/share/X11/XKeysymDB</code> to find keysym names, open the xmodmap.conf file and fill in the missing keysym using a name which makes sense (i.e. if you have a button with a calculator printed on it, use XF86Calculator as keysym).
** Repeat this passage for all your multimedia keys.
** When finished, you can apply the changes with:<pre>
        xmodmap xmodmap.conf</code>
** Now you want to load your new xmodmap.conf when X starts. I've found that the better way is to put the command in the PostLogin script of gdm. (if you use gdm, of course ;)) Type: <pre>
        sudo cp xmodmap.conf /etc/xmodmap.conf 
        cd /etc/X11/gdm/PostLogin 
        sudo cp Default.sample Default
</code>
** Now open the Default file with your favourite editor and at the end of it add this line:<pre>
        xmodmap /etc/xmodmap.conf</code>
** This way you should have all your scancodes/keycodes/keysyms assigned at system and X startup.
** On hoary, it seems that GNOME uses xkb and overwrites these changes.  Instead, you can copy your xmodmap.conf file into `~/.xmodmap`.
** Now you can make something useful with them like:
 0. Using gnome keybindings or metacity to bind commands to keys
** First, try to bind keys with gnome-keybinding-properties: it's quicker and it has some nice default action, so launch it from the terminal or from the menu.
** The utility is self-explanatory, but probabily you'll find that some actions don't work (like sleep for example), or they do the wrong thing, or there is no suitable action at all for your key.
** You can address those problems using metacity to bind keys to commands which is a lot more flexible. Open a terminal and type:<pre>
        gconf-editor</code>or launch it from the menu under Applications/System Tools/Configuration Editor.
** Go under apps/metacity in gconf-editor. You will see 2 rows (among others): <code>global_keybindings</code> and <code>keybinding_commands.</code>
** If you click on <code>global_keybindings</code>, on the right pane you'll find some entry for commands, like: <code>run_command_1</code>, <code>run_command_2</code>, etc. These have to be filled up with the relevant keysym for your key (like: XF86Play, XF86MyComputer, etc. use xev to see).
** Then you can assign the matching command (or script) on the other row, under <code>keybinding_commands.</code> You have a lot of useful commands at your disposal like:<code>totem --fullscreen</code> or <code>rhythmbox --next</code> etc.
** Use the command line help of those applications to discover which parameters are available, (e.g. totem --help or rhythmbox --help etc.) For firefox take a look here: http://www.mozilla.org/unix/remote.html
 0. Additional hints
** The actions you want to execute after a key press are limited only by fantasy: bash provides a very powerful scripting language and with hundreds of useful programs out there, there is virtually no limit.
** For example an app you may find useful is xmacro (sudo apt-get install xmacro): it let you play a mouse or a keyboard macro with a command. I use it to bind my Forward and Back multimedia keys to <Alt>Right and <Alt>Left respectively: this way I can control forward and back in epiphany which doesn't provide a command line option for this task. (it works with every app which takes <Alt>Right and <Alt>Left shortcuts). This is my xmacro script: <pre>
case "$1" in

forward)
sleep .3
echo -e "KeyStrPress Alt_L \n
        KeyStrPress Right \n
        KeyStrRelease Right \n
        KeyStrRelease Alt_L" | xmacroplay :0 ;;

backward)
sleep .3
echo "KeyStrPress Alt_L \n
        KeyStrPress Left \n
        KeyStrRelease Left \n
        KeyStrRelease Alt_L" | xmacroplay :0  ;;
esac

exit 0</code>

** Another hint is that if you have a sleep key, and it doesn't work with gnome-keybinding-properties, you can use it at least to shut off your monitor. Just assign it to the command: <pre>
        xset dpms force off</code> with metacity.
** If you have a scroll wheel on your keyboard, you can even assign a keybinding to the button under the wheel! Assign it to the "Return" keysym, it will act like a return key.

That's all!

[[Anchor(AssigningTracksToKeys)]]
=== Assigning mixer tracks to multimedia keys ===

In '''Ubuntu 7.04 (Feisty Fawn)''', there is a new UI for choosing what mixer tracks are controlled by volume keys on the keyboard. This UI can be found in the ''System > Preferences > Sound'' dialog. On the ''Devices'' page of this dialog, select the desired device and mixer tracks in the '''Default Mixer Tracks''' section. The corresoinding GConf keys for these settings are <code>/desktop/gnome/sound/default_mixer_device</code> and <code>/desktop/gnome/sound/default_mixer_tracks</code>.

Those who are interested in details, can take a look at the function <code>acme_volume_gstreamer_open()</code> in [http://svn.gnome.org/viewcvs/gnome-control-center/trunk/gnome-settings-daemon/actions/acme-volume-gstreamer.c?view=markup acme-volume-gstreamer.c] which defines the logic used to select mixer tracks.

In '''Ubuntu 6.10 (Edgy Eft)''', the above UI is absent and the GStreamer code seems to choose the ''master'' track of the ''first'' mixer device it finds. If it fails to find the master track, it looks for the first one named <code>volume</code>. There is also a '''Default sound card''' setting on the ''Sounds'' page of the ''Sound Preferences'' dialog in Edgy, but I'm not sure if it participates in the selection of the device mixer to control as I don't have an Edgy machine to check this. The same relates to earlier Ubuntu releases.

=== Comments ===
Great article. I finally got my third multimedia button working on my Edgy laptop. I noticed a couple of things, though:

** On Edgy the keysim database is on '''/usr/share/X11/XKeysimDB''' instead of '''/usr/lib/X11/XKeysimDB'''
** When executing ''''xmodmap xmodmap.conf'''' and after executing ''''gnome-keybinding-properties'''', pressing on my newly configured key to define a keybinding results in the <u>keysim</u> (''XF86Terminal'' in my case) appearing in the "Shortcut" field. However, when adding ''''xmodmap /etc/xmodmap.conf'''' to '''/etc/X11/gdm/PostLogin/Default''' and after rebooting, the same procedure results in the <u>keycode</u> (''0x8b'' in my case) being shown in the "Shortcut" field instead of the expected keysim. In any case, this is simply a cosmetic annoyance, since the functionality is the same regardless of using the keysim or keycode for the keybinding.

----
CategoryDocumentation CategoryLaptop

[[category:UbuntuHelp]]