os.pullEventRaw

From ComputerCraft Wiki
Revision as of 20:36, 24 March 2014 by Oeed (Talk | contribs) (Added os,pullEventRaw page, mainly information about how to prevent termination)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Grid Redstone.png  Function os.pullEventRaw
Waits (yields) until any event occurs (including 'terminate')
Syntax os.pullEventRaw([string filter])
Returns string event, variable parameters(see table below)
Part of ComputerCraft
API OS

Examples

Grid paper.png  Example
The program detects when the user attempted to terminate the program, informs them when they did so then quits.
Code
local running = true
while running do
   print ("Hello. Hold Ctrl + T to terminate (quit) this program.")
   local event = os.pullEventRaw () -- wait for the next event, you could use "terminate" as the filter in this case instead of the if statement
   if event == "terminate" then -- the event is the terminate event, tell the user and stop the loop (let the program end)
       print ("You terminated the program, good bye!")
       running = false
   end
end


Special Uses

Apart from the ability to detect when the user tries to terminate the program, os.pullEventRaw is identical to os.pullEvent. The main use is to prevent the user from quitting your program. This can be useful in programs such as door locks when you don't want the user interfering with the computer.

The quickest and easiest way (especially in pre-made programs) to prevent people from terminating your program is to add the following to the top of your code:

os.pullEvent = os.pullEventRaw

This replaces os.pullEvent with os.pullEventRaw and as such it will catch the 'terminate' event. If you do this, however, it is important to add a method for the user to quit the program (if it is appropriate for the program). If you are using a while loop, add a variable which remains true until you are ready to quit the program. Then, when you want to quit the program simply change the variable to false (as in the above example).

For further reference, see the os.pullEvent page, as almost all their functionality is identical.