68

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.

1
  • 5
    Have you tried using the Search function selecting the "votes" tab/button to filter results at AskUbuntu? The link at end of this comment may help you understand Argusvisions's answer and i will post one to in order to try and help. Here is one answer from search results when searching for "rempap key" using the advice just given - askubuntu.com/a/24930/102029
    – geezanansa
    May 16, 2013 at 20:38

3 Answers 3

81

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.

3
  • 2
    This worked fine for the current session, but it seems that Ubuntu 16.04 does not read .Xmodmap on reboot.
    – mivk
    Jan 5, 2017 at 13:00
  • Thanks. I got xmodmap mess up and appreciate if you could help askubuntu.com/questions/1026246/…
    – Tim
    Apr 19, 2018 at 1:50
  • My normal DownArrow key seems broken - with your step by step tutorial I remapped it to the NumPad DownArrow key and it works fine - thanks and +1 !
    – TheEagle
    Jul 13, 2021 at 20:00
35

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.

4
  • 2
    This post has a slightly "cleaner" xkb solution that doesn't rely on altering system-wide mappings: unix.stackexchange.com/a/65600/111905
    – sxc731
    Dec 12, 2016 at 18:58
  • 1
    This works in Ubuntu 16.04, unlike the creation of a .Xmodmap file which is ignored. And it works for all users. Still looking at how to make my changes also work in a Console without X.
    – mivk
    Jan 5, 2017 at 13:25
  • 1
    This works but as a note, I was not able to have two keys mapped to the same key code. For example I was not able to get rid of the caps-lock key and make it a ctrl key. I had to make the ctrl key a caps-lock and then I could make the caps-lock, ctrl.
    – Jerinaw
    Jan 11, 2017 at 15:20
  • 2
    One doesn't need to reboot after that. Simply use setxkbmap fr or setxkbmap gb intl or setxkbmap us intl or setxkbmap whatever_fits_your_keymap and you're good to go.
    – jmary
    Apr 28, 2020 at 9:45
8

Go to System Settings > keyboard > Custom Shortcuts. Here you can assign a command to any key or set of keys. Click the add (+) button. In the window that pops up, Name = Print Screen ; Command = gnome-screenshot. After you've created it, click on the line to the right of the name where it says "Disabled". That should change it to "New Accelorator" Just press F9.

Still looking for scroll lock.

After a little looking, I found an answer for scroll lock. I got started on this answer from what I found here. You should be able to apply it to any set of keys using xmodmap from the terminal. Edit: You can also read more about Xmodmap here

Take a look at the output of xmodmap -pke | grep F10.
Mine is:

keycode  76 = F10 F10 F10 F10 F10 F10 XF86Switch_VT_10  

This means my F10 key is keycode 76. Yours maybe different since you are using a different layout.

So I should be able to assign scroll lock to F10 with the following command:

xmodmap -e "keycode 76 = Scroll_Lock"  

You will need to insert the "keycode" you found for your F10 key.

You could also look through man xmodmap and xmodmap --help for additional options.
For example, xmodmap -pke lists your keymap table as expressions. That way you can locate the expressions assigned to other keys as well.

Not the answer you're looking for? Browse other questions tagged .