-
Notifications
You must be signed in to change notification settings - Fork 1
/
.apple_keyboard_mod.rc
executable file
·163 lines (140 loc) · 5.68 KB
/
.apple_keyboard_mod.rc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
[[ $(lsb_release -rs | tr -d '.') -ge 2204 ]] && {
# inspired by <https://linuxconfig.org/reprogram-keyboard-keys-with-xmodmap>
xmodmap -pke |
sed -re 's;^(keycode 105 = )Alt_R Meta_R Alt_R Meta_R;\1Control_R NoSymbol Control_R;g' \
-e 's;^(keycode 108 = )Control_R NoSymbol Control_R;\1Alt_R Meta_R Alt_R Meta_R;g' |
xmodmap -
echo "log out and log back in to effect keyboard key swaps"
exit
}
# default keycode values
#ctrl_l=37
ctrl_r=105
#alt_l=64
alt_r=108
#super_l=133
#cmd_l=${super_l}
cmd_r=134
reset_keyboard() {
setxkbmap -layout us
}
[[ ${1} =~ (-f|--force) ]] && FORCE=1
# alternate behavior depending on if an Apple keyboard is plugged in
if grep -qi 'apple keyboard' /proc/bus/input/devices; then
# make Cmd-R behave like Ctrl-R
# and make Alt-R actually behave like Alt-R
addtl_cmds="
keycode ${cmd_r} = Control_R NoSymbol Control_R
keycode ${alt_r} = Alt_R Meta_R"
if [[ ! $(grep -s 2 /sys/module/hid_apple/parameters/fnmode) ]]; then
echo "try enforcing function key mode on the Apple keyboard permanently? [y/N] "; read
if [[ ${REPLY} =~ [Yy] ]]; then
if [[ $(grep -s 'fnmode = 2' /etc/sysfs.conf) ]]; then
# from <https://help.ubuntu.com/community/AppleKeyboard#Change_Function_Key_behavior>
echo 'options hid_apple fnmode=2' | sudo tee -a /etc/modprobe.d/hid_apple.conf &>/dev/null
sudo update-initramfs -u -k all
else
echo 'module/hid_apple/parameters/fnmode = 2' | sudo tee -a /etc/sysfs.conf &>/dev/null
fi
fi
# either way, enable it without requiring a reboot
echo "enforcing function key mode temporarily"
echo 2 | sudo tee /sys/module/hid_apple/parameters/fnmode &>/dev/null
fi
else
# swap Ctrl-R and Alt-R
addtl_cmds="
keycode ${alt_r} = Control_R NoSymbol Control_R
keycode ${ctrl_r} = Alt_R Meta_R"
{ grep -E -q "replace key <RALT>.*Control_R, Control_R" /usr/share/X11/xkb/symbols/ctrl &&
grep -E -q "replace key <RCTL>.*Alt_R, Meta_R" /usr/share/X11/xkb/symbols/ctrl; } || {
# restart sudo timeout timer
sudo -v
# other methods of swapping that were already in /usr/share/X11/xkb/symbols/ctrl:
# // Right Ctrl functions as another right Alt.
# partial modifier_keys
# xkb_symbols "rctrl_ralt" {
# key <RCTL> { symbols[Group1]= [ Alt_R ] };
# modifier_map Mod1{ <RCTL> };
# };
#
# // Menu key functions as another right Ctrl.
# partial modifier_keys
# xkb_symbols "menu_rctrl" {
# replace key <MENU> { [ Control_R, Control_R ] };
# modifier_map Control { Control_L, <MENU> };
# };
#
# // Right Alt key functions as another right Ctrl.
# partial modifier_keys
# xkb_symbols "ralt_rctrl" {
# replace key <RALT> { type[Group1] = "TWO_LEVEL",
# symbols[Group1] = [ Control_R, Control_R ] };
# modifier_map Control { <RALT> };
# };
#
# // Swap the functions of the left Alt key and the left Ctrl key.
# partial modifier_keys
# xkb_symbols "swap_lalt_lctl" {
# replace key <LALT> { [ Control_L, Control_L ] };
# replace key <LCTL> { [ Alt_L, Meta_L ] };
# modifier_map Mod1 { <LCTL> };
# modifier_map Control { <LALT> };
# };
echo "adding config to '/usr/share/X11/xkb/symbols/ctrl'..."
cat <<-EOF | sudo tee -a /usr/share/X11/xkb/symbols/ctrl &>/dev/null
// Swap right Ctrl and and right Alt keys
// from <https://askubuntu.com/questions/885045/how-to-swap-ctrl-and-alt-keys-in-ubuntu-16-04/885047#885047>
xkb_symbols "nocaps" {
replace key <RALT> { [ Control_R, Control_R ] };
replace key <RCTL> { [ Alt_R, Meta_R ] };
};
EOF
}
grep -E -q "ctrl:swap_ralt_rctl = \+ctrl\(swap_ralt_rctl)" /usr/share/X11/xkb/rules/evdev || {
echo "adding config to '/usr/share/X11/xkb/rules/evdev'..."
sudo sed -i.bak -re 's;! option = symbols$;&\n ctrl:swap_ralt_rctl = +ctrl(swap_ralt_rctl);' /usr/share/X11/xkb/rules/evdev
}
grep -E -q "ctrl:swap_ralt_rctl Swap right Alt and right Ctrl" /usr/share/X11/xkb/rules/evdev.lst || {
echo "adding config to '/usr/share/X11/xkb/rules/evdev.lst'..."
sudo sed -i.bak -re 's;! option$;&\n ctrl:swap_ralt_rctl Swap right Alt and right Ctrl;' /usr/share/X11/xkb/rules/evdev.lst
}
#dconf write "/org/gnome/desktop/input-sources/xkb-options" "['ctrl:swap_lalt_lctl','ctrl:swap_ralt_rctl']"
dconf write "/org/gnome/desktop/input-sources/xkb-options" "['ctrl:swap_ralt_rctl']"
fi
if ! w | awk "/${USER}/ { print \$2 }" | grep -q 'pts/2' || [[ ${FORCE} -eq 1 ]]; then
# we're not in a new login session
echo -n "are the keybindings working as expected? [Y/n] "; read
if [[ ${REPLY} =~ ^n$ ]]; then
echo "ok, running alternative commands"
xmodmap - <<-EOF
! from <http://www.chrisamiller.com/blog/posts/remapping-your-macbooks-keyboard-in-ubuntu-1204>
! help from <http://askubuntu.com/questions/131900/how-do-i-switch-the-command-key-and-control-key-on-a-macbook-pro>
!
! clean most of the modifiers
clear control
clear mod4
clear mod1
! -----------------
! left side
! ----------------
! ensure Alt-L behaves like Alt-L
!keycode 64 = Alt_L Meta_L
! ensure Cmd-L behaves like Ctrl-L
!keycode 133 = Control_L NoSymbol Control_L
! ------------------
! right side
! -------------------
${addtl_cmds}
!add mod4 = Super_L
add mod1 = Alt_L Meta_L
add control = Control_L
add control = Control_R
!add mod1 = Alt_R Meta_R
!add control = Control_L
!add control = Control_R
EOF
unset addtl_cmds
fi
fi