在 Linux 上重新映射 Win、Ctrl 和 Alt 键

Remap Win, Ctrl and Alt keys on Linux

问题:

如何在 Linux 系统范围内重新映射(交换)修改键?

解释:

我想在我的 PC 上安装 Debian Jessie 以便与 Emacs 一起使用。我有一台装有 Emacs 的 MacBook,主要使用它。以下是笔记本电脑上按键的排列方式:

fn ctrl alt cmd 空格键 cmd alt

fn 在我的 Emacs 工作流程中未使用,其他键配置如下:

这些配置简化了我的 Emacs 体验(大拇指点击 Control cmd 键)并且我习惯了这种键绑定。

默认alt用作Meta,而ctrl用作[=14] =],键被交换,大拇指放在 Meta

我想以同样的方式在 Linux 上配置我的键,这是我电脑键盘上的内容:

ctrl win alt 空格键 alt win menu ctrl

所以,为了对应我的笔记本电脑键,我希望 win 键映射到 Alt 修饰符,并且 alt 键映射到 Control 修饰符。在这种情况下 ctrl 可以用作 Super 修饰符,而 menu 可以保持不变。

是否可以在 Linux (Debian) 上进行系统范围内的此类配置?


最终,我通过以下配置成功实现了我的目标:

!file ~/.Xmodmap

clear control
clear mod1
clear mod4


keycode 37  = Super_L     
! left  Ctrl becomes Super
keycode 64  = Control_L
! left  Alt  becomes Control 
keycode 133 = Alt_L Meta_L
! left  Win  becomes Alt
keycode 108 = Control_R
! right Alt  becomes Control 
keycode 134 = Alt_R Meta_R
! right Win  becomes Alt
keycode 105 = Super_R
! right Ctrl becomes Super


add control = Control_L Control_R
add mod1 = Alt_L Meta_L
add mod4 = Super_L Super_R

此外,similar question 提供了非常有用且简单的指南。

xmodmap 是开始的地方,请记住 X 对 HyperMeta 一无所知,因此您必须调查以查看哪个编号修饰符(mod4、mod5等),emacs 使用这些修饰符。

这里有一些有用的链接:

除了使用 xmodmap 之外,可能更容易检查 setxkbmap 给出的键盘布局选项。这些选项实现了常见的 xmodmap 更改,因此它们很可能包含您想要的内容。

例如,我喜欢 -option ctrl:nocaps,它在 caps lock 所在的位置添加了另一个 control。考虑文件 /usr/share/X11/xkb/rules/base.lst 以获得标准选项列表。

但是,xmodmapsetxkbmap 都不包括纯文本 tty 终端(非 X)的键盘设置。

完全公开

我是 Kinto 的作者。

您可能会发现 Kinto 很有用。这是我最近创建的一个项目,经过几年以不同的方式解决这个问题,我现在觉得它足够可靠,可以分享。

Kinto 安装程序仅使用 python 进行初始安装,之后它只是一个系统服务、bash、xprop、setxkbmap 和 xkbcomp。就我而言,这是您将要找到的解决此问题的最快、最原生的解决方案。

Xmodmap 也已失效,它将无法与 Wayland 一起使用。

https://github.com/rbreaves/kinto

https://medium.com/@benreaves/kinto-a-mac-inspired-keyboard-mapping-for-linux-58f731817c0

这里还有一个要点,如果你只是想看看它的核心是什么,它不会在需要时替换你的键盘映射。 Gist 也不包括设置 macOS 风格的自定义 xkb 键盘映射文件 cursors/word-wise 使用 Cmd 和方向键的操作。

https://gist.github.com/rbreaves/f4cf8a991eaeea893999964f5e83eebb

要点内容

# permanent apple keyboard keyswap
echo "options hid_apple swap_opt_cmd=1" | sudo tee -a /etc/modprobe.d/hid_apple.conf
update-initramfs -u -k all

# Temporary & instant apple keyboard keyswap
echo '1' | sudo tee -a /sys/module/hid_apple/parameters/swap_opt_cmd

# Windows and Mac keyboards - GUI (Physical Alt is Ctrl, Physical Super is Alt, Physical Ctrl is Super)
setxkbmap -option;setxkbmap -option altwin:ctrl_alt_win

# Windows and Mac keyboards - Terminal Apps (Physical Alt is Super, Physical Super is Alt, Physical Ctrl is Ctrl)
setxkbmap -option;setxkbmap -option altwin:swap_alt_win

#
# If you want a systemd service and bash script to help toggle between
# GUI and Terminal applications then look at project Kinto.
# https://github.com/rbreaves/kinto
#
# Note: The above may not work for Chromebooks running Linux, please look
# at project Kinto for that.
#
# If anyone would like to contribute to the project then please do!
#