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
local SLOTS = 5
local MOD_NAMES = {
"Ctrl+Alt",
"Ctrl+Shift",
"Alt+Shift",
"Ctrl+Alt+Shift",
"Cmd/Win+Alt",
"Cmd/Win+Shift",
}
local MODS = {
["Ctrl+Alt"] = { ctrl = true, alt = true },
["Ctrl+Shift"] = { ctrl = true, shift = true },
["Alt+Shift"] = { alt = true, shift = true },
["Ctrl+Alt+Shift"] = { ctrl = true, alt = true, shift = true },
["Cmd/Win+Alt"] = { win = true, alt = true },
["Cmd/Win+Shift"] = { win = true, shift = true },
}
local schema = {
captureModifier = UI.Select(
"Ctrl+Alt",
MOD_NAMES,
{ label = "Capture: modifier + slot key", tab = "Settings" }
),
insertModifier = UI.Select(
"Ctrl+Shift",
MOD_NAMES,
{ label = "Insert: modifier + slot key", tab = "Settings" }
),
}
for n = 1, SLOTS do
schema[`key{n}`] =
UI.Keybind(tostring(n), { label = `Slot {n} key`, tab = "Settings" })
end
local cfg = UI.Schema(schema)
local slots = {}
local function chordHeld(name)
local want = MODS[name] or {}
local have = Input.GetModifiers()
for _, m in { "ctrl", "alt", "shift", "win" } do
if (want[m] == true) ~= have[m] then
return false
end
end
return true
end
local function capture(n)
local ok, text = pcall(Clipboard.Get)
if not ok then
Log.Error(`slot {n}: {text}`)
UI.Notify("Clipboard could not be read", "error")
return
end
if text == nil or text == "" then
UI.Notify("Clipboard has no text", "warning")
return
end
slots[n] = text
UI.Notify(`Slot {n}: {#text} characters`, "success")
end
local function insert(n)
Run(function()
local waited = 0
while waited < 1000 do
local m = Input.GetModifiers()
if not (m.ctrl or m.alt or m.shift or m.win) then
break
end
Sleep(10)
waited += 10
end
if waited >= 1000 then
UI.Notify("Release the modifier keys to insert", "warning")
return
end
local text = slots[n]
if text == nil then
UI.Notify(`Slot {n} is empty`, "warning")
return
end
local ok, err = pcall(System.TypeText, text)
if not ok then
Log.Error(`slot {n}: {err}`)
UI.Notify(`Slot {n} could not be typed`, "error")
end
end)
end
for n = 1, SLOTS do
local key = cfg[`key{n}`]
Bind(key, {
when = function()
return chordHeld(cfg.captureModifier)
end,
action = function()
capture(n)
return nil
end,
})
Bind(key, {
when = function()
return chordHeld(cfg.insertModifier)
end,
action = function()
return nil
end,
release = function()
insert(n)
end,
})
end
function OnStart()
Log.Info(
`{cfg.captureModifier} + key captures, {cfg.insertModifier} + key types. Slots clear on stop.`
)
end
function OnStop()
slots = {}
end