Jump to content




Help me with key detection!

help

  • You cannot reply to this topic
8 replies to this topic

#1 tonkku107

  • Members
  • 140 posts
  • LocationFinland

Posted 04 May 2013 - 04:09 AM

i found this from the wiki:
http://computercraft.../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.

#2 BigSHinyToys

  • Members
  • 1,001 posts

Posted 04 May 2013 - 04: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


#3 tonkku107

  • Members
  • 140 posts
  • LocationFinland

Posted 04 May 2013 - 04:28 AM

View PostBigSHinyToys, on 04 May 2013 - 04:16 AM, said:

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?

#4 BigSHinyToys

  • Members
  • 1,001 posts

Posted 04 May 2013 - 04: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")


#5 tonkku107

  • Members
  • 140 posts
  • LocationFinland

Posted 04 May 2013 - 04:36 AM

View PostBigSHinyToys, on 04 May 2013 - 04:34 AM, said:

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

#6 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 04 May 2013 - 08: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


#7 tonkku107

  • Members
  • 140 posts
  • LocationFinland

Posted 05 May 2013 - 05:05 AM

View PostPharap, on 04 May 2013 - 08:01 AM, said:

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?

#8 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 05 May 2013 - 06:57 AM

View Posttonkku107, on 05 May 2013 - 05:05 AM, said:

View PostPharap, on 04 May 2013 - 08:01 AM, said:

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.

#9 tonkku107

  • Members
  • 140 posts
  • LocationFinland

Posted 05 May 2013 - 07:49 AM

View PostPharap, on 05 May 2013 - 06:57 AM, said:

View Posttonkku107, on 05 May 2013 - 05:05 AM, said:

View PostPharap, on 04 May 2013 - 08:01 AM, said:

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?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users