summaryrefslogtreecommitdiff
path: root/.local/bin/desktop/remap
blob: 363546421822b2511f9ddcb6cd59ae0dc208ceb2 (plain)
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
#!/bin/sh

# This script cannot run without the X server (GUI)
# So exit if DISPLAY is not set
[ -z "$DISPLAY" ] && {
	notify-send remap "ERROR: DISPLAY not set"
	exit
}

timeout="${KEY_RELEASE_TIMEOUT:-500}"

help() { echo "remap - remap modifier keys for easy access

USAGE:
	remap [OPTION]...

OPTIONS:
	-x  reset keys
	-t  toggle (remap/reset)
	-s  show remap status
	-i  show info of all key remaps
	-h  show this help message"; }

key_info() { echo "
	+-----------------+-----------------------------------+
	| Before          | After                             |
	+-----------------+-----------------+-----------------+
	| Key             | On Hold         | On Tap          |
	+-----------------+-----------------+-----------------+
	| Tab             | Super           | Tab             |
	| Caps Lock       | Ctrl            | Esc             |
	| Space           | Shift           | Space           |
	| Left Ctrl       | Ctrl            | Caps Lock       |
	| Escape          | Tab             | Tab             |
	+-----------------+-----------------+-----------------+

Note:
  By default, 'On Tap' will timeout at 500 miliseconds. In other words,
  if you release a key after timeout, the key will not be generated.
  You can increase it's value by setting the \$KEY_RELEASE_TIMEOUT environment
  variable. This is useful in case you don't have a fast typing speed.

If you want to change more keys or want a different configuration, you
can edit the remap_keys() function inside this script.
	"; }

while getopts 'rxtsih' o; do case "$o" in
  x) action=x ;;
  t) action=t ;;
  s) action=s ;;
  i) key_info >&2; exit ;;
  h) help >&2; exit ;;
	*) printf "remap: invalid argument -- '%s'\n" "$OPTARG" ;;
esac; done

status_file="/tmp/.remap.lock"
keymap_file="/tmp/.xmodmap.defaults"
[ -f "$keymap_file" ] || xmodmap -pke > "$keymap_file"

msg() {
	[ "$VERBOSE_REMAP" != 1 ] && return
	printf "remap: %s\n" "$@"
	# notify-send "remap" "$*"
}

remap_keys() {
	msg "remapping keys..."
	# Fast key repeats
	xset r rate 300 50

	# New key behaviour on Hold
	# lines after ! are comments
	xmodmap - <<-EOF
	!          Hold Caps_Lock -> Control_R
	remove lock = Caps_Lock
	remove Control = Control_R
	! keysym Control_R = Caps_Lock
	keysym Caps_Lock = Control_R
	! add lock = Caps_Lock
	add Control = Control_R
	keycode any = Caps_Lock

	!          Hold space -> Shift_R
	remove shift = Shift_R
	keycode 65 = Shift_R
	add shift = Shift_R
	keycode any = space

	!          Hold Tab -> Super_L
	! !          Escape -> Tab
	remove mod4 = Super_L
	keycode 23 = Super_L
	keycode any = Tab ISO_Left_Tab Tab ISO_Left_Tab
	! keycode 133 = Tab ISO_Left_Tab Tab ISO_Left_Tab
	add mod4 = Super_L
	! keycode 230 = Escape
	! keycode 9 = Tab ISO_Left_Tab Tab ISO_Left_Tab
	EOF

	# New key behaviour on Tap
	killall xcape 2>/dev/null
	xcape -t "$timeout" -e "Super_L=Tab"
	xcape -t "$timeout" -e "Control_R=Escape"
	xcape -t "$timeout" -e "Shift_L=Caps_Lock"
	xcape -t "$timeout" -e "Shift_R=space"
	date > "$status_file"
	xset led 3
	msg "keys remapped"
}


reset_keys() {
	msg "resetting keys..."
  xset r rate
  killall xcape 2>/dev/null
  setxkbmap -layout us
  xmodmap "$keymap_file"

  [ -f "$status_file" ] && rm "$status_file"
  xset -led 3
	msg "keys reset"
}

toggle_remap() {
	if [ -f "$status_file" ]; then
		reset_keys
	else
		remap_keys
	fi
}

remap_status() {
  if [ -f "$status_file" ]; then
    printf "Keys are remapped since "
		cat "$status_file"
  else
    echo "Keys are NOT remapped"
  fi
}

case "$action" in
	s) remap_status ;;
  x) reset_keys ;;
  t) toggle_remap ;;
	*) remap_keys ;;
esac