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

[Lua] Customized Termination?

Started by DemonAssassin, 05 March 2012 - 09:29 PM
DemonAssassin #1
Posted 05 March 2012 - 10:29 PM
As a forgetful person, when I make doors with passwords on a SMP server I usually screw up somewhere. This may not mean it results in error, but something like the timing of a door being open, or forgetting the password. Rather then removing the surrounding area and putting down a disk drive, how would you go about creating a customized termination for programs? Something OTHER then Ctrl + T so that random people don't access it.
Advert #2
Posted 05 March 2012 - 10:47 PM
There isn't an easy way of doing this, and even when you do this, if people find out about this, they'll gain access to your computer.

One way you could do this would be to have the computer check for a disk on the front side:
if the disk contains 'secretfile' with the contents '<random contents>, or something else', then unlock the terminal.

To use it, you'd turn the computer on, then place a disk drive on the screen side (front), then put in your disk, and destroy the disk drive. Your computer should then be unlocked.

You could alternatively check for certain keys being pressed (in order), such as 'Delete', 'ctrl', …
DemonAssassin #3
Posted 05 March 2012 - 11:08 PM
That last suggestion is what I was aming for. I've been to google and the CC Wiki, no help in either of those places other then say "Yes it's possible"
MysticT #4
Posted 05 March 2012 - 11:25 PM
Yes, it's possible. :unsure:/>/>
It should be easy to implement, you just have to save all the keys that are pressed in a table (or a string if it's just text), and check if it's correct, if it's not delete all.
You may have to use a custom read() function, but it's just copying the read() code and adding a few lines.
If I have time I'll post some example latter.
Advert #5
Posted 05 March 2012 - 11:27 PM
Take a look at the definition of read() in bios.lua; unless you're using os.pullEvent() and keeping track of the password yourself.

You'll need to check the 'key' event to see if a special code is being used, then keep track of it, and unlock if it's right.
Espen #6
Posted 06 March 2012 - 12:46 AM
That's an interesting idea. It's pretty straightforward, here's some simple example:

local tSecretKeySequence = { 29, 28, 14 }  -- LEFT-CTRL, ENTER, BACKSPACE
local nCurrentIndexPos = 1

while true do
  local event, key = os.pullEvent("key")

  if key == tSecretKeySequence[nCurrentIndexPos] then
    nCurrentIndexPos = nCurrentIndexPos + 1  -- Correct key, advance index for next key-check.
  else
    nCurrentIndexPos = 1  -- Reset index position.
  end

  if nCurrentIndexPos > #tSecretKeySequence then break end   -- If we have successfully run through all keys, then break out of the loop (or whatever).
end
The code basically runs the loop until you entered the correct key sequence.
In this case: LEFT-CTRL, then ENTER and finally BACKSPACE.
If you ever get out of sequence, then it resets and you have to begin anew.

Of course you'd pack this into a function and choose more/different keys.
Here's also a helpful code-table: http://www.minecraftwiki.net/wiki/Key_Codes
coolblockj #7
Posted 06 March 2012 - 03:18 AM
Of course you'd pack this into a function and choose more/different keys.
Here's also a helpful code-table: http://www.minecraft.&#46;&#46;/wiki/Key_Codes
Or you could use:

print (os.pullEvent())
then type a key