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

Cheat code function

Started by Cranium, 19 January 2013 - 04:44 PM
Cranium #1
Posted 19 January 2013 - 05:44 PM
I created a cheat code function that returns true if you enter a sequence of keystrokes. I think it would be fun to see this pop up in random programs in the future. *hint hint*
Spoiler

local function checkCode()
local kCode = {200, 200, 208, 208, 203, 205, 203, 205, 48, 30, 28}
local code = false
local index = 1
while true do
  local events = {os.pullEvent()}
  if events[1] == "key" then
   if events[2] == kCode[index] then
	if index == #kCode then
	 code = true
	 break
	else
	 code = false
	 index = index + 1
	end
   else
	code = false
	os.queueEvent(events[1], events[2], events[3], events[4])
	index = 1
	break
   end
  else
   code = false
   os.queueEvent(events[1], events[2], events[3], events[4])
   break
  end
end
return code
end
You can have it run secretly in the background, and if it returns false, it queues up the events, so you can't mess up a menu. All you have to do is change the kCode table to include the keycode sequence you want to check for. By default it includes the famous Konami Code.
For those who don't know, the Konami Code is up, up, down, down, left, right, left, right, b, a, enter(it's actually start, but I had to make do).
Eric #2
Posted 20 January 2013 - 04:03 AM
Here's a far shorter and more readable (IMHO) version:


local function checkCode()
  local kCodes = {200, 200, 208, 208, 203, 205, 203, 205, 48, 30, 28}
  for _, kCode in ipairs(kCodes) do
    local events = {os.pullEvent('key')}
    if events[2] ~= kCode then
      os.queueEvent(unpack(events))
      return false
    end
  end
  return true
end
Edited on 20 January 2013 - 03:07 AM
NeverCast #3
Posted 21 January 2013 - 04:04 PM
Very nice Cranium!

Also Eric, while I agree that unpack should be used, your code doesn't cancel the entire sequence if someone types in a wrong character? Also it seems that keys are cancelled if they are the right sequence, which would be if the first part of the code was A, then you would never be able to send an A without double tapping it.
Eric #4
Posted 22 January 2013 - 11:04 AM
Also Eric, while I agree that unpack should be used, your code doesn't cancel the entire sequence if someone types in a wrong character?
Yes it does? if events[2] ~= kCode then … return false end
Also it seems that keys are cancelled if they are the right sequence, which would be if the first part of the code was A, then you would never be able to send an A without double tapping it.
That's a flaw in the original as well, I think.
Cranium #5
Posted 22 January 2013 - 11:12 AM
That's true. No matter what, unless you are doing coroutines, it will throw away the first event if it is the correct one. I'm not sure how I would implement that without destroying the whole concept though.
NeverCast #6
Posted 22 January 2013 - 05:51 PM
Sorry Eric, I guess I didn't look at your code long enough.

Cranium, Keep the index, but pass the event on anyway.