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

os.pullEvent( "key" ) not working correctly.

Started by profjb, 13 July 2015 - 03:53 PM
profjb #1
Posted 13 July 2015 - 05:53 PM
So the below code is a new rules script i'm making based upon the player pressing a number from 1 to 3 for the answer. This code worked fine for a small while but for some reason when i edited something very minor that shouldn't have effected it (the stuff that happens after the else statement) it failed to recognise a keypress at all.


function kp(n)
  while true do
	local event, keypress = os.pullEvent( "key" )
	keys.getName(keypress)
  end
  term.setCursorPos(22,11)
  if keypress == n then
	term.setTextColor(colors.green)
	print("Correct!")
	os.sleep(2)
	term.clear()
  else
	term.setTextColor(colors.red)
	os.sleep(5)
	print("Incorrect, please try again")
  end
end

function y()
term.setTextColor(colors.yellow)
end

local function t(text)
local x,y = term.getSize()
local x1,y1 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y1)
print(text)
end

local function w()
term.setTextColor(colors.white)
end

term.setCursorPos(0,4)
y()
t("testquestion")
w()

options1 = {"1) test1","2) test2","3) test3"}

term.setCursorPos(0,6)
for i = 1, #options1 do
  t(options1[i])
end

kp(keys.two)
Edited on 13 July 2015 - 04:01 PM
profjb #2
Posted 13 July 2015 - 06:01 PM
I added print("test") into the function and it printed fine so it's not a problem with anything being sent to the function.
HPWebcamAble #3
Posted 13 July 2015 - 06:04 PM

while true do
	local event, keypress = os.pullEvent( "key" )
	keys.getName(keypress)			   --# This line doesn't change anything!
  end
  term.setCursorPos(22,11)
  if keypress == n then
	term.setTextColor(colors.green)
	print("Correct!")
	os.sleep(2)
	term.clear()
  else
	term.setTextColor(colors.red)
	os.sleep(5)
	print("Incorrect, please try again")
  end

Did you mean to do this?

keypress = keys.getName(keypress)

Anywho, that isn't your problem, this is:

while true do
	local event, keypress = os.pullEvent( "key" )
	keys.getName(keypress)
  end
Making 'event' and 'keypress' local variables makes them only exist in the while loop.

And MKlegoman pointed out (below) that you never end the loop either.
Edited on 13 July 2015 - 04:13 PM
MKlegoman357 #4
Posted 13 July 2015 - 06:07 PM
Also, you're never actually braking out of the loop.
profjb #5
Posted 13 July 2015 - 06:14 PM
Thats really weird, now you mentioned the while loop being the problem I removed the while loop and it worked fine again. Why did I add a while loop if it was working fine. God knows.

Anyway, thanks Hpwebcamable and MKlegoman357.
Just one last question, if I was to return the value of something in a loop would I do return [value] and then break?
flaghacker #6
Posted 13 July 2015 - 10:36 PM
Thats really weird, now you mentioned the while loop being the problem I removed the while loop and it worked fine again. Why did I add a while loop if it was working fine. God knows.

Anyway, thanks Hpwebcamable and MKlegoman357.
Just one last question, if I was to return the value of something in a loop would I do return [value] and then break?

Try it :P/>.

No, return automatically 'breaks' out of the entite function, so the loop also stops.
Edited on 13 July 2015 - 08:37 PM
Dragon53535 #7
Posted 15 July 2015 - 05:36 AM
I'm pretty sure he meant to have the while loop like this:

while true do
		local event, keypress = os.pullEvent( "key" )
		--local key = keys.getName(keypress)     --# Made it connect to a new variable. Since you're passing keys.two you don't need this line... I'll just comment it out
  --# Notice the lack of end
  term.setCursorPos(22,11)
  if keypress == n then --#Since keypress is the number value of the key being pressed, it will be able to equal keys.two
		term.setTextColor(colors.green)
		print("Correct!")
		os.sleep(2)
		term.clear()
		return --#Just so that your code DOES end when you answer correctly
  else
		term.setTextColor(colors.red)
		os.sleep(5)
		print("Incorrect, please try again")
  end
end --#Oh look new end. Now the while true do loop goes over EVERYTHING
Edited on 15 July 2015 - 03:38 AM