-
Notifications
You must be signed in to change notification settings - Fork 1
/
Keys.lua
239 lines (203 loc) · 5.54 KB
/
Keys.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
-- A cleaner way of connecting functions to key-presses
-- @readme https://github.com/RoStrap/Input/blob/master/README.md
-- @author Validark
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Resources = require(ReplicatedStorage:WaitForChild("Resources"))
local Debug = Resources:LoadLibrary("Debug")
local DeclarationAmbiguous = {
Ctrl = "LeftControl";
Cmd = "LeftControl";
Command = "LeftControl";
Control = "LeftControl";
WinKey = "LeftSuper";
Windows = "LeftSuper";
Shift = "LeftShift";
Alt = "LeftAlt";
Enter = "Return";
Dash = "Minus";
Hyphen = "Minus";
Stop = "Period";
[0] = "Zero"; ['0'] = "Zero";
[1] = "One"; ['1'] = "One";
[2] = "Two"; ['2'] = "Two";
[3] = "Three"; ['3'] = "Three";
[4] = "Four"; ['4'] = "Four";
[5] = "Five"; ['5'] = "Five";
[6] = "Six"; ['6'] = "Six";
[7] = "Seven"; ['7'] = "Seven";
[8] = "Eight"; ['8'] = "Eight";
[9] = "Nine"; ['9'] = "Nine";
['['] = "LeftBracket";
[']'] = "RightBracket";
['\''] = "BackSlash";
['\''] = "Quote";
[';'] = "Semicolon";
[','] = "Comma";
['.'] = "Period";
['/'] = "Slash";
['-'] = "Minus";
['='] = "Equals";
['`'] = "Backquote";
}
local KeyUps = {}
local KeyDowns = {}
local Combinations = {}
local Unknown = Enum.KeyCode.Unknown
local function KeyDown(Data, GuiInput)
if not GuiInput and Data.KeyCode ~= Unknown then
local KeyValue = Data.KeyCode.Value
local CombinationHash = Combinations[KeyValue]
if CombinationHash then
for Number, Function in next, CombinationHash do
if UserInputService:IsKeyDown(Number) then
return Function()
end
end
end
local Function = KeyDowns[KeyValue]
if Function then
return Function()
end
end
end
local function KeyUp(Data, GuiInput)
if not GuiInput and Data.KeyCode ~= Unknown then
local Function = KeyUps[Data.KeyCode.Value]
if Function then
return Function()
end
end
end
local Multicaller = {} -- Shhhh, we use this in two different ways. Just don't call your RbxScriptSignal xP
Multicaller.__index = Multicaller
function Multicaller:__call()
self.Bindable:Fire()
local Function = self[1]
if Function then
Function()
end
end
function Multicaller:Disconnect()
local Storage = self.KeyEvent.Storage
local KeyValue = self.KeyEvent.KeyValue
local Existing = Storage[KeyValue]
if type(Existing) == "table" then
Existing[1] = function() end
else
Storage[KeyValue] = nil
end
end
local Key = {}
Key.__index = {}
local DualEvent = {}
DualEvent.__index = setmetatable({}, {__index = Key.__index})
function DualEvent.__index:IsDown()
return UserInputService:IsKeyDown(self.KeyValue) and UserInputService:IsKeyDown(self.KeyValue2)
end
function Key.__add(a, b)
Debug.Assert(a and b and a.Storage == KeyDowns and b.Storage == KeyDowns, "You can only chain 2 KeyDown events")
local Storage = Combinations[b.KeyValue]
if not Storage then
Storage = {}
Combinations[b.KeyValue] = Storage
end
return setmetatable({
KeyName = a.KeyName;
KeyValue = a.KeyValue;
KeyValue2 = b.KeyValue;
Storage = Storage;
}, DualEvent)
end
function Key.__index:IsDown()
return UserInputService:IsKeyDown(self.KeyValue)
end
function Key.__index:Connect(Function)
local Storage = self.Storage
local KeyValue = self.KeyValue
local Existing = Storage[KeyValue]
if Existing then
if type(Existing) == "table" then
local Connection = Existing.Bindable.Event:Connect(Function)
Existing.Connections[#Existing.Connections + 1] = Connection
return Connection
else
local Bindable = Instance.new("BindableEvent")
local Connection = Bindable.Event:Connect(Function)
Storage[KeyValue] = setmetatable({
Existing;
Bindable = Bindable;
Connections = {Connection};
}, Multicaller)
return Connection
end
else
Storage[KeyValue] = Function
return setmetatable({KeyEvent = self}, Multicaller)
end
end
function Key.__index:Disconnect()
local Existing = self.Storage[self.KeyValue]
if type(Existing) == "table" then
local Connections = Existing.Connections
for a = 1, #Connections do
Connections[a]:Disconnect()
Connections[a] = nil
end
Existing.Bindable, Existing.Connections, Existing[1] = Existing.Bindable:Destroy()
end
self.Storage[self.KeyValue] = nil
end
function Key.__index:Press()
self.Storage[self.KeyValue]()
end
Key.__index.Fire = Key.__index.Press
function Key.__index:Wait()
local Existing = self.Storage[self.KeyValue]
if type(Existing) == "table" then
Existing.Bindable.Event:Wait()
else
local Caller = setmetatable({
Existing;
Bindable = Instance.new("BindableEvent");
Connections = {};
}, Multicaller)
self.Storage[self.KeyValue] = Caller
Caller.Bindable.Event:Wait()
if #Caller.Connections == 0 then
self.Storage[self.KeyValue] = Existing
Caller.Bindable, Caller.Connections, Caller[1] = Caller.Bindable:Destroy()
end
end
end
local Connection1, Connection2
return setmetatable({
Pause = function(self)
Connection1:Disconnect()
Connection2:Disconnect()
return self
end;
Resume = function(self)
Connection1 = UserInputService.InputBegan:Connect(KeyDown)
Connection2 = UserInputService.InputEnded:Connect(KeyUp)
return self
end;
}, {
__index = function(self, KeyName)
local KeyCode = Enum.KeyCode[DeclarationAmbiguous[KeyName] or KeyName]
local NewKey = {
KeyUp = setmetatable({
KeyName = KeyCode.Name;
KeyValue = KeyCode.Value;
Storage = KeyUps;
}, Key);
KeyDown = setmetatable({
KeyName = KeyCode.Name;
KeyValue = KeyCode.Value;
Storage = KeyDowns;
}, Key);
}
self[KeyCode.Name] = NewKey
return NewKey
end
}):Resume()