Skip to content

Instantly share code, notes, and snippets.

@Vftdan
Last active August 5, 2025 05:06
Show Gist options
  • Select an option

  • Save Vftdan/543b40c6c06331005a2f05a4a1df691f to your computer and use it in GitHub Desktop.

Select an option

Save Vftdan/543b40c6c06331005a2f05a4a1df691f to your computer and use it in GitHub Desktop.
ComputerCraft event monitor
local selectedEvents = {"key", "key_up", "char", "paste", "mouse_click", "mouse_drag", "mouse_up", "mouse_scroll"}
local allEvents = false
local function repr(x)
local visited = {}
local visitedCount = 0
local function reprInner(x)
visitedCount = visitedCount + 1
local t = type(x)
if t == "string" then
return ("%q"):format(x)
elseif t == "number" or t == "boolean" or t == "nil" then
return tostring(x)
else
local backIdx = visited[x]
if backIdx ~= nil then
return ("old(%d)"):format(x)
end
visited[x] = visitedCount
if t == "table" then
local builder = {"{"}
for k, v in pairs(x) do
table.insert(builder, "[" .. repr(k) .. "]=" .. repr(v) .. ",")
end
table.insert(builder, "}")
return table.concat(builder)
else
return t
end
end
end
return reprInner(x)
end
local args = {...}
local argi = 0
while argi < #args do
argi = argi + 1
local opt = args[argi]
if opt == "-e" or opt == "-event" or opt == "--event" then
allEvents = false
selectedEvents = {}
argi = argi + 1
for m in (args[argi] .. ","):gmatch("(.-)%,") do
table.insert(selectedEvents, m)
end
elseif opt == "-a" or opt == "-all" or opt == "--all" then
allEvents = true
elseif opt == "-h" or opt == "-help" or opt == "--help" then
print("Options:")
print(" -e[vent] <LIST> Set the event types to show. Separated with commas.")
print(" -a[ll] Show all events.")
print(" -h[elp] Show options and exit.")
return
else
error("Unknown option: " .. opt)
end
end
local filterSet = {}
for _, k in ipairs(selectedEvents) do
filterSet[k] = true
end
print("Waiting for events. Kill by holding CTRL-T.")
while true do
local e, v1, v2, v3 = os.pullEvent()
if allEvents or filterSet[e] then
local v1Repr = repr(v1)
if e == "key" or e == "key_up" then
v1Repr = v1Repr .. "(" .. repr(keys.getName(v1)) .. ")"
elseif e == "mouse_click" or e == "mouse_drag" or e == "mouse_up" then
v1Repr = v1Repr .. "(" .. repr(({"left", "right", "middle"})[v1]) .. ")"
elseif e == "mouse_scroll" then
v1Repr = v1Repr .. "(" .. repr(({[-1] = "up", [1] = "down"})[v1]) .. ")"
end
print(e, v1Repr, repr(v2), repr(v3))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment