forked from zhou13/hammerspoon-windows-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
164 lines (152 loc) · 4.01 KB
/
init.lua
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
164
local eventtap = hs.eventtap
local eventTypes = eventtap.event.types
local keycodes = hs.keycodes.map
SKIPPED_BUNDLE_IDS = {
["org.virtualbox.app.VirtualBoxVM"] = true,
["com.parallels.desktop.console"] = true,
["org.vmware.fusion"] = true,
["org.gnu.emacs"] = true,
["org.gnu.Emacs"] = true,
["com.jetbrains"] = true,
["com.microsoft.VSCode"] = true,
["com.vscodium"] = true,
["com.sublimetext.3"] = true,
["net.kovidgoyal.kitty"] = true,
["com.citrix.XenAppViewer"] = true,
["com.microsoft.rdc.macos"] = true,
["io.alacritty"] = true,
["co.zeit.hyper"] = true,
["com.googlecode.iterm2"] = true,
["com.apple.Terminal"] = true,
["com.github.wez.wezterm"] = true,
}
SCENE_BUNDLE_IDS = {
browser = {
["com.google.Chrome"] = true,
["com.brave.Browser"] = true,
["com.vivaldi.Vivaldi"] = true,
["org.mozilla.firefox"] = true,
["org.mozilla.nightly"] = true,
["com.apple.Safari"] = true,
},
}
KEY_MAPS = {
["ctrl+a"] = "cmd+a",
["ctrl+b"] = "cmd+b",
["ctrl+c"] = "cmd+c",
["ctrl+f"] = "cmd+f",
["ctrl+g"] = "cmd+g",
["ctrl+h"] = "cmd+h",
["ctrl+i"] = "cmd+i",
["ctrl+l"] = "cmd+l",
["ctrl+n"] = "cmd+n",
["ctrl+o"] = "cmd+o",
["ctrl+p"] = "cmd+p",
["ctrl+r"] = "cmd+r",
["ctrl+s"] = "cmd+s",
["ctrl+t"] = "cmd+t",
["ctrl+u"] = "cmd+u",
["ctrl+v"] = "cmd+v",
["ctrl+w"] = "cmd+w",
["ctrl+x"] = "cmd+x",
["ctrl+y"] = "cmd+y",
["ctrl+z"] = "cmd+z",
["home"] = "cmd+left",
["shift+home"] = "cmd+shift+left",
["end"] = "cmd+right",
["shift+end"] = "cmd+shift+right",
["ctrl+delete"] = "option+delete", -- delete is backspace in OSX
["ctrl+forwarddelete"] = "option+forwarddelete", -- forwarddelete is delete in OSX
["ctrl+left"] = "option+left",
["ctrl+shift+left"] = "option+shift+left",
["ctrl+right"] = "option+right",
["ctrl+shift+right"] = "option+shift+right",
}
KEY_MAP_SCENES = {
["ctrl+h"] = "browser",
["ctrl+l"] = "browser",
}
local function setHotkey(hotkey, event)
print("set hotkey: " .. hotkey)
local keycode = event:getKeyCode()
local flags = event:getFlags()
flags.ctrl = false
flags.option = false
flags.cmd = false
flags.shift = false
for key, plusSign in string.gmatch(hotkey, "([^+]+)([+]?)") do
print(key, plusSign)
if plusSign == "+" then
if key == "ctrl" then
flags.ctrl = true
elseif key == "option" then
flags.alt = true
elseif key == "cmd" then
flags.cmd = true
elseif key == "shift" then
flags.shift = true
end
else
keycode = keycodes[key]
end
end
event:setKeyCode(keycode)
event:setFlags(flags)
end
local function toHotkey(event)
local keycode = event:getKeyCode()
local flags = event:getFlags()
local hotkey = ""
if flags.ctrl then
hotkey = hotkey .. "ctrl+"
end
if flags.cmd then
hotkey = hotkey .. "cmd+"
end
if flags.alt then
hotkey = hotkey .. "option+"
end
if flags.shift then
hotkey = hotkey .. "shift+"
end
if hotkey:sub(-1) ~= "+" then
hotkey = hotkey:sub(1, -2)
end
if keycodes then
hotkey = hotkey .. keycodes[keycode]
end
print("read hotkey: " .. hotkey)
return hotkey
end
local function eventHandler(event)
local activeWindow = hs.window.focusedWindow()
local bundleID = ""
if activeWindow ~= nil then
local activeApp = activeWindow:application()
bundleID = activeApp:bundleID() or ""
if SKIPPED_BUNDLE_IDS[bundleID] ~= nil then
return false
end
end
print("bundleID: " .. bundleID)
local hotkey = toHotkey(event)
local mapped_hotkey = KEY_MAPS[hotkey]
if mapped_hotkey ~= nil then
local scene = KEY_MAP_SCENES[hotkey]
if scene ~= nil then
if SCENE_BUNDLE_IDS[scene][bundleID] == nil then
return false
end
end
setHotkey(mapped_hotkey, event)
end
return false
end
-- WARNING: DO NOT make it a local variable, or hammerspoon will freezes randomly
KeyWatcher = eventtap.new({
eventTypes.flagsChanged,
eventTypes.keyDown,
eventTypes.keyUp,
}, eventHandler)
KeyWatcher:start()
-- vim: set sw=2: