Difference between revisions of "Terminate (event)"

From ComputerCraft Wiki
Jump to: navigation, search
(Better description and mentions on how it indirectly calls error within the bios)
(Less confusing example for newbies. Also removed the comment that was implying to override os.pullEvent if you wish to capture this event)
 
Line 1: Line 1:
{{NeedsWork|A demonstration on the use and handling of this event would be beneficial. ''[[User:AfterLifeLochie|AfterLifeLochie]] 16:13, 30 November 2012 (MSK)''}}
 
 
 
{{Event
 
{{Event
 
|name=terminate
 
|name=terminate
|desc=Fired when CTRL+T is held for at least 1 second. With an unmodified bios and os.pullEvent, it indirectly calls error().
+
|desc=Fired when CTRL+T is held for at least 1 second. '''NOTE:''' This event cannot be captured with an unmodified [[os.pullEvent]], [[os.pullEventRaw]] or [[coroutine.yield]] should be used whenever possible instead of overriding os.pullEvent
 
}}
 
}}
 
{{Example
 
{{Example
|desc=The actual terminating part of CTRL+T is defined in the bios for os.pullEvent. This can however be overridden to catch terminate and modify it's effects, as shown below. Note that holding CTRL+T, and it firing the terminate event is independent of key events and is hard coded into ComputerCraft.
+
|desc=This loop listens for the terminate event and exits the program when found, cleaning the screen first
|code=function os.pullEvent(_sFilter)
+
|code=while true do
    local event = { os.pullEventRaw(_sFilter) }
+
  local event = os.pullEventRaw()
    if event[1] == "terminate" then
+
  if event == "terminate" then
      error("Terminate Event Found", 0)
+
    term.clear()
    end
+
    term.setCursorPos(1,1)
    return unpack(event)
+
    return
  end
+
  else
 +
    print("Still running!")
 +
  end
 +
end
 
}}
 
}}

Latest revision as of 09:48, 28 October 2013



Grid Modem.png  Event terminate
Fired when CTRL+T is held for at least 1 second. NOTE: This event cannot be captured with an unmodified os.pullEvent, os.pullEventRaw or coroutine.yield should be used whenever possible instead of overriding os.pullEvent
Returned Object 1 Nothing


Grid paper.png  Example
This loop listens for the terminate event and exits the program when found, cleaning the screen first
Code
while true do
  local event = os.pullEventRaw()
  if event == "terminate" then
    term.clear()
    term.setCursorPos(1,1)
    return
  else
    print("Still running!")
  end
end