I'm searching for a way to remap certain keys in ubuntu.
i.e.
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?
I'm searching for a way to remap certain keys in ubuntu.
i.e.
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?
Notice: As of 2013, Ubuntu and derivatives no longer use
xmodmap
, but instead usexkb
. For more information see this answer. The answer below is no longer relevant for current releases.For remapping certain keys you need two tools. First xev (command-line tool) and second xmodmap (also command-line tool). Both should be available in Ubuntu without extra installing them.
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: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 outputHere again the interesting part for us is
keycode 115
andNext
- the name of the behaviour.now when you want to swap the two keys use
xmodmap
.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:
Additional information: The sequence of these mappings depends on the keyboard layout. It usually is Key, Shift+Key, mode_switch+Key, mode_switch+Shift+Key, AltGr+Key, AltGr+Shift+Key, but can be very different for more special layouts, like in case of the German Neo 2 one. To skip a column use
NoSymbol
. Here is a comprehensive list of all keysyms.You can see the concrete sequence for your layout by finding it in one of the files in
/usr/share/X11/xkb/symbols/
.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:
(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 commandxmodmap .Xmodmap
in.You can now modify
.Xmodmap
and runxmodmap .Xmodmap
from console to see the changes immediately. The changes in.Xmodmap
will persist.source: Ubuntu Foruns
Bonus stuff:
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.
mod2
, becausexmodmap -pm
tells us thatmod2
isNum_Lock
, the other names are obtained by pressing the keys inxev
.If you're trying to move a Shift key, there are a few extra steps:
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
remaprebind a key.(a key can be used for different things in different apps/windows)
(a key has the same function in all windows)
For 'local to a program' methods, there is sometimes a way to change keybindings offered by the app itself... eg.
Firefox
has an addon called keyconfig ... for some info see this MozillZine postMost 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 runninggconf-editor
(via Terminal or Alt+F2)... navigate todesktop
--gnome
--interface
and selectcan_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
Keyboard Shortcuts
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):
REPORTS:
REPORTS:
REPORTS:
(ignore the next fifty lines or so)
What's important is the third line of each keypress:
REVERSE THE MAPPING:
SAVE THE RESULTS:
ADD
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!
In order to change the map to apply to Caps Lock (keycode 58 on my keyboard) instead
Now it reads
To add this remap to the default map, it simply needs to be appended to the remap include file for console-setup
and console-setup needs to be reconfigured (skipping low priority questions with
-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
Ctrl+Pageup
.I first tried
xmodmap
but that can't generate modifier events. So it is impossible to create a shortcut that generates for example theControl
event.I then tried
xbindkeys
withxmacro
. That sort of works, butxbindkeys
is not able to capture some key combinations on my system, such asAlt + ___
.So I finally used Unity's own Keyboard Shortcuts -> Custom Shortcuts to setup my shortcut.
And instead of
xmacro
, I have now usedxvkbd
program to generate the keyboard events, but that is just a personal preference. Bothxmacro
andxvkbd
work almost the same. One additional tip is to add a delay parameter inxmacro
orxvkbd
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/input-remapper
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.
To do this with hax11, open the program's profile configuration file under
~/.config/hax11/profiles
(e.g.~/.config/hax11/profiles/usr\lib\firefox\firefox
), and add:Non-Persistant remap via .desktop shortcut
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
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"
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:Extra info: state field output of
xev
,The state field is the "sum" of current active modifiers. These are the ones I deciphered.