This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
diegodan1893's profile picture

[Solved] Problem with events

Started by diegodan1893, 07 May 2013 - 10:47 AM
diegodan1893 #1
Posted 07 May 2013 - 12:47 PM
Hi, I'm writting an event handler for keyboard shortcuts but it isn't working and I can't find what is wrong. First, here is the code:


local function eventHandler()
--Do not call this function directly
while true do
  local shortcutTimer
  local shortcutTimeout = 0.4
  local shortcutMode = false
  local event = {os.pullEvent()}
  if event[1] == "mouse_click" then
   --snip
  elseif event[1] == "key" then
   tx.write("key", nil, nil,1,1) ----------------debug
   if event[2] == 29 then --ctrl
	shortcutMode = true
	shortcutTimer = os.startTimer(0.4)
	tx.write("activated shortcut"..tostring(shortcutTimer)..tostring(shortcutMode), nil, nil, 1,2) --------------debug
   elseif shortcutMode then
	tx.write("key + control pressed", nil, nil,1,3) ------------debug
	for i = #InterfaceEntities.list, 1, -1 do
	 local entity = InterfaceEntities.list[i]
	 if entity.shortcut == event[2] and entity.shortcutAction and entity.enabled then
	  tx.write("call action", nil, nil,1,4) ---------------debug
	  entity:shortcutAction()
	 end
	end
   end
  elseif event[1] == "timer" then
   tx.write("timer"..tostring(event[2]), nil, nil, 1,6) -------------------debug
   if event[2] == shortcutTimer then
	tx.write("deactivated shortcut", nil, nil, 1,5) ----------------debug
	shortcutMode = false
   end
  end
end
end
--tx.write is a custom write function

As you can see there are some debug lines. Well the program prints "key" and "activated shortcut" but for some reason it never prints "deactivated shortcut" or "key + control pressed" and it doesn't run that code. I don't know what I'm doing wrong and I have already checked for typos.
It only prints:

key
activated shortcut235true
--after 0.4 seconds
timer 235

Thanks in advance.
remiX #2
Posted 07 May 2013 - 01:26 PM
Try moving
  local shortcutTimer
  local shortcutTimeout = 0.4
  local shortcutMode = false
out of the while loop, above it
diegodan1893 #3
Posted 07 May 2013 - 01:30 PM
*facepalm* Yes, that was the problem, thanks.
It's not the first time I do the same error.