23
« on: June 03, 2012, 02:39:48 pm »
Excuse me. This is the code which i have tested.
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}
end
function 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 = true
end
function MasterKey:updateStatus(timeNow)
local timeNow = timeNow or timer.getMilliSecCounter()
self.keyDown = timeNow - self.time.firstEvent < self.firstEventInterval
or timeNow - self.time.lastEvent < self.eventInterval
end
function MasterKey:isDown()
return self.keyDown
end
Keys = class()
function Keys:init(keys, eventInterval)
self.keys = {}
for i = 1, #keys do
self.keys[keys] = {keyDown = false, timeLastEvent = 0}
end
self.eventInterval = eventInterval or 200
end
function Keys:keyEvent(key, timeNow)
self.keys[nobbc].timeLastEvent = timeNow or timer.getMilliSecCounter()
end
function 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
end
end
function Keys:isDown(key)
return self.keys[nobbc].keyDown
end
function on.tabKey()
tab:keyEvent()
end
function on.charIn(c)
if c == "x" then
keys:keyEvent("x")
elseif c == "y" then
keys:keyEvent("y")
elseif c == "z" then
keys:keyEvent("z")
end
end
function on.timer()
platform.window:invalidate()
end
function on.create()
timer.start(0.01)
end
tab = 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