os.pullEventRaw
Function os.pullEventRaw | |
Near identical to os.pullEvent(); however, it is capable of returning terminate events (rather than halting your script when encountering them). Note that if a terminate event is encountered, os.pullEventRaw() will return it even if a filter specifying a different type of event is used. | |
Syntax | os.pullEventRaw([string filter]) |
Returns | string event, variable parameters(see table below) |
Part of | ComputerCraft |
API | OS |
Examples
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 returning terminate events instead of actually terminating scripts when encountering them, os.pullEventRaw is identical to os.pullEvent - as such it is generally used to prevent users attempting to exit your code unless it's under your terms. This can be useful in programs such as door locks when you don't want the user interfering with the computer, or when you want to ensure certain "shutdown" operations are performed before your script exits.
The quickest and easiest way to prevent people from terminating (especially pre-made) programs is to add the following to the top of your code:
os.pullEvent = os.pullEventRaw
All references to os.pullEvent from that point on will then point to THIS function, and as such, Ctrl+T (the terminate key combo) won't kill scripts unless they're specifically coded to respond to it. Note that Ctrl+R (the reboot key combo) cannot be "disabled" in such a manner.
If you're intending for the program to eventually exit, it is strongly recommended to re-instate the original function pointer (as it'll otherwise be lost until the computer reboots!). This can be done by storing a copy of it before redirecting os.pullEvent, eg:
local oldPullEvent = os.pullEvent os.pullEvent = os.pullEventRaw
Then, when your script is ready to exit, simply:
os.pullEvent = oldPullEvent