feat: handle key events

This commit is contained in:
2025-05-06 20:02:07 -03:00
parent db7556cabf
commit d6a9408fcd
+17 -4
View File
@@ -128,12 +128,12 @@ declareCommandHandler("SCAN", handleScan,
event handling event handling
]] ]]
local eventListeners = {} local eventListeners = {}
local function registerEventListener(name, listener) function registerEventListener(name, listener)
if not eventListeners[name] then eventListeners[name] = {} end if not eventListeners[name] then eventListeners[name] = {} end
table.insert(eventListeners[name], listener) table.insert(eventListeners[name], listener)
end end
local function dispatchEvents(event, ...) function dispatchEvents(event, ...)
log.info("dispatching event: %s", event) log.info("dispatching event: %s", event)
local eventTbl = eventListeners[event] local eventTbl = eventListeners[event]
@@ -145,7 +145,7 @@ local function dispatchEvents(event, ...)
end end
end end
local function handleMessages(side, channel, replyChannel, message, dist) function handleMessages(side, channel, replyChannel, message, dist)
if channel ~= HOST_CHANNEL then return end if channel ~= HOST_CHANNEL then return end
-- msg validation -- msg validation
@@ -182,6 +182,11 @@ local function handleMessages(side, channel, replyChannel, message, dist)
end end
registerEventListener("modem_message", handleMessages) registerEventListener("modem_message", handleMessages)
function handleKey(key, isHeld)
log.info("key pressed (%d)", key)
end
registerEventListener("key", handleKey)
--[[ --[[
main main
]] ]]
@@ -203,15 +208,23 @@ function updateScreen()
term.write(text) term.write(text)
end end
-- reset state and clear screen
term.setBackgroundColor(colors.black) term.setBackgroundColor(colors.black)
term.clear() term.clear()
-- -- title bar
term.setCursorPos(1, 1) term.setCursorPos(1, 1)
term.setBackgroundColor(colors.lightGray) term.setBackgroundColor(colors.lightGray)
term.setTextColour(colors.black) term.setTextColour(colors.black)
term.clearLine() term.clearLine()
drawTextCentered("tutel host controller") drawTextCentered("tutel host controller")
term.setBackgroundColor(colors.black)
term.setTextColour(colors.white)
-- logs
local function drawLogs(startY, endY) local function drawLogs(startY, endY)
for i = #log.buffer, 1, -1 do for i = #log.buffer, 1, -1 do
if i > endY then break end if i > endY then break end
@@ -219,7 +232,7 @@ function updateScreen()
local entry = log.buffer[i] local entry = log.buffer[i]
local format = logTypes[entry.type] local format = logTypes[entry.type]
term.setCursorPos(0, startY + i) term.setCursorPos(0, startY - 1 + i)
local prevColourBg = term.getBackgroundColour() local prevColourBg = term.getBackgroundColour()
local prevColourFg = term.getTextColour() local prevColourFg = term.getTextColour()