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

Help with os.pullEvent()

Started by axel.codeFail(), 18 April 2014 - 01:01 AM
axel.codeFail() #1
Posted 18 April 2014 - 03:01 AM
I'm trying to make a verifying step to a program, but for some reason it doesn't pickup the key press.


shell.run("clear")
if fs.exists("startup") then
  print("Are you sure you want to replace your current startup file?  Y/N")
  while true do
    ev, but = os.pullEvent()
    if but == y then
      break
      
    elseif but == n then
	  print("Ok, I won't override it.")
	  sleep(.5)
	  print(textutils.slowWrite("rebooting..."))
	  sleep(1)
	  os.reboot()
    end
  end
  fs.delete("startup")
  fs.copy("VectOS/programs/startup", "startup")
else
  fs.copy("VectOS/programs/startup", "startup")
end

os.reboot()

When I press "y" or "n", it does nothing.
Grim Reaper #2
Posted 18 April 2014 - 04:01 AM
When you write

but == y
, the interpreter thinks that you are trying to compare the value stored in 'but' with the value stored in the variable 'y'. Simply put, you forgot the quotes around y and n which tell the interpreter that they are literal strings.

To fix this, just do this:

if but == "y" then

elseif but == "n" then
. This way the interpreter will understand that you want to compare the value stored in 'but' to the literal string value of "y" or "n".
axel.codeFail() #3
Posted 18 April 2014 - 04:06 AM
When you write

but == y
, the interpreter thinks that you are trying to compare the value stored in 'but' with the value stored in the variable 'y'. Simply put, you forgot the quotes around y and n which tell the interpreter that they are literal strings.

To fix this, just do this:

if but == "y" then

elseif but == "n" then
. This way the interpreter will understand that you want to compare the value stored in 'but' to the literal string value of "y" or "n".

Oh, thanks.
KingofGamesYami #4
Posted 18 April 2014 - 04:08 AM
You may also want to change os.pullEvent() to os.pullEvent( "key" ) if you want only key events as input (eg. it won't pick up mouse clicks on advanced monitors)
axel.codeFail() #5
Posted 18 April 2014 - 04:31 AM
You may also want to change os.pullEvent() to os.pullEvent( "key" ) if you want only key events as input (eg. it won't pick up mouse clicks on advanced monitors)

I'm have it set so that nothing will happen unless the variable 'but' is equal to "y" or "n", and no other event (to my knowledge) will send those.
Grim Reaper #6
Posted 18 April 2014 - 04:42 AM
You may also want to change os.pullEvent() to os.pullEvent( "key" ) if you want only key events as input (eg. it won't pick up mouse clicks on advanced monitors)

Actually, filtering for only key events would cause the program to never work properly; the variable 'but' would always contain a number rather than the character value returned by a character event.