8 posts
Posted 05 September 2013 - 05: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
8543 posts
Posted 05 September 2013 - 05:35 PM
sleep(0) will clear the stack, or use char events in your menu, since char follows key.
187 posts
Location
Bowie, TX
Posted 06 September 2013 - 01:08 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
You example almost looks like the example at
http://computercraft.info/wiki/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