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

help please

Started by chriskopp7, 13 June 2012 - 11:12 PM
chriskopp7 #1
Posted 14 June 2012 - 01:12 AM
so i want this to do something once a push the a key on my keyboard and the code i have.

here is my code


print("push 1")
local event, p1 = os.pullEvent()
if event == "key" and p1 == "30" then
print("HI")
MysticT #2
Posted 14 June 2012 - 01:17 AM
The argument of the "key" event is a number, not a string. So it should be:

print("push 1")
local event, p1 = os.pullEvent()
if event == "key" and p1 == 30 then -- removed the quotes from the number
  print("hi")
end
Also, here's a list of the key codes, cause the code for 1 is not 30 is 2.
chriskopp7 #3
Posted 14 June 2012 - 02:18 AM
ik i
The argument of the "key" event is a number, not a string. So it should be:

print("push 1")
local event, p1 = os.pullEvent()
if event == "key" and p1 == 30 then -- removed the quotes from the number
  print("hi")
end
Also, here's a list of the key codes, cause the code for 1 is not 30 is 2.
ik i change it to tets a different one and still did not work xD
chriskopp7 #4
Posted 14 June 2012 - 06:59 AM
oh here is what i wanted it for

term.setCursorPos(1,1)
term.clear()
print("push 1-6")
function main()
local event, p1 = os.pullEvent()
if event == "key" and p1 == 2 then – key 1
rs.setBundledOutput("bottom", colors.white)
sleep(1.0)
rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.white)
sleep(0.20)
shell.run("next")
end – key 1
if event == "key" and p1 == 3 then
rs.setBundledOutput("bottom", colors.orange)
sleep(1.0)
rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.orange)
sleep(0.20)
shell.run("next")
end – key 2
if event == "key" and p1 == 4 then
rs.setBundledOutput("bottom", colors.green)
sleep(1.0)
rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.green)
sleep(0.20)
shell.run("next")
end – key 3
if event == "key" and p1 == 5 then
rs.setBundledOutput("bottom", colors.brown)
sleep(1.0)
rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.brown)
sleep(0.20)
shell.run("next")
end – key 4
if event == "key" and p1 == 6 then
rs.setBundledOutput("bottom", colors.black)
sleep(1.0)
rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.black)
sleep(0.20)
shell.run("next")
end – key 5
if event == "key" and p1 == 7 then
rs.setBundledOutput("bottom", colors.blue)
sleep(1.0)
rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.blue)
sleep(0.20)
shell.run("next")
end – key 6
end
main()
BigSHinyToys #5
Posted 14 June 2012 - 09:18 AM
Fixed use char in place of key
Spoiler

term.setCursorPos(1,1)
term.clear()
print("push 1-6")
function main()
    local event, p1 = os.pullEvent()
    if event == "char" and p1 == "2" then -- key 1
	    rs.setBundledOutput("bottom", colors.white)
	    sleep(1.0)
	    rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.white)
	    sleep(0.20)
	    shell.run("next")
    end -- key 1
    if event == "char" and p1 == "3" then
	    rs.setBundledOutput("bottom", colors.orange)
	    sleep(1.0)
	    rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.orange)
	    sleep(0.20)
	    shell.run("next")
    end -- key 2
    if event == "char" and p1 == "4"then
	    rs.setBundledOutput("bottom", colors.green)
	    sleep(1.0)
	    rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.green)
	    sleep(0.20)
	    shell.run("next")
    end -- key 3
    if event == "char" and p1 == "5" then
	    rs.setBundledOutput("bottom", colors.brown)
	    sleep(1.0)
	    rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.brown)
	    sleep(0.20)
	    shell.run("next")
    end -- key 4
    if event == "char" and p1 == "6" then
	    rs.setBundledOutput("bottom", colors.black)
	    sleep(1.0)
	    rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.black)
	    sleep(0.20)
	    shell.run("next")
    end -- key 5
    if event == "char" and p1 == "7" then
	    rs.setBundledOutput("bottom", colors.blue)
	    sleep(1.0)
	    rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.blue)
	    sleep(0.20)
	    shell.run("next")
    end -- key 6
end
main()
chriskopp7 #6
Posted 14 June 2012 - 06:31 PM
Fixed use char in place of key
Spoiler

