input redo

This commit is contained in:
kepler155c@gmail.com 2017-10-20 13:57:24 -04:00
parent 31b3787695
commit 8e9ff9c626
1 changed files with 30 additions and 15 deletions

View File

@ -13,8 +13,14 @@ local input = {
pressed = { }, pressed = { },
} }
function input:toCode(ch, code) function input:modifierPressed()
return self.pressed[keys.leftCtrl] or
self.pressed[keys.rightCtrl] or
self.pressed[keys.leftAlt] or
self.pressed[keys.rightAlt]
end
function input:toCode(ch, code)
local result = { } local result = { }
if self.pressed[keys.leftCtrl] or self.pressed[keys.rightCtrl] then if self.pressed[keys.leftCtrl] or self.pressed[keys.rightCtrl] then
@ -32,7 +38,7 @@ function input:toCode(ch, code)
table.insert(result, ch:upper()) table.insert(result, ch:upper())
else else
table.insert(result, 'shift') table.insert(result, 'shift')
table.insert(result, keys.getName(code) or ch) table.insert(result, ch)
end end
elseif not code or not modifiers[code] then elseif not code or not modifiers[code] then
table.insert(result, ch) table.insert(result, ch)
@ -54,24 +60,32 @@ function input:translate(event, code, p1, p2)
if event == 'key' then if event == 'key' then
if p1 then -- key is held down if p1 then -- key is held down
if not modifiers[code] then if not modifiers[code] then
self.fired = input:toCode(keys.getName(code), code) self.fired = true
return self.fired return input:toCode(keys.getName(code), code)
end end
else else
self.fired = nil
self.pressed[code] = true self.pressed[code] = true
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
end end
elseif event == 'char' then elseif event == 'char' then
self.fired = true if not self:modifierPressed() then
return input:toCode(code) self.fired = true
return input:toCode(code)
end
elseif event == 'key_up' then elseif event == 'key_up' then
if not self.fired then if not self.fired then
if self.pressed[code] then if self.pressed[code] then
self.fired = input:toCode(keys.getName(code), code) self.fired = true
local ch = input:toCode(keys.getName(code), code)
self.pressed[code] = nil self.pressed[code] = nil
return self.fired return ch
end end
end end
self.pressed[code] = nil self.pressed[code] = nil
@ -80,7 +94,7 @@ function input:translate(event, code, p1, p2)
self.pressed[keys.leftCtrl] = nil self.pressed[keys.leftCtrl] = nil
self.pressed[keys.rightCtrl] = nil self.pressed[keys.rightCtrl] = nil
self.fired = true self.fired = true
return input:toCode('paste') return input:toCode('paste', 255)
elseif event == 'mouse_click' then elseif event == 'mouse_click' then
local buttons = { 'mouse_click', 'mouse_rightclick' } local buttons = { 'mouse_click', 'mouse_rightclick' }
@ -89,7 +103,8 @@ function input:translate(event, code, p1, p2)
elseif event == 'mouse_drag' then elseif event == 'mouse_drag' then
self.mfired = true self.mfired = true
return input:toCode('mouse_drag') self.fired = true
return input:toCode('mouse_drag', 255)
elseif event == 'mouse_up' then elseif event == 'mouse_up' then
if not self.mfired then if not self.mfired then
@ -105,10 +120,10 @@ function input:translate(event, code, p1, p2)
self.x = p1 self.x = p1
self.y = p2 self.y = p2
end end
self.mfired = input:toCode(self.mch) self.mfired = input:toCode(self.mch, 255)
else else
self.mch = 'mouse_up' self.mch = 'mouse_up'
self.mfired = input:toCode(self.mch) self.mfired = input:toCode(self.mch, 255)
end end
self.fired = true self.fired = true
return self.mfired return self.mfired
@ -119,7 +134,7 @@ function input:translate(event, code, p1, p2)
[ 1 ] = 'scrollDown' [ 1 ] = 'scrollDown'
} }
self.fired = true self.fired = true
return input:toCode(directions[code]) return input:toCode(directions[code], 255)
end end
end end
@ -127,7 +142,7 @@ function input:test()
while true do while true do
local ch = self:translate(os.pullEvent()) local ch = self:translate(os.pullEvent())
if ch then if ch then
print('GOT: ' .. ch) print('GOT: ' .. tostring(ch))
end end
end end
end end