Jump to content




Basic os.pullEvent help

help

2 replies to this topic

#1 fatboychummy

  • Members
  • 25 posts

Posted 07 October 2016 - 09: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.

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.

#2 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 08 October 2016 - 12:34 AM

When pulling an event, even with a filter, you'll get the event first, then the data - so it should be...
_, key = os.pullEvent("key") --# using _ as a throwaway variable to capture the event


#3 fatboychummy

  • Members
  • 25 posts

Posted 09 October 2016 - 02:33 AM

View PostDog, on 08 October 2016 - 12:34 AM, said:

When pulling an event, even with a filter, you'll get the event first, then the data - so it should be...
_, key = os.pullEvent("key") --# using _ as a throwaway variable to capture the event

Thanks; It worked and my really crappy shutdown program works :D
...now to work on the startup program and then continue making....
...something





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users