term.setCursorPos(1,1)
term.clear()
print("push 1-6")
function main()
	local event, p1 = os.pullEvent()
	if event == "char" and p1 == "2" then -- key 1
		rs.setBundledOutput("bottom", colors.white)
		sleep(1.0)
		rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.white)
		sleep(0.20)
		shell.run("next")
	end -- key 1
	if event == "char" and p1 == "3" then
		rs.setBundledOutput("bottom", colors.orange)
		sleep(1.0)
		rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.orange)
		sleep(0.20)
		shell.run("next")
	end -- key 2
	if event == "char" and p1 == "4"then
		rs.setBundledOutput("bottom", colors.green)
		sleep(1.0)
		rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.green)
		sleep(0.20)
		shell.run("next")
	end -- key 3
	if event == "char" and p1 == "5" then
		rs.setBundledOutput("bottom", colors.brown)
		sleep(1.0)
		rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.brown)
		sleep(0.20)
		shell.run("next")
	end -- key 4
	if event == "char" and p1 == "6" then
		rs.setBundledOutput("bottom", colors.black)
		sleep(1.0)
		rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.black)
		sleep(0.20)
		shell.run("next")
	end -- key 5
	if event == "char" and p1 == "7" then
		rs.setBundledOutput("bottom", colors.blue)
		sleep(1.0)
		rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.blue)
		sleep(0.20)
		shell.run("next")
	end -- key 6
end
main()
both work
BigSHinyToys #7
Posted 14 June 2012 - 06:38 PM
both work
i don't know whether you are asking a question or making a statement?
every time you press a key on your keyboard there is two events . one is a "char" event then other is a "key" event.

the key is always a number but the char is always a character eg "c" or ")" or "1" basically the symbol on the keyboard is "char" and the key is a number eg 1 or 24 or 16. i find it easer to work with "char" events normal as they are more readable
Lyqyd #8
Posted 14 June 2012 - 06:42 PM
Fixed use char in place of key
Spoiler

term.setCursorPos(1,1)
term.clear()
print("push 1-6")
function main()
	local event, p1 = os.pullEvent()
	if event == "char" and p1 == "2" then -- key 1
		rs.setBundledOutput("bottom", colors.white)
		sleep(1.0)
		rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.white)
		sleep(0.20)
		shell.run("next")
	end -- key 1
	if event == "char" and p1 == "3" then
		rs.setBundledOutput("bottom", colors.orange)
		sleep(1.0)
		rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.orange)
		sleep(0.20)
		shell.run("next")
	end -- key 2
	if event == "char" and p1 == "4"then
		rs.setBundledOutput("bottom", colors.green)
		sleep(1.0)
		rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.green)
		sleep(0.20)
		shell.run("next")
	end -- key 3
	if event == "char" and p1 == "5" then
		rs.setBundledOutput("bottom", colors.brown)
		sleep(1.0)
		rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.brown)
		sleep(0.20)
		shell.run("next")
	end -- key 4
	if event == "char" and p1 == "6" then
		rs.setBundledOutput("bottom", colors.black)
		sleep(1.0)
		rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.black)
		sleep(0.20)
		shell.run("next")
	end -- key 5
	if event == "char" and p1 == "7" then
		rs.setBundledOutput("bottom", colors.blue)
		sleep(1.0)
		rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.blue)
		sleep(0.20)
		shell.run("next")
	end -- key 6
end
main()
both work

But they work differently. Key returns a key code as the parameter, which is a number representing the key pressed. Enter is 208 I believe. Char returns an alphanumeric/symbolic character. I don't believe that key 1 corresponds with character 1 in any way (and 0 can't). So you'd need to figure out which key codes you actually wanted.

Char is better for ease of use when using alphanumerics.
BigSHinyToys #9
Posted 14 June 2012 - 06:45 PM
some example coded and the corresponding key on keyboard

28 enter
200 up
208 down
203 left
205 right
74 -
78 +
MysticT #10
Posted 14 June 2012 - 08:32 PM
His code is fine, it works and has the right key codes, he was just showing the finished code (Although there's some things that could be improved).
chriskopp7 #11
Posted 14 June 2012 - 10:34 PM
His code is fine, it works and has the right key codes, he was just showing the finished code (Although there's some things that could be improved).
i am making a program that show what i can do so other can help improve things here let me send the link to it
http://dl.dropbox.com/u/53751527/untitled%20folder.zip