2017-10-15 23:55:05 +00:00
|
|
|
local Util = require('util')
|
|
|
|
|
|
|
|
local keys = _G.keys
|
|
|
|
local os = _G.os
|
|
|
|
|
|
|
|
local modifiers = Util.transpose {
|
|
|
|
keys.leftCtrl, keys.rightCtrl,
|
|
|
|
keys.leftShift, keys.rightShift,
|
2017-10-20 08:23:17 +00:00
|
|
|
keys.leftAlt, keys.rightAlt,
|
2017-10-15 23:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
local input = {
|
|
|
|
pressed = { },
|
|
|
|
}
|
|
|
|
|
2017-10-20 17:57:24 +00:00
|
|
|
function input:modifierPressed()
|
|
|
|
return self.pressed[keys.leftCtrl] or
|
|
|
|
self.pressed[keys.rightCtrl] or
|
|
|
|
self.pressed[keys.leftAlt] or
|
|
|
|
self.pressed[keys.rightAlt]
|
|
|
|
end
|
2017-10-15 23:55:05 +00:00
|
|
|
|
2017-10-20 17:57:24 +00:00
|
|
|
function input:toCode(ch, code)
|
2017-10-15 23:55:05 +00:00
|
|
|
local result = { }
|
|
|
|
|
|
|
|
if self.pressed[keys.leftCtrl] or self.pressed[keys.rightCtrl] then
|
|
|
|
table.insert(result, 'control')
|
|
|
|
end
|
|
|
|
|
2017-10-20 08:23:17 +00:00
|
|
|
if self.pressed[keys.leftAlt] or self.pressed[keys.rightAlt] then
|
|
|
|
table.insert(result, 'alt')
|
|
|
|
end
|
2017-10-15 23:55:05 +00:00
|
|
|
|
|
|
|
if self.pressed[keys.leftShift] or self.pressed[keys.rightShift] then
|
2017-10-20 08:23:17 +00:00
|
|
|
if code and modifiers[code] then
|
2017-10-15 23:55:05 +00:00
|
|
|
table.insert(result, 'shift')
|
2017-10-20 08:23:17 +00:00
|
|
|
elseif #ch == 1 then
|
|
|
|
table.insert(result, ch:upper())
|
2017-10-15 23:55:05 +00:00
|
|
|
else
|
2017-10-20 08:23:17 +00:00
|
|
|
table.insert(result, 'shift')
|
2017-10-20 17:57:24 +00:00
|
|
|
table.insert(result, ch)
|
2017-10-15 23:55:05 +00:00
|
|
|
end
|
2017-10-20 08:23:17 +00:00
|
|
|
elseif not code or not modifiers[code] then
|
2017-10-15 23:55:05 +00:00
|
|
|
table.insert(result, ch)
|
|
|
|
end
|
|
|
|
|
|
|
|
return table.concat(result, '-')
|
|
|
|
end
|
|
|
|
|
|
|
|
function input:reset()
|
|
|
|
self.pressed = { }
|
|
|
|
self.fired = nil
|
2017-10-16 04:02:42 +00:00
|
|
|
|
2017-10-15 23:55:05 +00:00
|
|
|
self.timer = nil
|
2017-10-16 04:02:42 +00:00
|
|
|
self.mch = nil
|
|
|
|
self.mfired = nil
|
2017-10-15 23:55:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function input:translate(event, code, p1, p2)
|
|
|
|
if event == 'key' then
|
|
|
|
if p1 then -- key is held down
|
|
|
|
if not modifiers[code] then
|
2017-10-20 17:57:24 +00:00
|
|
|
self.fired = true
|
|
|
|
return input:toCode(keys.getName(code), code)
|
2017-10-15 23:55:05 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
self.pressed[code] = true
|
2017-10-20 17:57:24 +00:00
|
|
|
if self:modifierPressed() and not modifiers[code] or code == 57 then
|
|
|
|
self.fired = true
|
|
|
|
return input:toCode(keys.getName(code), code)
|
|
|
|
else
|
|
|
|
self.fired = false
|
|
|
|
end
|
2017-10-15 23:55:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
elseif event == 'char' then
|
2017-10-20 17:57:24 +00:00
|
|
|
if not self:modifierPressed() then
|
|
|
|
self.fired = true
|
|
|
|
return input:toCode(code)
|
|
|
|
end
|
2017-10-15 23:55:05 +00:00
|
|
|
|
|
|
|
elseif event == 'key_up' then
|
|
|
|
if not self.fired then
|
|
|
|
if self.pressed[code] then
|
2017-10-20 17:57:24 +00:00
|
|
|
self.fired = true
|
|
|
|
local ch = input:toCode(keys.getName(code), code)
|
2017-10-15 23:55:05 +00:00
|
|
|
self.pressed[code] = nil
|
2017-10-20 17:57:24 +00:00
|
|
|
return ch
|
2017-10-15 23:55:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
self.pressed[code] = nil
|
|
|
|
|
2017-10-16 04:02:42 +00:00
|
|
|
elseif event == 'paste' then
|
|
|
|
self.pressed[keys.leftCtrl] = nil
|
|
|
|
self.pressed[keys.rightCtrl] = nil
|
2017-10-20 08:23:17 +00:00
|
|
|
self.fired = true
|
2017-10-20 17:57:24 +00:00
|
|
|
return input:toCode('paste', 255)
|
2017-10-15 23:55:05 +00:00
|
|
|
|
2017-10-16 04:02:42 +00:00
|
|
|
elseif event == 'mouse_click' then
|
2017-10-15 23:55:05 +00:00
|
|
|
local buttons = { 'mouse_click', 'mouse_rightclick' }
|
2017-10-16 04:02:42 +00:00
|
|
|
self.mch = buttons[code]
|
|
|
|
self.mfired = nil
|
2017-10-15 23:55:05 +00:00
|
|
|
|
|
|
|
elseif event == 'mouse_drag' then
|
2017-10-20 08:23:17 +00:00
|
|
|
self.mfired = true
|
2017-10-20 17:57:24 +00:00
|
|
|
self.fired = true
|
|
|
|
return input:toCode('mouse_drag', 255)
|
2017-10-15 23:55:05 +00:00
|
|
|
|
|
|
|
elseif event == 'mouse_up' then
|
2017-10-16 04:02:42 +00:00
|
|
|
if not self.mfired then
|
2017-10-15 23:55:05 +00:00
|
|
|
local clock = os.clock()
|
|
|
|
if self.timer and
|
|
|
|
p1 == self.x and p2 == self.y and
|
|
|
|
(clock - self.timer < .5) then
|
|
|
|
|
2017-10-16 04:02:42 +00:00
|
|
|
self.mch = 'mouse_doubleclick'
|
2017-10-15 23:55:05 +00:00
|
|
|
self.timer = nil
|
|
|
|
else
|
|
|
|
self.timer = os.clock()
|
|
|
|
self.x = p1
|
|
|
|
self.y = p2
|
|
|
|
end
|
2017-10-20 17:57:24 +00:00
|
|
|
self.mfired = input:toCode(self.mch, 255)
|
2017-10-15 23:55:05 +00:00
|
|
|
else
|
2017-10-16 04:02:42 +00:00
|
|
|
self.mch = 'mouse_up'
|
2017-10-20 17:57:24 +00:00
|
|
|
self.mfired = input:toCode(self.mch, 255)
|
2017-10-15 23:55:05 +00:00
|
|
|
end
|
2017-10-20 08:23:17 +00:00
|
|
|
self.fired = true
|
2017-10-16 04:02:42 +00:00
|
|
|
return self.mfired
|
2017-10-15 23:55:05 +00:00
|
|
|
|
|
|
|
elseif event == "mouse_scroll" then
|
|
|
|
local directions = {
|
|
|
|
[ -1 ] = 'scrollUp',
|
|
|
|
[ 1 ] = 'scrollDown'
|
|
|
|
}
|
2017-10-20 08:23:17 +00:00
|
|
|
self.fired = true
|
2017-10-20 17:57:24 +00:00
|
|
|
return input:toCode(directions[code], 255)
|
2017-10-16 04:02:42 +00:00
|
|
|
end
|
|
|
|
end
|
2017-10-15 23:55:05 +00:00
|
|
|
|
2017-10-16 04:02:42 +00:00
|
|
|
function input:test()
|
|
|
|
while true do
|
|
|
|
local ch = self:translate(os.pullEvent())
|
|
|
|
if ch then
|
2017-10-20 17:57:24 +00:00
|
|
|
print('GOT: ' .. tostring(ch))
|
2017-10-16 04:02:42 +00:00
|
|
|
end
|
2017-10-15 23:55:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return input
|