0 Members and 4 Guests are viewing this topic.
Key = class()function Key:init(eventInterval, firstEventInterval) self.keyDown = false self.eventInterval = eventInterval or 150 self.firstEventInterval = firstEventInterval or 750 self.time = {firstEvent = 0, lastEvent = 0}endfunction Key:keyEvent(timeNow) local timeNow = timeNow or timer.getMilliSecCounter() if not self.keyDown then self.time.firstEvent = timeNow end self.time.lastEvent = timeNow self.keyDown = trueendfunction Key:isDown(timeNow) local timeNow = timeNow or timer.getMilliSecCounter() self.keyDown = timeNow - self.time.firstEvent < self.firstEventInterval or timeNow - self.time.lastEvent < self.eventInterval return self.keyDownendfunction on.tabKey() tab:keyEvent()endfunction on.timer() platform.window:invalidate()endfunction on.create() timer.start(0.01)endtab = Key()function on.paint(gc) gc:drawString("Tab key down: " .. tostring(tab:isDown()), 0, 0, "top")end
MasterKey = class()function MasterKey:init(eventInterval, firstEventInterval) self.keyDown = false self.eventInterval = eventInterval or 200 self.firstEventInterval = firstEventInterval or 750 self.time = {firstEvent = 0, lastEvent = 0}endfunction MasterKey:keyEvent(timeNow) local timeNow = timeNow or timer.getMilliSecCounter() if not self.keyDown then self.time.firstEvent = timeNow end self.time.lastEvent = timeNow self.keyDown = trueendfunction MasterKey:updateStatus(timeNow) local timeNow = timeNow or timer.getMilliSecCounter() self.keyDown = timeNow - self.time.firstEvent < self.firstEventInterval or timeNow - self.time.lastEvent < self.eventIntervalendfunction MasterKey:isDown() return self.keyDownendKeys = class()function Keys:init(keys, eventInterval) self.keys = {} for i = 1, #keys do self.keys[keys[i]] = {keyDown = false, timeLastEvent = 0} end self.eventInterval = eventInterval or 200endfunction Keys:keyEvent(key, timeNow) self.keys[nobbc].timeLastEvent = timeNow or timer.getMilliSecCounter()endfunction Keys:updateStatus(key, masterKey) if masterKey.keyDown then if not self.keys[nobbc].keyDown and masterKey.time.lastEvent - self.keys[nobbc].timeLastEvent < self.eventInterval then for key, value in pairs(self.keys) do self.keys[nobbc].keyDown = false end self.keys[nobbc].keyDown = true end else self.keys[nobbc].keyDown = false endendfunction Keys:isDown(key) return self.keys[nobbc].keyDownendfunction on.tabKey() tab:keyEvent()endfunction on.charIn(c) if c == "x" then keys:keyEvent("x") elseif c == "y" then keys:keyEvent("y") elseif c == "z" then keys:keyEvent("z") endendfunction on.timer() platform.window:invalidate()endfunction on.create() timer.start(0.01)endtab = MasterKey()keys = Keys({"x", "y", "z"})function on.paint(gc) tab:updateStatus() keys:updateStatus("x", tab) keys:updateStatus("y", tab) keys:updateStatus("z", tab) gc:drawString("'x' key down: " .. tostring(keys:isDown("x")), 0, 0, "top") gc:drawString("'y' key down: " .. tostring(keys:isDown("y")), 0, 20, "top") gc:drawString("'z' key down: " .. tostring(keys:isDown("z")), 0, 40, "top")end
function on.timer() timer.stop() platform.window:invalidate()endtab = Key()function on.paint(gc) gc:drawString("Tab key down: " .. tostring(tab:isDown()), 0, 0, "top") timer.start(0.01)end
function on.timer() platform.window:invalidate()endtab = Key()function on.create() timer.start(0.01)endfunction on.paint(gc) gc:drawString("Tab key down: " .. tostring(tab:isDown()), 0, 0, "top")end