Linux Tools to Remap Keys and Mouse Buttons

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

Various facilities available:

  • xbindKeys - a program that grab keys and mouse button events in X and starts associated shell command. This is to make a key press do something, such as start a program.
  • xbindkeys-config - a graphical front-end to xbindkeys.
  • xte - use with xbindkeys. xte is a program that generates fake input using the XTest extension, more reliable than xse.
  • xmodmap - a program which remaps keys to various functions. xmodmap is a utility for modifying keymaps and pointer button mappings in Xorg. Use this to make a key press act as another key press. As of 2013, Ubuntu and derivatives no longer use xmodmap, but instead use xkb.
  • xfce4-keyboard-settings - a graphical front-end in Xfce with an "Applications Shortcut" facility for mapping keys to various functions similar to xbindkeys-config, but not using xbindKeys.
  • xinput - a utility to list available input devices, query information about a device and change input device settings.
  • xkb - The Xorg XKB replaces xmodmap and more. It is new.
  • imwheel -
  • evrouter - simulate X11 key events and mouse events and run shell controls.

These tools apply to XFree86 X servers and X.org on Linux.

xbindkeys

To install on Ubuntu and Debian:

sudo apt-get install xbindkeys xbindkeys-config 

Runs process

/usr/bin/xbindkeys -f /home/mythbox/.xbindkeysrc

There is a graphical configuration tool called xbindkeys-config (GTK frontend).

An example to put in .xbindkeysrc to have CONTROL F open the Firefox web browser.

"firefox"
  control + f

xte

Install on Ubuntu

sudo apt-get install xautomation

Example:

xte 'keydown XF86Paste' 'keyup XF86Paste'

External resources:

Using xbindkeys with xte

Some examples:

# close the window under the mouse cursor
 "xte 'mouseclick 1' && xte 'keydown Alt_L' 'key F4' 'keyup Alt_L'"
  b:8+Release
# double click
 "xte 'mouseclick 1' 'mouseclick 1'"
  b:9

Using xbindkeys with xvkbd

xvkbd is a virtual (graphical) keyboard program for X Window System which provides facility to enter characters onto other clients (softwares) by clicking on a keyboard displayed on the screen. This may be used for systems without a hardware keyboard such as kiosk terminals or handheld devices. This program also has facility to send characters specified as the command line option to another client.

sudo apt-get install xvkbd

Using xvkbd (X Virtual Keyboard) it is possible to do something such as mapping the middle mouse button to act as a keyboard key, such as the escape key, as in the example below: First, open .xbindkeysrc

vi ~/.xbindkeysrc

Add the following:

# 3rd mouse button to Escape
"xvkbd  -text "\e""
   m:0x0 + b:3

This example was a useful way to make a specific button on the Aerb Remote Control MX3 perform a needed keypress for MythTV.

xmodmap

background

xmodmap is a utility for modifying keymaps and pointer button mappings in Xorg / XFree86 / Xwindows Server.

"When you change the display’s or keyboard backlight’s brightness using keyboard hotkeys, the change in brightness should be shown in a confirmation bubble with a gauge. "
This is the KNotification API. https://wiki.ubuntu.com/NotifyOSD#Volume_changes The Xfce desktop I see it for volume up and down, those seem to be mapped in the distribution I use - Xubuntu.

What's it trapped by? Well the kernel generates the scancode, which is different from a keycode. Don't confuse the two. There is a mapping for scancode to keycode at the kernel level. When you map a scancode to a different key with xmodmap there isn't a problem unless the scancode is already assigned to something, a good example is volume up and down. The new mapping doesn't apply after running xmodmap, however, will stop functionality of the volume control. After you reboot the system the new mapping will take effect by those that were previously intercepted. Gnome, KDE and Xfce will intercept some of the hotkeys. Adjusting the volume or pressing mute will result in a screen icon appearing generated by the KNotification API and resulting in the change occurring in PulseAudioMixer and/or ALSA.

Keycode values once were mapped to a text string called a keysym in a file called XF86keysym.h that is compiled into the kernel. When you install a distribution such as Mythbuntu the keysym strings are already in the kernel. They are like global constants that can be used by other software. The string values are used globally without the leading 3 characters. Not all of the keysym constants cover every hotkey. Multimedia keyboards with volume controls:

Volume Up and Down: keycode 176 = XF86AudioRaiseVolume and keycode 174 = XF86AudioLowerVolume

configuration and activation

1. Find your keycodes

First you need to install a utility such as xev to get the keycodes for the buttons you wish to map. Many of these buttons are relatively standard, however, not standardized. Some keycodes differ from one manufacturer to the next.

2. Install and configure Xmodmap

It should already be installed. If not...

apt-get install xmodmap

See the default X key mapping table.

xmodmap -pke

See also: event_key_remap' in 'xorg.conf'.

Now create the .Xmodmap or modify the existing file. It is not necessary to do this under a privileged account. You want to do it as the MythTV user account. Create the xmodmap mapping file.

vi ~/.Xmodmap

Populate it with the mappings you desire. Take a look at our example file for the Aerb Remote Control MX3:

! Keymap for Aerb Remote Control MX3

!  172    Play / Pause
!  174    Stop
!  056    Back (Rewind)
!  041    Forward 
!  173    Last track
!  171    Next track
!  121    Mute
!  122    Volume up
!  123    Volume down

keycode 176 = Left
keycode 216 = Right
keycode 173 = Home
keycode 171 = End
keycode 69 = Escape
keycode 172 = space
keycode 135 = m M 
keycode 180 = Menu
keycode 163 = i I
keycode 68 = w W

3. Test the configuration

As a non privileged user run the following command:

xmodmap .Xmodmap

Any key mappings not being intercepted by Xfce or a plugin will become usable immediately. If there is an error in your .Xmodmap file the mapping will abort completely. You will see the error message on console along with line number. Correct the errors and try again until there are no errors.

4. Reboot the system

If you do not reboot the system then you will find some of your key mappings will not come though. On Mythbuntu you will find that buttons such as PLAY are mapped to activate the DVD drive. If you try to remap to something like in our example the change will not be effective until the system is restarted. I know, kinda lame like the old Microsoft Windows. Technically a system restart is not necessary if X.org and some other processes are restarted, however, it is just easier to reboot.

Creating mappings for actions

Use xfce keyboard settings (on xfce4-settings-manager) to customize keymaps to actions such as opening a program. You can create an action for something like "Launch web browser" or "launch MythTV Frontend."

 

related pages

 

xkb

The X KeyBoard extension, otherwise known as xkb, is does the mapping of physical keys to their proper function. The kernel manages console keyboard input, and in X it's managed by xkb, X Keyboard. xkb controls keymapping, keyboard models, and layouts.

xkb symbol files can be found in /usr/share/X11/xkb/symbols and the modifier keys are mapped in /usr/share/X11/xkb/symbols/pc

In current Ubuntu distributions, the default directory is not /etc/X11/xkb but rather /usr/share/X11/xkb for xkb.

Example: Disable CAPS LOCK and make it so you can toggle the caps lock state by pressing both shift keys at the same time. Type the following at console:

setxkbmap -option "caps:none"
setxkbmap -option "shift:both_capslock"

External resources:

keyword: XKeyboardConfig

xinput

To get a list of the various input devices, mice and keyboards

xinput -list