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

Key press Help Required

Started by Deathknight0897, 04 July 2012 - 08:59 AM
Deathknight0897 #1
Posted 04 July 2012 - 10:59 AM
Ok trying to-do something real simple and its not working i have a if statement but its not working no error returned now i think its to-do with the os.queueEvent("Key",w)


os.pullEvent("redstone")
if rs.getBundledInput(bottom) == 2048 then
os.queueEvent("key",W)
end
Mtdj2 #2
Posted 04 July 2012 - 11:35 AM
I think you need an "end", or maybe you need to change the 2048 to the color.
Hope this helped.
Deathknight0897 #3
Posted 04 July 2012 - 11:40 AM
ah sorry i have an end didn't paste that in as that was irrelevant no it runs fine the main issue is the key isn't working i believe
Kolpa #4
Posted 04 July 2012 - 11:51 AM
Ok trying to-do something real simple and its not working i have a if statement but its not working no error returned now i think its to-do with the os.queueEvent("Key",w)


os.pullEvent("redstone")
if rs.getBundledInput(bottom) == 2048 then
os.queueEvent("key",W)
end
try putting a print after the if to check if thats the error

os.pullEvent("redstone")
if rs.getBundledInput(bottom) == 2048 then
print("debug")
os.queueEvent("key",W)
end
Deathknight0897 #5
Posted 04 July 2012 - 12:02 PM
Ok trying to-do something real simple and its not working i have a if statement but its not working no error returned now i think its to-do with the os.queueEvent("Key",w)


os.pullEvent("redstone")
if rs.getBundledInput(bottom) == 2048 then
os.queueEvent("key",W)
end
try putting a print after the if to check if thats the error

os.pullEvent("redstone")
if rs.getBundledInput(bottom) == 2048 then
print("debug")
os.queueEvent("key",W)
end
ok so just debugged the hell out of it and im not even getting the signal checked my wiring and its fine the code looks fine so really stumped now any suggestions
Kolpa #6
Posted 04 July 2012 - 12:31 PM
Ok trying to-do something real simple and its not working i have a if statement but its not working no error returned now i think its to-do with the os.queueEvent("Key",w)


os.pullEvent("redstone")
if rs.getBundledInput(bottom) == 2048 then
os.queueEvent("key",W)
end
try putting a print after the if to check if thats the error

os.pullEvent("redstone")
if rs.getBundledInput(bottom) == 2048 then
print("debug")
os.queueEvent("key",W)
end
ok so just debugged the hell out of it and im not even getting the signal checked my wiring and its fine the code looks fine so really stumped now any suggestions
oh shiz i just noticed the problem

os.pullEvent("redstone")
if rs.getBundledInput("bottom") == 2048 then --better not forget quotes
print("debug")
os.queueEvent("key",W)
end
Deathknight0897 #7
Posted 04 July 2012 - 12:37 PM
Ok trying to-do something real simple and its not working i have a if statement but its not working no error returned now i think its to-do with the os.queueEvent("Key",w)


os.pullEvent("redstone")
if rs.getBundledInput(bottom) == 2048 then
os.queueEvent("key",W)
end
try putting a print after the if to check if thats the error

os.pullEvent("redstone")
if rs.getBundledInput(bottom) == 2048 then
print("debug")
os.queueEvent("key",W)
end
ok so just debugged the hell out of it and im not even getting the signal checked my wiring and its fine the code looks fine so really stumped now any suggestions
oh shiz i just noticed the problem

os.pullEvent("redstone")
if rs.getBundledInput("bottom") == 2048 then --better not forget quotes
print("debug")
os.queueEvent("key",W)
end
cant test right now but hope your right will test later and respond
Cloudy #8
Posted 04 July 2012 - 12:46 PM
Ok trying to-do something real simple and its not working i have a if statement but its not working no error returned now i think its to-do with the os.queueEvent("Key",w)


os.pullEvent("redstone")
if rs.getBundledInput(bottom) == 2048 then
os.queueEvent("key",W)
end

I can't believe nobody noticed the obvious. Here is fixed code, annotated with comments.

os.pullEvent("redstone")
if rs.getBundledInput("bottom") == 2048 then
os.queueEvent("key",17) -- key code of w is 17 - you're passing nil as a param by just using W
os.queueEvent("char","w") -- You'll also want to queue the char "w" also to cover any usage scenario.
end
Kolpa #9
Posted 04 July 2012 - 12:50 PM
Ok trying to-do something real simple and its not working i have a if statement but its not working no error returned now i think its to-do with the os.queueEvent("Key",w)


