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

[solved] Read() almost after os.pullEvent("key") showing pressed key

Started by First_One, 12 January 2015 - 09:45 PM
First_One #1
Posted 12 January 2015 - 10:45 PM
Silly question

With this code:

function main()
local ch
while ch ~= "9" do
  term.clear()
  term.setCursorPos(1,1)
  print(" ##  -- - OPTIONS - --  ## ")
  print(" 1 - Option 1")
  print(" 2 - Option 2")
  print(" 9 - Exit")
  write(" -> ")
  ch = read()
  if(ch == "1") then
   --option1()
  elseif(ch == "2") then
   --option2()
  elseif(ch == "9") then
   print("Bye Bye")
  else
   print("WRONG OPTION")
   write("Press any key to continue...")
   os.pullEvent("key")
  end
end
end
main()

When i hit a key, it's written after the "->", so everytime i hit a wrong number and hit for example A,B, whatever to continue , i have to erase that char cause, even though i print some stuff after that, the char is written on the screen

Like this:
[namedspoiler:Hit wrong number:]
[attachment=2048:CC1.png]


[namedspoiler:Hit 'a' once:]
[attachment=2050:cc2.png]
Edited on 12 January 2015 - 10:54 PM
KingofGamesYami #2
Posted 12 January 2015 - 10:52 PM
Try adding a sleep(0) after os.pullEvent( "key" )
First_One #3
Posted 12 January 2015 - 11:24 PM
Try adding a sleep(0) after os.pullEvent( "key" )

Nice.. that's a fix. Thx

Is it better to add some sort of [solved] to title?
Bubba #4
Posted 12 January 2015 - 11:32 PM
Try adding a sleep(0) after os.pullEvent( "key" )

Nice.. that's a fix. Thx

Is it better to add some sort of [solved] to title?

It certainly can't hurt. It's not at all required, but if you feel like doing so as a courtesy to readers I'm sure it would be appreciated. Oftentimes people will still pop in the thread to check whether the accepted answer should really be the accepted answer: sometimes even if it works it might not work in all cases, or it might simply be a horrible way to do it.