-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasynckey.lua
More file actions
215 lines (195 loc) · 3.92 KB
/
Copy pathasynckey.lua
File metadata and controls
215 lines (195 loc) · 3.92 KB
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
local asynckey = {}
asynckey.delay = 0.001
if not love or love.system.getOS() ~= "Windows" then
return asynckey
end
local ThreadPool = require("thread.ThreadPool")
local keymap
---@param event table
---@return table
local function transform(event)
local key = keymap[event.key]
if key then
event.key = key
end
return event
end
local threadCode
function asynckey.start()
local thread = love.thread.newThread(threadCode:gsub("<delay>", asynckey.delay))
thread:start()
local channel = love.thread.getChannel("AsyncInputChannel")
local control_channel = love.thread.getChannel("AsyncInputControlChannel")
control_channel:clear()
local managed_thread = {
isRunning = function()
return thread:isRunning()
end,
stop = function()
control_channel:push({name = "stop"})
end,
}
ThreadPool:registerManagedThread(asynckey, "asynckey", managed_thread, true)
asynckey.events = coroutine.wrap(function()
while true do
local event = channel:pop()
while event do
coroutine.yield(transform(event))
event = channel:pop()
end
coroutine.yield()
end
end)
function asynckey.stop()
managed_thread:stop()
end
function asynckey.clear()
channel:clear()
end
asynckey.start = nil
end
threadCode = [[
local ffi = require("ffi")
local bit = require("bit")
ffi.cdef("int16_t GetAsyncKeyState(int vKey);")
---@class ffi.namespace*
---@field GetAsyncKeyState fun(vKey: integer): integer
require("love.timer")
local keys = {}
for i = 0, 255 do
keys[i] = false
end
local channel = love.thread.getChannel("AsyncInputChannel")
local control_channel = love.thread.getChannel("AsyncInputControlChannel")
local event = {}
local function send(key, state, time)
event.key = key
event.state = state
event.time = time
channel:push(event)
end
while true do
local control_event = control_channel:pop()
if control_event and control_event.name == "stop" then
return
end
local time = love.timer.getTime()
for key = 0, 255 do
local state = bit.band(ffi.C.GetAsyncKeyState(key), 0x8000) ~= 0
if keys[key] ~= state then
send(key, state, time)
keys[key] = state
end
end
love.timer.sleep(<delay>)
end
]]
keymap = {
[0x08] = "backspace",
[0x09] = "tab",
[0x0C] = "kp5",
[0x0D] = "return",
[0x14] = "capslock",
[0x1B] = "escape",
[0x20] = "space",
[0x21] = "pageup",
[0x22] = "pagedown",
[0x23] = "end",
[0x24] = "home",
[0x25] = "left",
[0x26] = "up",
[0x27] = "right",
[0x28] = "down",
[0x2C] = "printscreen",
[0x2D] = "insert",
[0x2E] = "delete",
[0x30] = "0",
[0x31] = "1",
[0x32] = "2",
[0x33] = "3",
[0x34] = "4",
[0x35] = "5",
[0x36] = "6",
[0x37] = "7",
[0x38] = "8",
[0x39] = "9",
[0x41] = "a",
[0x42] = "b",
[0x43] = "c",
[0x44] = "d",
[0x45] = "e",
[0x46] = "f",
[0x47] = "g",
[0x48] = "h",
[0x49] = "i",
[0x4A] = "j",
[0x4B] = "k",
[0x4C] = "l",
[0x4D] = "m",
[0x4E] = "n",
[0x4F] = "o",
[0x50] = "p",
[0x51] = "q",
[0x52] = "r",
[0x53] = "s",
[0x54] = "t",
[0x55] = "u",
[0x56] = "v",
[0x57] = "w",
[0x58] = "x",
[0x59] = "y",
[0x5A] = "z",
[0x5B] = "lgui",
[0x60] = "kp0",
[0x61] = "kp1",
[0x62] = "kp2",
[0x63] = "kp3",
[0x64] = "kp4",
[0x65] = "kp5",
[0x66] = "kp6",
[0x67] = "kp7",
[0x68] = "kp8",
[0x69] = "kp9",
[0x6A] = "kp*",
[0x6B] = "kp+",
[0x6D] = "kp-",
[0x6E] = "kp.",
[0x6F] = "kp/",
[0x70] = "f1",
[0x71] = "f2",
[0x72] = "f3",
[0x73] = "f4",
[0x74] = "f5",
[0x75] = "f6",
[0x76] = "f7",
[0x77] = "f8",
[0x78] = "f9",
[0x79] = "f10",
[0x7A] = "f11",
[0x7B] = "f12",
[0x90] = "numlock",
[0xA0] = "lshift",
[0xA1] = "rshift",
[0xA2] = "lctrl",
[0xA3] = "rctrl",
[0xA4] = "lalt",
[0xA5] = "ralt",
[0xAD] = "audiomute",
[0xAE] = "volumedown",
[0xAF] = "volumeup",
[0xB0] = "audionext",
[0xB1] = "audioprev",
[0xB3] = "audioplay",
[0xBA] = ";",
[0xBB] = "=",
[0xBC] = ",",
[0xBD] = "-",
[0xBE] = ".",
[0xBF] = "/",
[0xC0] = "`",
[0xDB] = "[",
[0xDC] = "\\",
[0xDD] = "]",
[0xDE] = "'",
}
return asynckey