11 posts
Posted 24 October 2012 - 10:44 AM
SOLVED, just delete this… ^_^/>/>
I'm trying to make a simple notepad-like program. That means that it would have to understand input from the keyboard and the mouse, but the code below doesn't work for me.
while true do
local event, p1, p2, p3 = os.pullEvent()
if event == "char" then
term.write(p1)
elseif event == "mouse_click" and p1 == 1 and p2 <= 3 and p3 <= 3 then
break
end
sleep(0.01)
end
What's confusing me is that the mouse_click works fine, but the char doesn't.
Doe anyone have any idea how to make this run?
175 posts
Posted 24 October 2012 - 10:51 AM
Well, you're not breaking anything if it's a char. It's a bit hard to tell if this is intentional, you might want to post more code.
11 posts
Posted 24 October 2012 - 10:58 AM
What I wanted to do was, when a user inputs a char it would be written to the screen, and when a user clicks the top left corner it would stop the program. But when a key is pressed, nothing happens.
This also doesn't work if instead of the event = "char" there is a event = "key"
2005 posts
Posted 24 October 2012 - 11:16 AM
Try print() or write() instead of term.write() and see what happens.
11 posts
Posted 24 October 2012 - 11:31 AM
Nope, that doesn't work either. I really have no idea what is causing this… :/
11 posts
Posted 24 October 2012 - 11:41 AM
Is there another way to use os.pullEvent and both have a filter for mouse and char?
11 posts
Posted 24 October 2012 - 11:54 AM
Okay, I've figured it out, the problem was somehow the sleep(0.01), I thought I would need to put that there to avoid the "too long without yielding" error. But it seems to work now.
2005 posts
Posted 24 October 2012 - 11:55 AM
Sure, use a table with keys of the events you want to pull and values of what you want to do in the case of that event. Not much of a savings to be had in this case, though.
Oh, I just realized your problem. You always, always get a key event right before a char event, sleep keeps you from getting the char event cause it's right after the key event. And you realized it too.
236 posts
Location
Germany
Posted 24 October 2012 - 01:28 PM
to prevent too long without yielding errors put
sleep(0)
2005 posts
Posted 24 October 2012 - 08:28 PM
A loop that uses "os.pullEvent" won't go too long without yielding anyway, it always pauses execution to await the events. Pretty much any function that pulls an event somehow or other won't go too long without yielding, unless the loop also generates instant events to be pulled.
8543 posts
Posted 24 October 2012 - 10:48 PM
A loop that uses "os.pullEvent" won't go too long without yielding anyway, it always pauses execution to await the events. Pretty much any function that pulls an event somehow or other won't go too long without yielding, unless the loop also generates instant events to be pulled.
No, it doesn't matter how long it yields for, it only has to yield. Any loop that doesn't go more than ten seconds without yielding will be fine, no matter how short the time that it wants to yield is.
2005 posts
Posted 25 October 2012 - 12:59 AM
Oh, so it yields whenever checking for an event to pull, even if it generated the event instantly earlier in it's own loop? That's good to know.
8543 posts
Posted 25 October 2012 - 04:03 AM
Oh, so it yields whenever checking for an event to pull, even if it generated the event instantly earlier in it's own loop? That's good to know.
Yes, it has to yield in order to be resumed with an event.