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

[Question] Differentiate between T and Ctrl+T?

Started by my_hat_stinks, 14 May 2012 - 08:56 PM
my_hat_stinks #1
Posted 14 May 2012 - 10:56 PM
Alright, I'm trying to override the default os.pullEvent function, so I can restrict Ctrl+T to people with the Admin password

Here's my debug code so far:
local OldEvent = os.pullEvent
local Admin = "pass"

os.pullEvent = function()
  local e,p1,p2,p3 = OldEvent()
  write("\n"..e.."-"..p1)

  if p2 then
	write("-"..p2)
  end
  return e,p1,p2,p3
end

It seems, however, that both T and Ctrl+T give "key-20", so I can't actually differentiate between someone typing normally and someone trying to terminate the program


I could check for the ctrl key then the t key, but there's no way to detect the release of a key (as far as I can see)
I can't check for char-t then ignore the next key-20, because char-t looks like it shows up after key-20

So, my question is this: Is there any way to differentiate between a user hitting T and hitting Ctrl+T? I'd prefer a method using os.pullEvent, since that's what I'm using atm
MysticT #2
Posted 14 May 2012 - 11:09 PM
When you hold Ctrl-T for some time (I think it was 1 sec), the computer sends the "terminate" event, wich, when received by os.pullEvent, throws an error that terminates the current program. So instead of checking for the keys, check for that event without using os.pullEvent.

os.pullEvent = function(filter)
  local evt, p1, p2, p3, p4, p5 = os.pullEventRaw() -- os.pullEventRaw() can be replaced with coroutine.yield(), wich is what the function does
  if evt == "terminate" then
	if isAdmin() then -- replace this with the check for admins you use
	  error("Terminated")
	end
  end
  return evt, p1, p2, p3, p4, p5
end
This is the os.pullEvent code, just add some lines to check for admins like you want.
my_hat_stinks #3
Posted 14 May 2012 - 11:58 PM
Thanks, that worked perfectly