Posted 07 October 2016 - 10:43 PM
Hey people who are smarter than me; I'm trying to make a basic, personalized shutdown program in which it asks you if you are sure you want to shutdown with a yes/no highlighted area. Basically I want it so when you press the left or right arrow key it switches the highlight to either yes/no. I have this portion done, and upon a while true do test it swapped between the two normally, but the problem I'm having is waiting for the arrow key to be pressed, and only THEN acting on it.
I will post the code on here, and highlight the part I'm having problems with.
It errors off the E1 error code, so I know the yn variable isn't doing anything wrong at this point, I figure either I put the
key = os.pullevent("key")
in wrong or something, or that it is working, but the Swap() function doesn't run after being called.
Also, is there an easy way to do this in a single function? I feel there should be but am unable to figure it out in my coding youth.
I will post the code on here, and highlight the part I'm having problems with.
term.clear()
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
x,y = term.getSize()
term.setCursorPos((x/2)-5, (y/2)-1)
print("YES")
term.setBackgroundColor(colors.blue)
term.setCursorPos((x/2)+2, (y/2)-1)
print("NO")
term.setCursorPos((x/2)-16, (y/2)-2)
term.write("Are you sure you want to shut down?")
yn = 0
function Swap()
if yn == 1 then
term.setCursorPos((x/2)-5, (y/2)-1)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
print("YES")
term.setCursorPos((x/2)+2, (y/2)-1)
term.setBackgroundColor(colors.blue)
print("NO")
yn = 0
os.sleep(1)
Keydet()
elseif yn == 0 then
term.setCursorPos((x/2)-5, (y/2)-1)
term.setBackgroundColor(colors.blue)
term.setTextColor(colors.white)
print("YES")
term.setCursorPos((x/2)+2, (y/2)-1)
term.setBackgroundColor(colors.black)
print("NO")
yn = 1
os.sleep(1)
Keydet()
else
error("An internal error has occured (E2)")
end
end
[color=#ff0000]function Keydet()
while true do
key = os.pullEvent("key")
if key == keys.d then
Swap()
elseif key == keys.a then
Swap()
elseif key == keys.enter then
if yn == 1 then
Shutdown()
elseif yn == 0 then
Dont()
else
error("An internal error has occured (E3)")
end
else
error("An internal error has occurred (E1)")
end
end
end[/color]
function Shutdown()
term.setBackgroundColor(colors.yellow)
term.clear()
term.setTextColor(colors.black)
term.setCursorPos(7,7)
print("See ya")
os.sleep(1.5)
os.shutdown()
end
function Dont()
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
error("You have requested to keep the computer running")
end
Keydet()
It errors off the E1 error code, so I know the yn variable isn't doing anything wrong at this point, I figure either I put the
key = os.pullevent("key")
in wrong or something, or that it is working, but the Swap() function doesn't run after being called.
Also, is there an easy way to do this in a single function? I feel there should be but am unable to figure it out in my coding youth.