Skip to content

Instantly share code, notes, and snippets.

@p7cq
Last active October 13, 2022 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p7cq/a7a7fbb815b199f236ea6106330a33a1 to your computer and use it in GitHub Desktop.
Save p7cq/a7a7fbb815b199f236ea6106330a33a1 to your computer and use it in GitHub Desktop.
i3 WM: Remap Right Win key to Right Hyper

i3 WM: Remap Right Win key to Right Hyper

As sometimes I use a TKL keyboard without multimedia keys, I wanted Right Win and some other key combination to control audio volume.

My keyboard layout is us and I am using Left Win key assigned to mod4 as the modifier key. The spacebar row on my keyboard looks like this:

[Ctrl] [Left Win] [Alt] [Space] [Alt] [Right Win] [Menu] [Ctrl]

Using X

Identify the Right Win keycode

Run xev. When pressing Right Win on my keyboard the output is:

KeyRelease event, serial 34, synthetic NO, window 0x2000001,
    [...]
    state 0x40, keycode 134 (keysym 0xffec, Super_R), same_screen YES,
    [...]

Right Win has key code 134.

Checking modifier map:

xmodmap -pm
[...]
mod3
mod4        Super_L (0x85),  Super_R (0x86),  Super_L (0xce),  Hyper_L (0xcf)
[...]

Modifier mod3 is not set.

Configure i3

  • Create ~/.Xmodmap and add the following lines:
remove mod4 = Super_R
keycode 134 = Hyper_R
add    mod3 = Hyper_R
  • Modify ~/.config/i3/config:
exec "sleep 3; xmodmap ~/.Xmodmap"
set $mod3 Mod3

bindsym $mod3+equal exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ +10% && $refresh_i3status
bindsym $mod3+minus exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ -10% && $refresh_i3status
bindsym $mod3+End   exec --no-startup-id pactl set-sink-mute @DEFAULT_SINK@ toggle && $refresh_i3status

Without the sleep statement in config it appears that i3 runs xmodmap too early, which in turn looks like the command didn't run, hence key bindings do not work.

Checking the modifier map, it is also updated:

xmodmap -pm
[...]
mod3        Hyper_R (0x86)
mod4        Super_L (0x85),  Super_L (0xce),  Hyper_L (0xcf)
[...]

The key binding will work with a few seconds of delay after login. Having any setxkbmap commands in i3's configuration file may cause xmodmap to not have the desired effect.

Update: this method is unreliable; the key binding stops working after some time without any apparent reason. A workaround would be to use exec_always and to reload i3 a few times, but this becomes annoying quickly.

Using XKB

  • Get keyboard details:
setxkbmap -print
xkb_keymap {
        xkb_keycodes  { include "evdev+aliases(qwerty)" };
        xkb_types     { include "complete"      };
        xkb_compat    { include "complete"      };
        xkb_symbols   { include "pc+us+inet(evdev)"     };
        xkb_geometry  { include "pc(pc105)"     };
};
  • Edit pc file at /usr/share/X11/xkb/symbols/pc and replace
key <RWIN> {      [ Super_R               ]       };

with

key <RWIN> {        [ Hyper_R               ]       };

and

key <HYPR> {      [ NoSymbol, Hyper_L     ]       };
modifier_map Mod4   { <HYPR> };

with

key <HYPR> {        [ NoSymbol, Hyper_R     ]       };
modifier_map Mod3   { <HYPR> };
  • Modify ~/.config/i3/config:
set $mod3 Mod3

bindsym $mod3+equal exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ +10% && $refresh_i3status
bindsym $mod3+minus exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ -10% && $refresh_i3status
bindsym $mod3+End   exec --no-startup-id pactl set-sink-mute @DEFAULT_SINK@ toggle && $refresh_i3status

Logout and login for the modifier change to take effect.

Drawbacks: a system update might overwrite the file; if there are multiple users using the system this approach is not the way to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment