Event Handling

edited October 2014 in Programming Questions

I have a question about event handling.

Some of my code is not working properly, and I suspect it is because I am unfamiliar with the way processing handles mouse events.

So, say I am dong something in the draw function.

And I also have a mousePressed function implemented.

When someone clicks their mouse, what happens with the draw function? Does it pause until the mousePressed function exits, or do they happen asyncronously via multi-threading?

(It seems not to be asyncronous to me.)

If it does pause and wait, does it resume at the point it paused at or does it start at the beginning of the loop?

My best guess is that when someone clicks their mouse, that event doesn't get acted upon until the draw function finishes its current iteration, then the event gets taken care of and the draw function resumes at the beginning of the next iteration. Is this correct?

As a side note, is there stuff that you should only do in the draw function? for example, in my event handlers, I am using resetMatrix() this doesn't seem to cause any trouble, but processing (.js atleast) seems to be picky about some stuff.

Answers

  • Answer ✓

    There's a separate Thread which monitors & enqueues every input event.
    Once draw() is finished, those events are dequeued empty 1 by 1 synchronously.

  • edited October 2014

    the QUEUE is FIFO correct?

    and also, the tread is just for holding the events, right? the all get handled in the main thread right?

  • Basically, sketches are run by a Thread called "Animation Thread".
    The other 1 responsible to collect input events is called "Event Dispatch Thread".
    And yes, a Queue is a FIFO! And even when they're dequeued, they're still run by the "Animation Thread".
    That is, unless we issue noLoop(). In that case, events are run by the "Event Dispatch Thread" instead.

  • gotchya.

    I derped on the queue thing.

    And processing.js is the same way correct, so there should be no problem running anything I want the mousePressed function, then? as long as I'm aware that it will be executing in between draw() iterations

  • Answer ✓

    That's right. Sketches use 1 Thread only! Instructions are followed 1 by 1.
    Of course, we can create other threads if we want to! :D

Sign In or Register to comment.