This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[Programming]How do I use a key press to activate something?
Started by EmTeaKay, 02 August 2012 - 03:34 AMPosted 02 August 2012 - 05:34 AM
How do I use a key press like 'enter' to use shell.run("[Program name here]")?
Posted 02 August 2012 - 05:40 AM
while true do
local event, param = os.pullEvent()
if event == "key" and param == 28 then -- key 28 is the 'enter' key
shell.run("program name here")
end
end
Posted 02 August 2012 - 06:23 AM
http://www.minecraft.../wiki/Key_Codes - The link for the key codes.
while true do
event, p1 = os.pullEvent()
if event == "key" then
if p1 == KEYCODE_HERE then
shell.run("PROGRAM_HERE")
end
end
end
Posted 02 August 2012 - 06:36 AM
You can also filter the os.pullEvent() so it only triggers off of a key event. This will help eliminate the need of some of the if statements.
while true do
event, p1 = os.pullEvent("key")
if p1 == KEYCODE_HERE then
shell.run("PROGRAM_HERE")
end
end
Posted 02 August 2012 - 04:11 PM
is it possible to use os.pullEvent ("key" to output redstone signals?
Posted 02 August 2012 - 04:14 PM
Absolutely! That is the beauty of ComputerCraft!
quick example:
quick example:
event, p1 = os.pullEvent("key")
if p1== KEYCODE_HERE then
rs.setOutput("side", true)
sleep(.2)
rs.setOutput("side", false)
end
Not tested, but that should do it.Posted 02 August 2012 - 04:59 PM
probably should maybe start another thread, but what im trying to do is control a couple of frame motors that require redstone pulses with the computercraft computer with a redstone wire on each side of it and for every button press fires the redstone once up arrow left side and down arrow right side.. is this possible?
Posted 02 August 2012 - 05:06 PM
Yeah, just try this with a little modification to your setup:
event, p1 = os.pullEvent("key")
while true do
if p1== KEYCODE_HERE then
rs.setOutput("side", true)
sleep(.2)
rs.setOutput("side", false)
elseif p1==KEYCODE2_HERE then
rs.setOutput("side", true)
sleep(.2)
rs.setOutput("side", false)
elseif p1==KEYCODE3_HERE then
rs.setOutput("side", true)
sleep(.2)
rs.setOutput("side", false)
elseif p1==KEYCODE4_HERE then
rs.setOutput("side", true)
sleep(.2)
rs.setOutput("side", false)
else
sleep(.5)
os.reboot()
end
end
All you need to do is input the keycodes in the appropriate areas, as well as the sides. The sleep(.2) is going to have the pulse last for .2 secods before shutting off, and can be increased if you need it to.Posted 02 August 2012 - 05:41 PM
doesn't seem to be looping? i got it to light up the redstone on the proper side for the proper keys, but it just stays lit
Posted 02 August 2012 - 05:49 PM
Does it come back to wait for the input, or is it just ending the program? If it's waiting for the input again, then you may want to increase the sleep() time. Other than that, I'm not sure…
Sorry, I haven't tested the program yet, since I'm at work.
Sorry, I haven't tested the program yet, since I'm at work.
Posted 02 August 2012 - 05:55 PM
i basically just want the redstone output to pulse on for a split second then off, and then come back to wait for the next input.
what it is doing is keeping the redstone lit up and then pretty much locking up till i hold ctrl r to reset it again
what it is doing is keeping the redstone lit up and then pretty much locking up till i hold ctrl r to reset it again
Posted 02 August 2012 - 06:16 PM
This should work:
local function pulse(side)
rs.setOutput(side, true)
sleep(0.1)
rs.setOutput(side, false)
sleep(0.1)
end
while true do
local evt, key = os.pullEvent("key")
if key == 208 then -- Down
pulse("down side")
elseif key == 200 then -- Up
pulse("up side")
elseif key == 203 then -- Left
pulse("left side")
elseif key == 205 then -- Right
pulse("right side")
end
end
The pullEvent should be inside the loop, so it waits for input again.Posted 02 August 2012 - 06:29 PM
Derp, now that you say it, I see my error…The pullEvent should be inside the loop, so it waits for input again.
Posted 02 August 2012 - 08:54 PM
i basically just want the redstone output to pulse on for a split second then off, and then come back to wait for the next input.
what it is doing is keeping the redstone lit up and then pretty much locking up till i hold ctrl r to reset it again
I have a simple script that can control 1 frame motor at a time. You can tell it how many times to move the direction and it is made for a frame motor.
write ("How many times would you like to move forward? Please enter the number of times: ")
local times = tonumber(read())
for donttouch=1, times do
redstone.setOutput("left". true)
sleep(1)
redstone.setOutput("left", false)
sleep(1)
end
Posted 04 August 2012 - 12:46 AM
On this subject, how would you have a block of code activate if ANY key is pressed?
Posted 04 August 2012 - 01:02 AM
On this subject, how would you have a block of code activate if ANY key is pressed?
while true do -- optional
os.pullEvent("key")
[block of code]
end -- optional
example usage
[block of code]
print("Error code has failed press any key to continue")
os.pullEvent("key")
[block of code]
simple haPosted 04 August 2012 - 01:04 AM
Hmm… I think I may have to create a new thread, since my problem may be more complicated. But thank you, help me in the next thread!(I'm not gonna hijack this guys' thread)
Posted 06 August 2012 - 03:34 PM
use if key ~= 1 thenHmm… I think I may have to create a new thread, since my problem may be more complicated. But thank you, help me in the next thread!(I'm not gonna hijack this guys' thread)
–code
this will run the code if any key is pressed apart from esc (so it doesnt run when you are off the pc)