os.pullEvent("redstone")
if rs.getBundledInput(bottom) == 2048 then
os.queueEvent("key",W)
end

I can't believe nobody noticed the obvious. Here is fixed code, annotated with comments.



os.pullEvent("redstone")
if rs.getBundledInput("bottom") == 2048 then
os.queueEvent("key",17) -- key code of w is 17 - you're passing nil as a param by just using W
os.queueEvent("char","w") -- You'll also want to queue the char "w" also to cover any usage scenario.
end
hey i did i just never used queue event i thought u can use it like that :/
Deathknight0897 #10
Posted 05 July 2012 - 05:33 AM
Ok trying to-do something real simple and its not working i have a if statement but its not working no error returned now i think its to-do with the os.queueEvent("Key",w)


os.pullEvent("redstone")
if rs.getBundledInput(bottom) == 2048 then
os.queueEvent("key",W)
end

I can't believe nobody noticed the obvious. Here is fixed code, annotated with comments.

os.pullEvent("redstone")
if rs.getBundledInput("bottom") == 2048 then
os.queueEvent("key",17) -- key code of w is 17 - you're passing nil as a param by just using W
os.queueEvent("char","w") -- You'll also want to queue the char "w" also to cover any usage scenario.
end
thanks very much cloudy trying it now new to this whole coding malarkey but getting on with it well my day job is a graphical artist so this a real step on the while side for me :P/>/>
Deathknight0897 #11
Posted 05 July 2012 - 09:25 AM
Ok so i have redone alot of the code and its still not working could someone have a look and see if any obvious error is screaming out here to why this is refusing to work


local function Control()
-- repeat so function continues
repeat
-- inputs (using bundled cables/pressure plates)
-- green=up, red=left, orange=down, yellow=right,
-- grey=enter
-- white=restart
-- black= shutdown
if rs.testBundledInput("bottom", 8192 ) then--green
  os.queueEvent("key", 200) --up
elseif rs.testBundledInput("bottom", 16384) then--red
  os.queueEvent("key", 203) --left
elseif rs.testBundledInput("bottom", 2) then--orange
  os.queueEvent("key", 205)--down
elseif rs.testBundledInput("bottom", 16) then--yellow
  os.queueEvent("key", 208)--right
elseif rs.testBundledInput("bottom", 128) then--grey
  os.queueEvent("key", 28)--enter
elseif rs.testBundledInput("bottom", 1) then--White
  os.queueEvent("os.reboot()")--restart
elseif rs.testBundledInput("bottom", 32768) then--Black
  os.queueEvent("os.shutdown()")--shutdown
end
-- sleep so parallels doesn't get flooded and error
sleep(0.2)
until a == 1
end
KaoS #12
Posted 05 July 2012 - 01:06 PM
give the variable 'a' a value

try enclosing all of the numbers in os.queueEvent("key",number) with quotes

and why are you calling the event os.reboot() and os.shutdown() thay are not events they are just codes, so try


local function Control()
local a=0
repeat
if rs.testBundledInput("bottom", 8192 ) then
  os.queueEvent("key", "200")
elseif rs.testBundledInput("bottom", 16384) then
  os.queueEvent("key", "203")
elseif rs.testBundledInput("bottom", 2) then
  os.queueEvent("key", "205")
elseif rs.testBundledInput("bottom", 16) then
  os.queueEvent("key", "208")
elseif rs.testBundledInput("bottom", 128) then
  os.queueEvent("key", "28")
elseif rs.testBundledInput("bottom", 1) then
  os.reboot()
elseif rs.testBundledInput("bottom", 32768) then
  os.shutdown()
end
sleep(0.2)
until a == 1
end

maybe I'm wrong about the quotes around the numbers so try without them as well. I probably have a few errors in there as well, I am not the greatest at coding but give it a try
Deathknight0897 #13
Posted 05 July 2012 - 01:23 PM
i am pretty sure that code doesn't work it just spams errors sorry
KaoS #14
Posted 05 July 2012 - 01:30 PM
and without the quotes on all of the numbers?
Deathknight0897 #15
Posted 05 July 2012 - 01:32 PM
same sadly its really frustrating that this wont work and what even worse it doesn't even report back an error
and without the quotes on all of the numbers?
Pinkishu #16
Posted 05 July 2012 - 02:10 PM
Well not sure when a becomes 1 but until then once you call the function it'll just keep adding events to the queue, doing nothing else in your code Oo
KaoS #17
Posted 05 July 2012 - 02:10 PM
ok, time to do this in my format. try this one


