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
Os.pullevent() Followed By Read(), Character Persists
Started by stringcheese, Sep 05 2013 04:28 PM
2 replies to this topic
#1
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:
#2
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
Posted 06 September 2013 - 12:08 PM
stringcheese, 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
3 user(s) are reading this topic
0 members, 3 guests, 0 anonymous users











