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

Ctrl+T

Started by EurypteriD, 09 February 2012 - 09:11 PM
EurypteriD #1
Posted 09 February 2012 - 10:11 PM
Hey.

ive been looking around both on the net and in my local files.
On how to disabled CTRL+t on a single terminal without install redworks (working on my own OS)

Reason i need this is.. Else you can pass the user logon.
Espen #2
Posted 09 February 2012 - 10:38 PM
CTRL+T throws the 'terminate' error and this stops program execution.
To prevent this from happening you have to catch the error, telling LUA to carry on with your program regardless of the error.
To do this you can make use of the pcall() function ("Protected Call").

So instead of e.g. …

local password = "secret"
repeat
	write("Password: ")	 -- write without line break
	local input = read()	  -- read input
until input == password	-- repeat until the input matches the password
… you could encase the read() function within a pcall() and write this:

local password = "secret"
repeat
	write("Password: ")	 -- write without line break
	local status, input = pcall( read ) 	 -- read input
until input == password	-- repeat until the input matches the password

So as long as read() has not returned, you cannot terminate the input with CTRL+T.
Since nothing else happens outside of the read() in my example I chose to encase just the read() function.
But if you had more going on, then it might be a good idea to encase everything in a new function and then call that whole function via pcall().
Also note the missing () of the read function. That's because we passed the function to pcall() as an argument.

Further links:
http://www.lua.org/pil/8.4.html
http://www.lua.org/m...manual.html#2.7
http://www.lua.org/m....html#pdf-pcall

EDIT: Sorry but I forgot something. When read is called by pcall() then it returns a status value along with the values of the called function.
That means instead of …
local input = pcall( read )
… it should've been …
local status, input = pcall( read )
Otherwise I'm checking the wrong value and even the correctly entered password wouldn't have worked.
Also: status returns true if there wasn't an error during read, and false if there was (like CTRL+T).
In my code I just ignore the status, but you could do different thing depending on its value, of course.

Sorry again, I correct the code above. ;)/>/>
Edited on 09 February 2012 - 10:18 PM
arongy #3
Posted 10 February 2012 - 01:05 AM
You can also add this code into your script:

function os.pullEvent()
	local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
	if event == "terminate" then
		os.reboot() -- Remove this for do not reboot on Ctrl+T
	end
	return event, p1, p2, p3, p4, p5
end

From http://mccraftcpl.pr...splay&thread=78
FuzzyPurp #4
Posted 10 February 2012 - 01:19 AM
Redworks prevents ctrl+t. One could just look at the source of the Redworks API to see how we did that, the code above works just fine.
EurypteriD #5
Posted 10 February 2012 - 05:54 AM
Hey Tanks for the replys.

Ill test both out. But i think the one that turns terminate to shutdown would be the best
As i have put in encrypted debug password so i can get into the terminal even though its messing up.

Im using this trying to replicate a chicken farm i saw on you tube (without Ic as i just use redpower)
its made by Cookiebal, But im gonna write my own just to get into the scripting more.

¨@fuzzy I already looked at the redworks api. But its very complicated in there. Its way to advanced for me at this state XD
FuzzyPurp #6
Posted 10 February 2012 - 06:15 AM
Snippet from Redworks API:

local falt, sEvent, param = nil,nil,nil
while not bExit do
if disableTerm then
falt, sEvent, param = pcall(os.pullEvent)
else
sEvent, param = os.pullEvent()
falt=false
end
EurypteriD #7
Posted 10 February 2012 - 04:05 PM
@arongy
Tried that code. and it worked. Just one note. It have to be in the start else it will mess up

@fuzzy that one didnt work for me. But as long it works for redworks its good ;)/>/>

Thanks both for the replies. Nice to have community like this
Cookiebal #8
Posted 10 February 2012 - 07:57 PM
Another way to stop ctrl+T is to run the program in another program with a loop. The drawback of this is that it resets the program after using ctrl+T.

while true do
shell.run("program")
if programended = true then break end
end

Here the program only ends if somewhere inside the programended is set to true, else it just restarts the program again.
Do note that variables won't get reset.

This is what I use for CookieSystem, there it works pretty good and I don't have to worry about any pcall stuff or other things.
Advert #9
Posted 10 February 2012 - 08:46 PM
Another way to stop ctrl+T is to run the program in another program with a loop. The drawback of this is that it resets the program after using ctrl+T.

while true do
shell.run("program")
if programended = true then break end
end

Here the program only ends if somewhere inside the programended is set to true, else it just restarts the program again.
Do note that variables won't get reset.

This is what I use for CookieSystem, there it works pretty good and I don't have to worry about any pcall stuff or other things.

Why would you have to worry about pcall? D:
Cookiebal #10
Posted 10 February 2012 - 09:51 PM
Why would you have to worry about pcall? D:

Depends on the method you use. I never tried any others myself (never needed to), but I often hear about confusion with pcall :D/>/>
Advert #11
Posted 10 February 2012 - 09:56 PM
Why would you have to worry about pcall? D:

Depends on the method you use. I never tried any others myself (never needed to), but I often hear about confusion with pcall :D/>/>

pcall's syntax is quite simple:

local isSuccessful, returnValue, returnValue2 [...] = pcall(function, arg1, arg2, ...)

if not isSuccessful then
 print("Error: ", returnValue)
end
FuzzyPurp #12
Posted 10 February 2012 - 11:53 PM
@arongy
Tried that code. and it worked. Just one note. It have to be in the start else it will mess up

@fuzzy that one didnt work for me. But as long it works for redworks its good :D/>/>

Thanks both for the replies. Nice to have community like this

Yea it wouldn't work for you because its has redworks variables in it ^_^/>/>

Here's one that would work with a bypass key set to the number 2 key..


while true do
fault, event, param1, param2, param3 = pcall(os.pullEvent)
  if fault == false then
  elseif event == "key" and param1 == 2 then
  break end
end
arongy #13
Posted 13 February 2012 - 01:38 AM
Just put this at the end of your script to reactivate the Terminate command!


function os.pullEvent()
	    local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
	    if event == "terminate" then
			    error( "Terminated" )
	    end
	    return event, p1, p2, p3, p4, p5
end
spyman68 #14
Posted 20 March 2013 - 07:13 AM
Guys, at the very top of your program just type
os.pullEvent = os.pullEventRaw
Cruor #15
Posted 20 March 2013 - 07:16 AM
Please refrain from using the foul art of necromancy.
Locked.