local function control()
local function qu(number)
os.queueEvent("key",number)
end
local function chk(colors)
return rs.testBundledInput("bottom",colors)
end
while true do
if chk(colors.green) then
qu(200)
elseif chk(colors.red) then
qu(203)
elseif chk(colors.orange) then
qu(205)
elseif chk(colors.yellow) then
qu(208)
elseif chk(colors.grey) then
qu(28)
elseif chk(colors.white) then
os.reboot()
elseif chk(colors.black) then
os.shutdown()
end
sleep(0.5)
end
end

I can summarise it further if you like but I am at work at the moment with a pissed off client:) give that a try
KaoS #18
Posted 05 July 2012 - 02:16 PM
Well not sure when a becomes 1 but until then once you call the function it'll just keep adding events to the queue, doing nothing else in your code Oo

yes, that is the point, we are sending in the event of pressing a key, thus if you input the code os.queueEvent("key",200) the system thinks you pressed key 200
Pinkishu #19
Posted 05 July 2012 - 02:18 PM
Yeah but no code will ever process that queue'd event until a becomes 1 cause until then it just keeps looping and queueing doing nothing else
KaoS #20
Posted 05 July 2012 - 02:22 PM
you can use the corouteine api to run this in the background of anouther program. you can run worm with this in the background and use bundled cable inputs to control the worm, take a look at this thread http://www.computercraft.info/forums2/index.php?/topic/1996-questionlua/

lol. that was horrible spelling, sorry
KaoS #21
Posted 05 July 2012 - 02:24 PM
but either way, I have no clue what he is using it for, I just want to get it working for him. then maybe he can tell us how he wants to integrate it
Pinkishu #22
Posted 05 July 2012 - 02:27 PM
Yeah he could be using that, i doubt he does kinda
and that sleep(0.2) will likely eat all your nicely queue'd up events
KaoS #23
Posted 05 July 2012 - 02:32 PM
and that sleep(0.2) will likely eat all your nicely queue'd up events

true, should build an alternative function for the delay

or sleep before creating the event, not after
Deathknight0897 #24
Posted 05 July 2012 - 03:00 PM
Ok so i tried your code from earlier same problem as my code i think the lady may be right about it staying in a queue but never getting release to the rest of the program how would i do that
KaoS #25
Posted 05 July 2012 - 03:03 PM
How are you planning on using this code? then I can write something to do that
Deathknight0897 #26
Posted 05 July 2012 - 03:08 PM
basically i have a menu u know what ill send u what i got in a pm for obvious reasons dont want to post it until im finished will be open source once done however
Pinkishu #27
Posted 05 July 2012 - 03:10 PM
you could just use a loop with pullEvent
MysticT #28
Posted 05 July 2012 - 03:22 PM
Do you realise that you never call the function? So the program won't do anything.
The function:

local function control()
  while true do
	os.pullEvent("redstone") -- wait for redstone input
	if rs.testBundledInput("bottom", 8192) then -- you should use colors.<colorName> instead, so it's more readable
	  os.queueEvent("key", 200)
	elseif rs.testBundledInput("bottom", 16384) then
	  os.queueEvent("key", 203)
	elseif rs.testBundledInput("bottom", 2) then
	  os.queueEvent("key", 205)
	elseif rs.testBundledInput("bottom", 16) then
	  os.queueEvent("key", 208)
	elseif rs.testBundledInput("bottom", 128) then
	  os.queueEvent("key", 28)
	elseif rs.testBundledInput("bottom", 1) then
	  os.reboot()
	elseif rs.testBundledInput("bottom", 32768) then
	  os.shutdown()
  end
end
And now to actually run this, you need to call that function:

control()
But if you'r trying to run it on the background, you need to use parallel:

parallel.waitForAny(control, theOtherFunction)
So it will run both functions, and when it receives a redstone event it checks the input and queues the corresponding key.
Example:

parallel.waitForAny(control, function() shell.run("shell") end)
That would run the shell and the control function at the same time, so you can control the computer with redstone (just the keys you added in the function).

