How can I remap keyboard keys? [duplicate]

I am looking for general instructions on remapping keys.

Can someone tell me how to remap F9 to PrtScr and F10 to ScrollLock button?

I have looked through every thread here and none of the suggestions worked.

I am using a Swedish keyboard setup and Ubuntu 12.04.


Solution 1:

This answer has been mostly made up using the information from the first three steps of this Ask Ubuntu answer and this Ubuntu Forums thread to which it links/refers to. This other Ask Ubuntu answer was very helpful to. So all credit to NES, llazarte, bodhi.zazen and Argusvision.

To simply swap two keys functions: From Terminal run xev and then press F9 which would give something like

Terminal window showing F9 KeyPress event

Doing the same for PrtSc did not give any output in xev for me (or I could not find the "KeyPress event") so I used Argusvision's advice for using the Custom Shortcuts in All Settings but doing so in order to disable PrtSc as screenshot button by reassigning as Shift + Alt then tried pressing again which gave me keycode 107 for PrtSc in xev like so

Terminal window showing PrtSc KeyPress event

Repeating the process for all four keys gave me

  • F9 = keycode 75
  • PrtSc = keycode 107, action Print
  • F10 = keycode 76
  • Scroll Lock = keycode 127, action Pause

To change a keys function we need to know the keycode of the NEW key being pressed and the "action" of the OLD or existing key for that function.

Now that we have the key codes for identifying the keys we now make the system do what we want by using xmodmap and as we know F9 has "keycode 75" and PrtSc has the action of "Print" all we need to do is

xmodmap -e "keycode 75 = Print"

to make the F9 key behave as if the PrtSc key has been pressed. i.e. the keycode stays the same but pressing the F9 key will have different results.

Using xev with Scroll Lock also gave

Terminal window showing Scroll Lock KeyPress event

which confirms the "action" for the Scroll Lock key is "Pause" so for making the F10 act as Scroll Lock trying

xmodmap -e "keycode 76 = Pause"

should give the desired results.

That is until you log out or shutdown etc so what we have to do after confirming the commands

xmodmap -e "keycode 75 = Print"
xmodmap -e "keycode 76 = Pause"

do give the desired results is sort out SysRec which is modified PrtSc and swap over the other buttons so we do not have multiple instances of same key action . Adding

xmodmap -e "keycode 107 mod1 = F9 Sys_Req"

should swap F9 to where PrtSc was keeping the modified SysReq (Alt + PrtSc) again we can use xev to help verify this.

Swapping the Scroll Lock and F10 buttons is easier

xmodmap -e "keycode 127 = F10"

After making sure that when running

xmodmap -e "keycode 75 = Print"
xmodmap -e "keycode 107 mod1 = F9 Sys_Req"
xmodmap -e "keycode 76 = Pause"
xmodmap -e "keycode 127 = F10"

does indeed do what is intended all that is left to do is get this happening at startup time. To do this run

xmodmap -pke|egrep  -e '(F9|Print)'

which gave me

some more text

we are only interested in the keycodes 75 and 107. Do the same for

xmodmap -pke|egrep  -e '(F10|Pause)'

which gave me

even more text

and we are only interested in the keycodes 76 and 127.

Create a new text document with your favourite text editor copy and paste the relevant information

keycode  75 = Print NoSymbol Print
keycode 107 = F9 Sys_Req F9 Sys_Req
keycode  76 = Pause NoSymbol Pause
keycode 127 = F10 NoSymbol F10

naming the file as .Xmodmap and saving it in your Home directory would allow you to run the changes simply by logging on after rebooting.

Solution 2:

For newer versions of Ubuntu, Xmodmap works but after you reboot, you lose your keymappings! Instead what I did was to edit the xbd file (/usr/share/X11/xkb/symbols/pc).

In this case you would add the following entries:

key <FK09> { [Print] };
key <FK10> { [Scroll_Lock] };

Then clear the xkb settings cache:

rm -rf /var/lib/xkb/*

After a reboot your keys are mapped corectly.