Jump to content




Os.pullevent() Followed By Read(), Character Persists


  • You cannot reply to this topic
2 replies to this topic

#1 stringcheese

  • Members
  • 8 posts

Posted 05 September 2013 - 04:28 PM

This is a simple enough problem that I can't figure out how to fix. I have a to-do list program where there's a menu that uses os.pullEvent() to get a key for a menu selection, say "A" for adding a task to a list. I then go to a function that uses read() to get a string for the task. The problem is that the "a" is always sitting there from the prior os.pullEvent(). How do I purge this "a" before I get to the read()? Here's the pruned code:

local event, p1, p2, p3 = os.pullEvent()
if event == "key" then
  local keyPressed = p1
  if keyPressed == 30 then
	AddTask()
  end

...

function AddTask()
  taskToAdd = read()
end


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 05 September 2013 - 04:35 PM

sleep(0) will clear the stack, or use char events in your menu, since char follows key.

#3 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 06 September 2013 - 12:08 PM

View Poststringcheese, on 05 September 2013 - 04:28 PM, said:

This is a simple enough problem that I can't figure out how to fix. I have a to-do list program where there's a menu that uses os.pullEvent() to get a key for a menu selection, say "A" for adding a task to a list. I then go to a function that uses read() to get a string for the task. The problem is that the "a" is always sitting there from the prior os.pullEvent(). How do I purge this "a" before I get to the read()? Here's the pruned code:

local event, p1, p2, p3 = os.pullEvent()
if event == "key" then
  local keyPressed = p1
  if keyPressed == 30 then
	AddTask()
  end

...

function AddTask()
  taskToAdd = read()
end

You example almost looks like the example at http://computercraft...ki/Os.pullEvent which by the way lists a set of events that might interest you.

Also I think I would optimize that code you showed us a wee bit to:
local _, keyPressed = os.pullEvent("key")
  if keyPressed == 30 then
	AddTask()
  end

...

function AddTask()
  taskToAdd = read()
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users