EDIT:
I just noticed you just copied the code from here, and you did it wrong, cause you left some parts out (the part that actually runs the program).
Kolpa #29
Posted 05 July 2012 - 09:13 PM
im confused why are people using repeats for infinite loops o.O thats stupid and anyways why use elseif if u dont need to

local function Control()
local a=0
while true do
if rs.testBundledInput("bottom", 8192 ) then
  os.queueEvent("key", "200")
end
if rs.testBundledInput("bottom", 16384) then
  os.queueEvent("key", "203")
end
if rs.testBundledInput("bottom", 2) then
  os.queueEvent("key", "205")
end
if rs.testBundledInput("bottom", 16) then
  os.queueEvent("key", "208")
end
if rs.testBundledInput("bottom", 128) then
  os.queueEvent("key", "28")
end
if rs.testBundledInput("bottom", 1) then
  os.reboot()
end
if rs.testBundledInput("bottom", 32768) then
  os.shutdown()
end
sleep(0.2)
end
end
Pinkishu #30
Posted 06 July 2012 - 01:49 AM
im confused why are people using repeats for infinite loops o.O thats stupid and anyways why use elseif if u dont need to

i'm confused why people loop over redstone test with a sleep()
just wait for a redstone event.. seriously


And yes in this case If would probably be better as more than 1 color can turn on at once
MysticT #31
Posted 06 July 2012 - 01:55 AM
You'r right, the elseif's wouldn't allow multiple inputs.
It should be:

local function control()
  while true do
	os.pullEvent("redstone") -- wait for redstone input
	if rs.testBundledInput("bottom", 8192) then
	  os.queueEvent("key", 200)
    end
    if rs.testBundledInput("bottom", 16384) then
	  os.queueEvent("key", 203)
    end
    if rs.testBundledInput("bottom", 2) then
	  os.queueEvent("key", 205)
    end
    if rs.testBundledInput("bottom", 16) then
	  os.queueEvent("key", 208)
    end
    if rs.testBundledInput("bottom", 128) then
	  os.queueEvent("key", 28)
    end
    if rs.testBundledInput("bottom", 1) then
	  os.reboot()
    end
    if rs.testBundledInput("bottom", 32768) then
	  os.shutdown()
    end
  end
end
Kolpa #32
Posted 06 July 2012 - 10:02 AM
well im here for probably 3 hours redownloading tf2 :/ damm i hate my internet so just ask
Deathknight0897 #33
Posted 06 July 2012 - 10:30 AM
ok mystic t i will try tht im going away for the weekend with my girlfriend so will be back Monday and try that out
KaoS #34
Posted 08 July 2012 - 06:29 AM
Give this a try


local function check(color)
return rs.testBundledInput("back",color)
end
local function control(main,one,two,three,four,select)
while true do
  os.pullevent("redstone")
  if check(colors.white) then
   main(one)
  end
  if check(colors.orange) then
   main(two)
  end
  if check(colors.magenta) then
   main(three)
  end
  if check(colors.lightBlue) then
   main(four)
  end
  if check(colors.black) then
   return main(select)
  end
end
end

then if you call control(myfunctionname,"up","right","back","left","select")

every time it gets a change in redstone input it checks again and calls myfunctionname with the text string in the 2-6th parameter as a parameter to it (eg. if you give it white input it will call myfunctionname("up"))

PLEASE NOTE THAT THE FUNCTION NAME SHOULD NOT BE IN INVERTED COMMAS, IT IS NOT A STRING!!

I used this to create a good menu, you just make myfunctionname handle the menu, it automatically stops looping when you select a menu item and it actually returns the output of myfunctionname to the selecting command (that way you can make myfunctionname always return true if it succeeds in selecting something and you can then see if it failed by using the control function in a line like this 'if control(myfunctionname,"up","right","back","left")==false then print("ERROR!!!") return end')

you can use it for multiple menus as you just change myfunctionname

you can adapt that in a few ways, you can make it accept a table as a last parameter and make it automatically compile the menu from that or rather than using one function make it call a separate one each time etc etc etc

if there are any problems with it let me know, I have tested it
KaoS #35
Posted 08 July 2012 - 06:37 AM
another thing to keep in mind. using pressure plates with this is not such a great idea as while you step on the second one the first one is still on so it gets called again (because it got a redstone event and looped again while it was still on)

you can add functionality to store the previous input and ignore it the next time if you like but consider yourself warned, that's a whole other story with hidden problems 8o

@Deathknight0897 If you would like me to adapt it for your purpose let me know, a pm would be fine