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

Help me with key detection!

Started by tonkku107, 04 May 2013 - 03:09 AM
tonkku107 #1
Posted 04 May 2013 - 05:09 AM
i found this from the wiki:
http://computercraft.info/wiki/Raw_key_events

function rawread()
    while true do
	    local sEvent, param = os.pullEvent("key")
	    if sEvent == "key" then
		    if param == 54 then
			    print("Secret Button Detected!")
			    break
		    end
	    end
    end
end
i have also

io.read()
to detect for enter. Should i use else and then the R SHIFT code, or what?
I have tried it almost in every place!
Tell me if you want to see my code on the program.
BigSHinyToys #2
Posted 04 May 2013 - 05:16 AM
Run this code then press buttons you will see what events occur when the buttons are pressed also redstone peripheral and other events.

while true do
    local event = {os.pullEvent()} -- saves event data in table
    for i = 1,#event do -- writes data to scren
	    write(tostring(event[i]).." ")
    end -- end for loop
    print("") -- makes a new line
end -- end of while loop
tonkku107 #3
Posted 04 May 2013 - 05:28 AM
Run this code then press buttons you will see what events occur when the buttons are pressed also redstone peripheral and other events.

while true do
	local event = {os.pullEvent()} -- saves event data in table
	for i = 1,#event do -- writes data to scren
		write(tostring(event[i]).." ")
	end -- end for loop
	print("") -- makes a new line
end -- end of while loop
I have a thing that when you open the computer, this appears:
PassLock v.2.2

Press enter to open the door…

Then i want it to wait for enter, and R SHIFT.
How would that work then?
BigSHinyToys #4
Posted 04 May 2013 - 05:34 AM
that code tels you what the key codes are for the buttons you want you would then do something like


while true do
local event,nKey = os.pullEvent()
if event == "key" then
  if nKey == 28 then -- key 28 is enter
   break
  end
end
end
print("Hello world")
tonkku107 #5
Posted 04 May 2013 - 05:36 AM
that code tels you what the key codes are for the buttons you want you would then do something like


while true do
local event,nKey = os.pullEvent()
if event == "key" then
  if nKey == 28 then -- key 28 is enter
   break
  end
end
end
print("Hello world")
Thanks! Haven't tested this yet but i'm going to when i get back home
Pharap #6
Posted 04 May 2013 - 09:01 AM
There's a keys table that stores the numbers of all the keys. It loads up by default with the OS so instead of doing if param == 28 you can do if param == keys.enter. It should be in the api folder if you dig around the CC mod jar lua section.

There's no way to test two key presses at a time because of the way the system works, but you can test for keys being pressed in an order, for example:

function rawread()
while true do
local event, param = os.pullEventRaw()
-- catches terminate so people cant ctrl-t your program
if  event == 'key' and param == keys.shift then
local event, param = os.pullEventRaw()
if event == 'key' and param == keys.enter then
print("Secret Button Combo Detected!")
break
end
elseif event == 'terminate' then
print("Nice Try")
end
end
end
tonkku107 #7
Posted 05 May 2013 - 06:05 AM
There's a keys table that stores the numbers of all the keys. It loads up by default with the OS so instead of doing if param == 28 you can do if param == keys.enter. It should be in the api folder if you dig around the CC mod jar lua section.

There's no way to test two key presses at a time because of the way the system works, but you can test for keys being pressed in an order, for example:

function rawread()
while true do
local event, param = os.pullEventRaw()
-- catches terminate so people cant ctrl-t your program
if  event == 'key' and param == keys.shift then
local event, param = os.pullEventRaw()
if event == 'key' and param == keys.enter then
print("Secret Button Combo Detected!")
break
end
elseif event == 'terminate' then
print("Nice Try")
end
end
end
How can i then open the door with enter and then get to the command place with a secret key or a secret button combo?
Pharap #8
Posted 05 May 2013 - 07:57 AM
There's a keys table that stores the numbers of all the keys. It loads up by default with the OS so instead of doing if param == 28 you can do if param == keys.enter. It should be in the api folder if you dig around the CC mod jar lua section.

There's no way to test two key presses at a time because of the way the system works, but you can test for keys being pressed in an order, for example:

function rawread()
while true do
local event, param = os.pullEventRaw()
-- catches terminate so people cant ctrl-t your program
if  event == 'key' and param == keys.shift then
local event, param = os.pullEventRaw()
if event == 'key' and param == keys.enter then
print("Secret Button Combo Detected!")
break
end
elseif event == 'terminate' then
print("Nice Try")
end
end
end
How can i then open the door with enter and then get to the command place with a secret key or a secret button combo?
Use the redstone API for opening doors:
http://computercraft.info/wiki/index.php?title=Redstone_(API)
Just substitute the print calls for calls to change the redstone output and make sure the computer can output to the door.

I recommend placing the door so it blocks people when it's powered as opposed to opening when powered, otherwise people can just use levers to get through.
tonkku107 #9
Posted 05 May 2013 - 08:49 AM
There's a keys table that stores the numbers of all the keys. It loads up by default with the OS so instead of doing if param == 28 you can do if param == keys.enter. It should be in the api folder if you dig around the CC mod jar lua section.

There's no way to test two key presses at a time because of the way the system works, but you can test for keys being pressed in an order, for example:

function rawread()
while true do
local event, param = os.pullEventRaw()
-- catches terminate so people cant ctrl-t your program
if  event == 'key' and param == keys.shift then
local event, param = os.pullEventRaw()
if event == 'key' and param == keys.enter then
print("Secret Button Combo Detected!")
break
end
elseif event == 'terminate' then
print("Nice Try")
end
end
end
How can i then open the door with enter and then get to the command place with a secret key or a secret button combo?
Use the redstone API for opening doors:
http://computercraft...=Redstone_(API)
Just substitute the print calls for calls to change the redstone output and make sure the computer can output to the door.

I recommend placing the door so it blocks people when it's powered as opposed to opening when powered, otherwise people can just use levers to get through.
yes yes yes!

rs.setOutput("back",true)
can i read both with else or something?