1

I have tried many things to try to swap the alt and windows key on my Thinkpad X220 running LinuxMint 20.3 with xfce/xfwm4.

I've tried setxkbmap -option altwin:swap_alt_win, adding the following to ~/.xmodmap:

remove mod1 = Alt_L
remove mod4 = Super_L
add mod1 = Super_L
add mod4 = Alt_L

and running xmodmap ~/.xmodmap

I've looked through the Keyboard > Layout > Change Layout Option to see what's available.

FWIW, showkey reports:

ALT keycode  56
SUPER keycode 125

and I've also tried manually setting this in ~/.xmodmap:

keycode 56 = Alt_L
keycode 125 = Super_L
remove Mod1 = Alt_L
remove Mod4 = Super_L
add Mod1 = Super_L
add Mod4 = Alt_L

So far nothing has worked for me, any hints in the right direction would be greatly appreciated!

3
  • X.Org or Wayland? What DM (or no DM)? Jan 31, 2022 at 8:38
  • I am using Xfce - and WM is Xfwm4
    – waffl
    Feb 1, 2022 at 19:30
  • Xfce is a Desktop Environment (DE), I asked about a Display Manager (DM) and whether you are using X.Org or Wayland. Feb 1, 2022 at 20:53

1 Answer 1

2

I have never had any luck getting setxkbmap to do what I want but have remapped keys at the kernel level with success.

First you need to know your input device and the key scan code. The tool evtest can be used for that.

$ sudo evtest
No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0:  Power Button
/dev/input/event1:  Power Button
/dev/input/event2:  USB Laser Mouse
...
/dev/input/event7:  SONiX USB DEVICE
/dev/input/event8:  SONiX USB DEVICE Keyboard
/dev/input/event9:  SONiX USB DEVICE System Control
/dev/input/event10: SONiX USB DEVICE Consumer Control
/dev/input/event11: SONiX USB DEVICE
...
Select the device event number [0-31]:

Shown here are 5 devices that relating to keyboard and device number 7 happens to be the actual keyboard. Trial and error to find this out.

Note the device id after selecting the keyboard.

Select the device event number [0-31]: 7
Input driver version is 1.0.1
Input device ID: bus 0x3 vendor 0xc45 product 0x5004 version 0x111
Input device name: "SONiX USB DEVICE"
Supported events:
...

The line Input device ID is what you need and this will be display just after you select your device.

When selected you can then hit some keys on the keyboard and an event will be logged for the key press and release. If you don't see events being logged, you need to try selecting another device. His ctrl-c when finished.

You will want to note the key code for alt and super after pressing them. In my case the output for those keys presses looks like this:

Event: time 1643575346.666078, type 4 (EV_MSC), code 4 (MSC_SCAN), value 700e3
Event: time 1643575346.666078, type 1 (EV_KEY), code 125 (KEY_LEFTMETA), value 1
Event: time 1643575346.666078, -------------- SYN_REPORT ------------
Event: time 1643575346.766070, type 4 (EV_MSC), code 4 (MSC_SCAN), value 700e3
Event: time 1643575346.766070, type 1 (EV_KEY), code 125 (KEY_LEFTMETA), value 0
Event: time 1643575346.766070, -------------- SYN_REPORT ------------
...
Event: time 1643575369.928048, type 4 (EV_MSC), code 4 (MSC_SCAN), value 700e2
Event: time 1643575369.928048, type 1 (EV_KEY), code 56 (KEY_LEFTALT), value 1
Event: time 1643575369.928048, -------------- SYN_REPORT ------------
Event: time 1643575370.046045, type 4 (EV_MSC), code 4 (MSC_SCAN), value 700e2
Event: time 1643575370.046045, type 1 (EV_KEY), code 56 (KEY_LEFTALT), value 0
Event: time 1643575370.046045, -------------- SYN_REPORT ------------

Shown above, code 125 is for the super key, and 56 the left alt key. Your values could be different.

Now you need to create a file to remap the desired keys. Create a file in /etc/udev/hwdb.d/ called 50-alt_super_swap.hwdb. Exactly what is is called is up to you, but you need a number at the beginning, the dash, and the extension.

In that file you will place the following:

# Input device ID: bus 0x3 vendor 0x13ba product 0x1 version 0x110
# evdev:input:b<bus_id>v<vendor_id>p<product_id>e<version_id>-<modalias>
evdev:input:b0003v13BAp0001*
  KEYBOARD_KEY_70038=125
  KEYBOARD_KEY_7007D=56 

You need to modify the evdev:input line to reflect the device id printed out be evtest. Note the translation and the upper/lower case. If you don't get this line exactly correct, nothing will happen.

The KEYBOARD_KEY_* lines have their values in hexadecimal. 38 hex is 56 decimal, and 7D is 125.

$ sudo systemd-hwdb update
$ sudo udevadm trigger

It takes several seconds for the changes to be applied. Along with finding the key swap worked you can also see if the changes were applied.

$ udevadm info /dev/input/event7
P: /devices/pci0000:00/0000:00:07.1/0000:0d:00.3/usb3/3-2/3-2:1.0/0003:0C45:5004.0005/input/input7/event7
N: input/event7
L: 0
S: input/by-path/pci-0000:0d:00.3-usb-0:2:1.0-event-kbd
S: input/by-id/usb-SONiX_USB_DEVICE-event-kbd
...
E: KEYBOARD_KEY_70038=125
E: KEYBOARD_KEY_70075=56
...

While I have not swapped alt and super myself, I have used this technique to remap media keys using this article as a reference.

4
  • Thank you for this detailed response. It is very clear and makes sense. I did follow quite closely and managed to get the events to show in udevadm but it simply wouldn't work. In the end, I edited Keyboard > Application Shortcuts and changed the Shortcut for xfce4-popup-whiskermenu to Alt L and then in Window Manager > Keyboard, adjusted Cycle Windows to Super + Tab which sort of achieves what I was looking for.
    – waffl
    Feb 1, 2022 at 20:31
  • Start always with: KEYBOARD_KEY_700 What is 700 ?
    – user500425
    Jun 10, 2022 at 4:56
  • 70038 and 7007D are meant to be the key scancodes. These should be 700e3 and 700e2 for the given example, as per the last of the linked articles. Once I realised that, this is now working fine for me. I like that this solution is so low level that it should work regardless of window manager/desktop environment, and can be applied on a per-device basis.
    – boltronics
    Jan 17, 2023 at 1:18
  • Values like 700e3 aren't linux but hardware specific: they are 32-bit USB HID usage codes. The highest 16 bits store a usage page ID while the lowest 16 bits select a usage ID from that page. 700e3 refers to usage page 7 (keyboard) and usage ID e3 ("left GUI" in USB lingo). These standardised usage IDs can be found in the usb.org/sites/default/files/hut1_5.pdf document. The usage page IDs are on page 17 of the pdf. Keyboards usually rely on usage page IDs 7 and C. C is the consumer control page (multimedia keys). The usage IDs of the keyboard usage page start on page 89 of the pdf. Apr 4 at 16:23

You must log in to answer this question.

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