3

I have a fresh vanilla 18.04 LTS install, using gnome-shell 3.28.4. I'm using a wired Apple aluminum keyboard:

enter image description here I would like to use the top-row F-keys as keyboard shortcuts in various end-user applications (Firefox, CLion, etc), but whenever I press any of them, something in the gnome/X11 stack (I assume) is intercepting them and interpreting them as "media" keys. Such F-row keypresses are not delivered to any application, as far as I can tell.

For example, if I press F5, I'd like Firefox to reload the current webpage, or CLion to trigger a build, but instead, the only thing that happens is that the following giant icon is overlaid on the screen:

enter image description here

I've played around with gnome-tweaks and the settings app, to no avail. I've also looked around in dconf-editor, but I'm not familiar with it and haven't changed anything in there because I don't know what I'm doing.

I've done a lot of searching, but the only hits I've gotten seem to be from people whose media aren't working (and they want them to be working). To be clear, I want to disable media keys entirely.

Does anyone know how to do this? Thanks!

Update 1: output of cat /usr/share/X11/xkb/symbols/pc:

daniel@legolas:~$  cat /usr/share/X11/xkb/symbols/pc
default  partial alphanumeric_keys modifier_keys
xkb_symbols "pc105" {

    key <ESC>  {    [ Escape        ]   };

    // The extra key on many European keyboards:
    key <LSGT> {    [ less, greater, bar, brokenbar ] };

    // The following keys are common to all layouts.
    key <BKSL> {    [ backslash,    bar ]   };
    key <SPCE> {    [    space      ]   };

    include "srvr_ctrl(fkey2vt)"
    include "pc(editing)"
    include "keypad(x11)"

    key <BKSP> {    [ BackSpace, BackSpace  ]   };

    key  <TAB> {    [ Tab,  ISO_Left_Tab    ]   };
    key <RTRN> {    [ Return        ]   };

    key <CAPS> {    [ Caps_Lock     ]   };
    key <NMLK> {    [ Num_Lock      ]   };

    key <LFSH> {    [ Shift_L       ]   };
    key <LCTL> {    [ Control_L     ]   };
    key <LWIN> {    [ Super_L       ]   };

    key <RTSH> {    [ Shift_R       ]   };
    key <RCTL> {    [ Control_R     ]   };
    key <RWIN> {    [ Super_R       ]   };
    key <MENU> {    [ Menu          ]   };

    // Beginning of modifier mappings.
    modifier_map Shift  { Shift_L, Shift_R };
    modifier_map Lock   { Caps_Lock };
    modifier_map Control{ Control_L, Control_R };
    modifier_map Mod2   { Num_Lock };
    modifier_map Mod4   { Super_L, Super_R };

    // Fake keys for virtual<->real modifiers mapping:
    key <LVL3> {    [ ISO_Level3_Shift  ]   };
    key <MDSW> {    [ Mode_switch       ]   };
    modifier_map Mod5   { <LVL3>, <MDSW> };

    key <ALT>  {    [ NoSymbol, Alt_L   ]   };
    include "altwin(meta_alt)"

    key <META> {    [ NoSymbol, Meta_L  ]   };
    modifier_map Mod1   { <META> };

    key <SUPR> {    [ NoSymbol, Super_L ]   };
    modifier_map Mod4   { <SUPR> };

    key <HYPR> {    [ NoSymbol, Hyper_L ]   };
    modifier_map Mod4   { <HYPR> };
    // End of modifier mappings.

    key <OUTP> { [ XF86Display ] };
    key <KITG> { [ XF86KbdLightOnOff ] };
    key <KIDN> { [ XF86KbdBrightnessDown ] };
    key <KIUP> { [ XF86KbdBrightnessUp ] };
};

hidden partial alphanumeric_keys
xkb_symbols "editing" {
    key <PRSC> {
    type= "PC_ALT_LEVEL2",
    symbols[Group1]= [ Print, Sys_Req ]
    };
    key <SCLK> {    [  Scroll_Lock      ]   };
    key <PAUS> {
    type= "PC_CONTROL_LEVEL2",
    symbols[Group1]= [ Pause, Break ]
    };
    key  <INS> {    [  Insert       ]   };
    key <HOME> {    [  Home         ]   };
    key <PGUP> {    [  Prior        ]   };
    key <DELE> {    [  Delete       ]   };
    key  <END> {    [  End          ]   };
    key <PGDN> {    [  Next         ]   };

    key   <UP> {    [  Up           ]   };
    key <LEFT> {    [  Left         ]   };
    key <DOWN> {    [  Down         ]   };
    key <RGHT> {    [  Right        ]   };
};
3
  • 1
    Please edit your question and add the output of cat /usr/share/X11/xkb/symbols/pc
    – Raffa
    Jan 5, 2020 at 22:36
  • @Raffa: Edited to include that. Jan 5, 2020 at 23:08
  • Super. I am writing the answer now. :)
    – Raffa
    Jan 5, 2020 at 23:11

2 Answers 2

4

Are keyboard multimedia keys different?

Yes, these keys are a little different. They might report a single key code or multiple key codes when pressed with other keys like Fn for example. Furthermore, it is oftentimes difficult to identify their key codes with utilities like xev. This depends on the keyboard's manufacture's configuration.

Multimedia keys, usually, have their key codes configured with XF86 key names. These names differ but start with XF86 like XF86MonBrightnessUp and XF86MonBrightnessDown and so on.


How to disable/reconfigure keyboard multimedia keys?

XKB Method

The easiest method is to edit the XKB configuration file like so:

  1. Edit the XKB /usr/share/X11/xkb/symbols/pc file by running the following command in the terminal:

    sudo nano /usr/share/X11/xkb/symbols/pc

  2. Find lines that contain XF86 like so:

    key <KEY_CODE> { [ XF86MonBrightnessDown ] };

  3. Comment them out by adding // before them like so:

    //key <KEY_CODE> { [ XF86MonBrightnessDown ] };

  4. Save the file and exit the editor by pressing Ctrl + X then press Y.

  5. Clear the XKB cache by running the following command in the terminal:

    sudo rm -rf /var/lib/xkb/*

  6. Reboot your system to activate your new XKB configuration or alternatively you can avoid reboot and try to reload the new XKB configuration by setting an XKB map layout using the following command in the terminal:

    setxkbmap -layout us

  7. Test your keys.

If the keyboard multimedia keys are configured by the manufacturer to report a single key code, the above solution might disable them and leave the keys free to be used for other purposes.

If, however, this is not the case, then you will need to deal with xmodmap.

xmodmap Method

The xmodmap method is a little different and you cannot just disable multimedia keys using this method and assign them to nothing like so xmodmap -e 'keycode Number =' because this, oftentimes, will render them unusable so you will need to reassign them to the desired functions. You will also need to put in action some mechanism to maintain your changes between reboots and logouts/logins.

To implement this method, please follow the steps below:

  1. Show the current keyboard map by running the following command in the terminal:

    xmodmap -pke

  2. Inspect the output and identify lines that contain XF68 right after the = sign like so:

keycode 232 = XF86MonBrightnessDown NoSymbol XF86MonBrightnessDown NoSymbol XF86MonBrightnessDown

The above line, for example, shows that the key with code number 232 is currently configured to trigger XF86MonBrightnessDown which will decrease the monitor's brightness and you can change this behavior by assigning a different value to it like so xmodmap -e 'keycode 232 = New_Value'. For example to assign the F11 functionality to this multimedia key, please run the following command in the terminal:

xmodmap -e 'keycode 232 = F11'

The new functionality will be effective immediately. This change in functionality will, however, be lost after reboot or logout/login.

Notice: It might be helpful to run the acpi_listen command in the terminal and monitor the output as you press the actual physical key then compare it to the output you got from xmodmap -pke so that you confirm the key code is for the one you want. You can also try the xev utility but, unfortunately it will not always return a key code when dealing with multimedia or vendor specific keys.

To preserve the change after reboots and logouts/logins, you will need to do the following:

  1. Create and edit a script file in your home directory by running the following command in the terminal:

    nano ~/.Modify_Multimedia_Keys.sh

  2. Add this #!/bin/bash in the first line then add your xmodmap -e 'keycode Number = New_Value' commands below the first line ( each command in a single new line ) like so:

#!/bin/bash
xmodmap -e 'keycode 232 = F11'
xmodmap -e 'keycode 122 = F2'
  1. Save the script file and exit the editor by pressing Ctrl + X then press Y.

  2. Make the script file executable by running the following command in the terminal:

    chmod +x ~/.Modify_Multimedia_Keys.sh

  3. Make the script file execute at each start-up either by adding it to your Startup Applications through the GUI or by placing a Modify_Multimedia_Keys.desktop file in the ~/.config/autostart/ directory that contains the following content replacing YOUR_USERNAME with your actual username:

[Desktop Entry]
Type=Application
Exec=/home/YOUR_USERNAME/.Modify_Multimedia_Keys.sh
Hidden=false
X-GNOME-Autostart-enabled=true
Name=Modify Multimedia Keys
Comment=This modifies keyboard multimedia keys
4
  • Thanks for your detailed answer! I performed each step in turn, but unfortunately there is no change in behavior that I can see. I'd be happy to post the output of any other files if those are needed. Jan 6, 2020 at 11:52
  • @DanielA.Thompson I am sorry for that. It will be solved :) Please add the output of xmodmap -pke and I need you to stay around so I ask you how you want the keys modified as there is more than just disabling them.
    – Raffa
    Jan 6, 2020 at 15:36
  • @DanielA.Thompson Answer updated, Please try it and let me know if you need help. Best of luck
    – Raffa
    Jan 7, 2020 at 13:50
  • xmodmap method works. Thanks! Jan 7, 2020 at 15:30
1

For posterity, here is the shell script with the xmodmap commands needed to map keycodes to keys for this situation:

#!/bin/bash

# fix the common F keys
xmodmap -e 'keycode 128 = F3'
xmodmap -e 'keycode 212 = F4'
xmodmap -e 'keycode 237 = F5'
xmodmap -e 'keycode 238 = F6'
xmodmap -e 'keycode 173 = F7'
xmodmap -e 'keycode 172 = F8'
xmodmap -e 'keycode 171 = F9'
xmodmap -e 'keycode 121 = F10'
xmodmap -e 'keycode 122 = F11'
xmodmap -e 'keycode 123 = F12'

# fix the '=' on the numeric keypad
xmodmap -e 'keycode 125 = equal' 

I determined the keycodes using xev as described in this answer. Note that the Apple Aluminum keyboard has F keys all the way to F19 (!), which are not covered in the script above because I don't really use them. It ought to be possible to use xev to determine what the keycodes for those keys are and add them to the script (if needed).

2
  • Well done :). It is worth noting though that xev does not always return a key code. Not with all keyboard's keys and especially multimedia keys. However acpi_listen can make it easier to identify key codes from xmodmap -pke output.
    – Raffa
    Jan 7, 2020 at 17:50
  • 1
    OMIGOSH I am so glad I found this, I was having an issue where I couldn't hit any of my F-keys because my number row doubles as a F-key row with a fn modifier, but every time I hit them they'd engage one of the shortcut keys on my laptop. Struggled with it for hours! Finally came across this and it worked great. I'm creating a login script and calling this case closed... Aug 4, 2020 at 22:03

You must log in to answer this question.

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