1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-14 04:34:49 +00:00
opus/sys/apis/input.lua

154 lines
3.6 KiB
Lua
Raw Normal View History

2017-10-15 23:55:05 +00:00
local Util = require('util')
local keyboard = _G.device.keyboard
local keys = _G.keys
local os = _G.os
2017-10-15 23:55:05 +00:00
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 = {
state = { },
2017-10-15 23:55:05 +00:00
}
2017-10-20 17:57:24 +00:00
function input:modifierPressed()
return keyboard.state[keys.leftCtrl] or
keyboard.state[keys.rightCtrl] or
keyboard.state[keys.leftAlt] or
keyboard.state[keys.rightAlt]
2017-10-20 17:57:24 +00:00
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 keyboard.state[keys.leftCtrl] or keyboard.state[keys.rightCtrl] or
code == keys.leftCtrl or code == keys.rightCtrl then
2017-10-15 23:55:05 +00:00
table.insert(result, 'control')
end
if keyboard.state[keys.leftAlt] or keyboard.state[keys.rightAlt] or
code == keys.leftAlt or code == keys.rightAlt then
2017-10-20 08:23:17 +00:00
table.insert(result, 'alt')
end
2017-10-15 23:55:05 +00:00
if keyboard.state[keys.leftShift] or keyboard.state[keys.rightShift] or
code == keys.leftShift or code == 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.state = { }
2017-10-15 23:55:05 +00:00
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.state[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.state[code] then
2017-10-20 17:57:24 +00:00
self.fired = true
local ch = input:toCode(keys.getName(code), code)
self.state[code] = nil
2017-10-20 17:57:24 +00:00
return ch
2017-10-15 23:55:05 +00:00
end
end
self.state[code] = nil
2017-10-15 23:55:05 +00:00
2017-10-16 04:02:42 +00:00
elseif event == 'paste' then
--self.state[keys.leftCtrl] = nil
--self.state[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