I'm searching for a way to remap certain keys in ubuntu.
I'd like to change PgUp to Home or PgDown to End.
Does a built-in command or a tool exist reassign keys in Ubuntu/GNOME?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
Notice: As of 2013, Ubuntu and derivatives no longer use
xmodmap, but instead use
xkb. For more information see this answer. The answer below is no longer relevant for current releases.
Start terminal window and run
xev. Now it's active and waits for you to press a key. Then press the key whose behaviour you want to change. i.e. PgUp.
xev will output some information about the pressed key. The third line is important. It should look similar to:
state 0x10, keycode 110 (keysym 0xff55, Prior), same_screen YES,
in this example
Prior is the name of the behaviour the key is assigned to at the moment, the number keycode is the internal id to recognize the key.
Now do this with another key i.e. PgDown give this output
state 0x10, keycode 115 (keysym 0xff56, Next), same_screen YES,
Here again the interesting part for us is
keycode 115 and
Next - the name of the behaviour.
now when you want to swap the two keys use
xmodmap -e "keycode 110 = Next"
This changes the key with keycode 110 on your keyboard to the action
Next. It's pretty simple.
Note that if the key you are mapping should have a different meaning when used with the Shift key (for example for British keyboard layouts, Shift+2 gives quotation marks) then you can simply list the secondary command after the first. For example if you want the key with code 53 to map to backslash normally, but to the bar symbol when used with shift, you might do:
xmodmap -e "keycode 53 = backslash bar"
Additional information: The sequence of these mappings is Key, Shift+Key, mode_switch+Key, mode_switch+Shift+Key, AltGr+Key, AltGr+Shift+Key. To skip a column use
NoSymbol. Moreover, here is a comprehensive list of all keysyms.
Note: These change are for the active X session only and will be lost after reboot. When you want to save the changes permanently you have to run the following commands after the ones above:
xmodmap -pke >~/.Xmodmap
(it creates a file named
.Xmodmap in your home directory (
Then you have to create a file named
.xinitrc in your home directory where you put command
xmodmap .Xmodmap in.
You can now modify
.Xmodmap and run
xmodmap .Xmodmap from console to see the changes immediately. The changes in
.Xmodmap will persist.
source: Ubuntu Foruns
If the key you are remapping has different behavior depending on a state (
like how the keys in the numeric keyboard depend on NumLock) you simply have to do
xmodmap -pm to get a list of modifiers and then do:
xmodmap -e "KEYCODE MODIFIER = behaviour behaviour_with_modifier"
Suppose, for example, that you want to get a period instead of a comma on the numeric keyboard (useful for most programmers), but you want to keep the "delete" behavior when NumLock is off.
xmodmap -e "keycode 91 mod2 = KP_Delete period"
xmodmap -pm tells us that
Num_Lock, the other names are obtained by pressing the keys in
If you're trying to move a Shift key, there are a few extra steps:
xmodmap -e "keycode 62 = Up" # Shift => Up xmodmap -e "keycode 111 = Shift_R" # Up => Shift xmodmap -e "add shift = Shift_R" # Make the new Shift key actually do shifting xmodmap -e "remove shift = Up" # Prevent the old Shift key from shifting xset r 62 # Make the new Up key autorepeat xset -r 111 # Prevent the new Shift key from autorepeating
I've just had an afterthought.. I think you may mean something entirely different by "remap".. but I'll leave my answer as it is... (I don't know how to re-assign one key to behave as another)
UPDATE: my 'afterthought' has been confirmed; ( I've answered the wrong question :)... please see NES's Community Wiki answer (accepted above).
There are two general ways to
remap rebind a key.
For 'local to a program' methods, there is sometimes a way to change keybindings offered by the app itself... eg.
Most Ubuntu programs are Gnome based and there is a specific utility to modify the keybinding for any menu item of these Gnome apps... It is called
Editable Menu Accelerator ... It is a very 'touchy' tool, but quite powerful.. You can enable it by running
gconf-editor (via Terminal or Alt+F2)... navigate to
interface and select
can_change_accels .... You can then change menu items to virtual anything you like (per program/window)... I suggest you disable it as soon as you've done what you need..
Otherwise you can set up Global hotkeys. I use a program called xbindkeys , and there is also an option available via the Main Menu -- Preferences, called
If you use xbindkeys, you will need to add it to your "Startup Applications" (Main Menu -- Preferences) ... Also (as suggested by Stefano Palazzo) I have previously written a more detailed description of xbindkeys in an answer on this askubuntu page
Here is how I tried to switch the mapping of the ENTER key to the SHIFT key (and vice versa):
$ uname -a
Linux box 2.6.32-37-generic #81-Ubuntu SMP Fri Dec 2 20:35:14 UTC 2011 i686 GNU/Linux $ which xmodmap
/usr/bin/xmodmap $ which xev
/usr/bin/xev $ xev
(ignore the next fifty lines or so)
PRESS THE ENTER KEY (notice the third line): KeyPress event, serial 33, synthetic NO, window 0x5600001, root 0x110, subw 0x0, time 263441120, (738,242), root:(771,314), state 0x0, keycode 36 (keysym 0xff0d, Return), same_screen YES, XLookupString gives 1 bytes: (0d) XmbLookupString gives 1 bytes: (0d) XFilterEvent returns: False KeyRelease event, serial 33, synthetic NO, window 0x5600001, root 0x110, subw 0x0, time 263441271, (738,242), root:(771,314), state 0x0, keycode 36 (keysym 0xff0d, Return), same_screen YES, XLookupString gives 1 bytes: (0d) XFilterEvent returns: False PRESS THE SHIFT KEY (notice the third line): KeyPress event, serial 30, synthetic NO, window 0x5600001, root 0x110, subw 0x0, time 263592202, (464,368), root:(497,440), state 0x0, keycode 62 (keysym 0xffe2, Shift_R), same_screen YES, XLookupString gives 0 bytes: XmbLookupString gives 0 bytes: XFilterEvent returns: False KeyRelease event, serial 33, synthetic NO, window 0x5600001, root 0x110, subw 0x0, time 263592298, (464,368), root:(497,440), state 0x1, keycode 62 (keysym 0xffe2, Shift_R), same_screen YES, XLookupString gives 0 bytes: XFilterEvent returns: False
What's important is the third line of each keypress:
FOR: state 0x0, keycode 36 (keysym 0xff0d, Return), same_screen YES, The name "Return" is the name of the behavior of the key pressed. The number of the key pressed is "36". state 0x0, keycode 62 (keysym 0xffe2, Shift_R), same_screen YES, The name "Shift_R" is the name of the behavior of the key pressed. The number of the key pressed is "62".
REVERSE THE MAPPING:
$ xmodmap -e "keycode 62 = Return" $ xmodmap -e "keycode 36 = Shift_R"
SAVE THE RESULTS:
$ xmodmap -pke > ~/.Xmodmap $ vi ~/.xinitrc
xmodmap ~/.Xmodmap $ sudo reboot
The main problem was that the reversal did NOT work. The ENTER key was mapped to the SHIFT_R key; but the SHIFT_R key was not mapped to the ENTER key. Go figure.
In order to do global remaps independently of X, you can make use of console-setup(5) instead.
In my case I wanted to remap Caps Lock key to D, since my D-key is broken :)
First I used dumpkeys(1) to get a template for a mapping, in the case of the D-key, the interesting bit is the mapping for keycode 32 (on my keyboard); Note that there are two spaces in the grep pattern!
$ sudo dumpkeys | grep "keycode 32" > tempfile $ cat tempfile keycode 32 = +d shift keycode 32 = +D altgr keycode 32 = +eth shift altgr keycode 32 = +ETH control keycode 32 = Control_d shift control keycode 32 = Control_d altgr control keycode 32 = Control_d shift altgr control keycode 32 = Control_d (121 lines total...)
In order to change the map to apply to Caps Lock (keycode 58 on my keyboard) instead
sed 's/32/58/' -i tempfile
Now it reads
keycode 58 = +d shift keycode 58 = +D altgr keycode 58 = +eth (etc...)
To add this remap to the default map, it simply needs to be appended to the remap include file for console-setup
sudo sh -c 'cat tempfile >> /etc/console-setup/remap.inc'
and console-setup needs to be reconfigured (skipping low priority questions with
sudo dpkg-reconfigure console-setup -phigh
Now the remap should be completed, and be loaded automatically on boot.
I spent one whole day in trying to create a shortcut for
I first tried
xmodmap but that can't generate modifier events. So it is impossible to create a shortcut that generates for example the
I then tried
xmacro. That sort of works, but
xbindkeys is not able to capture some key combinations on my system, such as
Alt + ___.
So I finally used Unity's own Keyboard Shortcuts -> Custom Shortcuts to setup my shortcut.
And instead of
xmacro, I have now used
xvkbd program to generate the keyboard events, but that is just a personal preference. Both
xvkbd work almost the same. One additional tip is to add a delay parameter in
xvkbd to ensure that the events don't get lost.
For me AutoKey from Software Center worked the best. It has intuitive GUI, to add new binding click New -> Phrase and
<left>(list of special keys is here).
You could also try https://github.com/sezanzeb/key-mapper
It runs in the background and actively injects mapped keycodes.
While this adds some extra flexibility, if all you need is remap a single button and be done you could try to use xmodmap. Looks like that has already be explained in detail.
Or write an xkb "symbols" file and use setxkbmap, but I wouldn't recommend that. setxkbmap can be used on a per-device basis as well.
Writing xkb or xmodmap configurations caused trouble for me though if both devices report the same keycode, even though the configuration maps them to different characters, when pressing them simultaneously (which is the main reason why I had to write the injection tool for that)
And recently I came across this solution: https://www.reddit.com/r/linux_gaming/comments/k3h9qv/remapping_keys_using_hwdb_files/
If you need to remap a key for a specific program only, I've just added this feature to hax11.
I'd like to change PgUp to Home or PgDown to End.
To do this with hax11, open the program's profile configuration file under
~/.config/hax11/profiles/usr\lib\firefox\firefox), and add:
Enable=1 MapK112=K110 MapK117=K115
In my case I wanted to remap the keypad decimal point to a comma, but only temporarily, so
xmodmap satisfies this requirement in my case
:~$ xev | grep keycode
state 0x10, keycode 91 (keysym 0xffae, KP_Decimal), same_screen YES, XKeysymToKeycode returns keycode: 129 state 0x10, keycode 59 (keysym 0x2c, comma), same_screen YES,
1st I pressed the key I want to remap and 2nd I pressed the key I want to map to.
..., keycode 91 (...
...(keysym 0x2c, comma),...
Find out what the current mapping for the keycode to remap is:
:~$ xmodmap -pke | grep "keycode\s*91"
keycode 91 = KP_Delete KP_Decimal KP_Delete KP_Decimal
As specified in the manpage of xmodmap: Up to eight keysyms may be attached to a key.. : 1st keysym is used when no modifier key, 2nd with Shift, etc... This does not correspond exactly to what was happening in my case but trial and error allowed me to discover the the 2nd positional argument was the one I was after
Map to the new config.
:~$ xmodmap -e "keycode 91 = KP_Delete comma KP_Delete KP_Decimal"
Map back to original config.
:~$ xmodmap -e "keycode 91 = KP_Delete KP_Decimal KP_Delete KP_Decimal"
I created a couple
.desktop shortcuts in
~\.local\share\applications\ to easily switch to one or the other configuration:
[Desktop Entry] Name=KeyPad comma Exec=xmodmap -e "keycode 91 = KP_Delete comma KP_Delete KP_Decimal" Terminal=false Type=Application Icon=/home/paui/.icons/KP_comma.png
state field output of
The state field is the "sum" of current active modifiers. These are the ones I deciphered.
0x01 Shift 0x02 Caps 0x04 Control 0x08 Alt 0x10 NumLock 0x80 Alt Gr
If your lucky, perhaps the specific remap you want to perform is already integrated into a set of predefined options you can add to a your current configuration with setxkbmap.
:~$ man xkeyboard-config
\for your specific configuration option. in my case I wanted to remap the keypad Delete key to a comma instead of a period, and I was in luck
┌─────────────────────────────────────────────────────────────────┐ │Option Description │ ├─────────────────────────────────────────────────────────────────┤ │kpdl:dot Legacy key with dot │ │kpdl:comma Legacy key with comma │ │kpdl:dotoss Four-level key with dot │ │kpdl:dotoss_latin9 Four-level key with dot, Latin-9 only │ │kpdl:commaoss Four-level key with comma │ │kpdl:momayyezoss Four-level key with momayyez │ │kpdl:kposs Four-level key with abstract separators │ │kpdl:semi Semicolon on third level │ │ │ └─────────────────────────────────────────────────────────────────┘
:~$ setxkbmap -query
rules: evdev model: pc105 layout: es,gb variant: winkeys,
:~$ setxkbmap -option kpdl:commaoss
:~$ setxkbmap -query rules: evdev model: pc105 layout: es,gb variant: winkeys, options: kpdl:commaoss
:~$ setxkbmap -option
If you require a mapping that is not predefined there, such as the example you placed, you will need to have to write it yourself as explained in this answer