-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphrases-keychord.pd_lua
106 lines (67 loc) · 2.21 KB
/
phrases-keychord.pd_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
local KeyParser = pd.Class:new():register("phrases-keychord")
local commands = require("phrases-keychord-tables")
function KeyParser:initialize(sel, atoms)
-- 1. A key command in list form, generated by using [pack f s] on the output of [keyname]
self.inlets = 1
-- 1. Custom commands out
-- 2. Regular keys out
self.outlets = 2
self.keysdown = {}
return true
end
function KeyParser:in_1_list(cmd)
if #cmd[2] >= 3 then
-- Collapse every _L and _R button into the same value (e.g. Shift_L and Shift_R become equivalent)
local suffix = cmd[2]:sub(#cmd[2] - 1)
if (suffix == "_L")
or (suffix == "_R")
then
cmd[2] = cmd[2]:sub(1, #cmd[2] - 2)
end
end
if cmd[1] == 1 then -- On down-keystrokes, put the keypress into the keysdown table, compute commands, and send out its symbol
if cmd[2] == "Space" then -- Keychord panic command, to flush the keychord array
self.keysdown = {}
self:outlet(1, "symbol", {"UPDATE_EDITOR_GUI"})
end
self.keysdown[cmd[2]] = cmd[2]
self:outlet(2, "symbol", {cmd[2]})
for k, v in pairs(commands) do
local sendflag = true
-- Reorganize the "v" table to have a comparable layout to the "keysdown" table
local temptab = {}
for _, vv in pairs(v) do
temptab[vv] = vv
end
-- Ensure that the command is only sent if the contents of "keysdown" are identical to the contents of "temptab"
for kk, _ in pairs(self.keysdown) do
if temptab[kk] == nil then
sendflag = false
end
end
for kk, _ in pairs(temptab) do
if self.keysdown[kk] == nil then
sendflag = false
end
end
-- If the two tables are identical, send the command and empty all non-modifier keys from the keysdown table
if sendflag == true then
self:outlet(1, "symbol", {k})
local temptab = self.keysdown
for k, v in pairs(self.keysdown) do
if
(k ~= "Shift")
and (k ~= "Ctrl")
and (k ~= "Alt")
and (k ~= "Tab")
then
temptab[k] = nil
end
end
self.keysdown = temptab
end
end
elseif self.keysdown[cmd[2]] ~= nil then -- On up-keystrokes, if keysdown[key] isn't nil, remove it from keysdown
self.keysdown[cmd[2]] = nil
end
end