We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Order of operations on keypress or mousemove etc.
Page Index Toggle Pages: 1
Order of operations on keypress or mousemove etc. (Read 861 times)
Order of operations on keypress or mousemove etc.
Mar 19th, 2008, 8:10am
 
My code is causing problems so I'm looking into this.

The draw routine runs at the speed of fps, but what happens when a keypress is caught with keyPressed() or a mouse event occurs?  When does the operations in mouse-keyboard releated functions occur? Is it deterministic?

Because in some cases, if you generate very fast keyboard and mouse events, if they are processed in the middle of a draw operation, things will fail.

I assume that there is a single thread running in the program, so how does draw() and kayboard-mouse event functions interlock? What is the best practise to avoid problems?

I am thinking of this: a global keypressedflag and mouseeventflag which are false by default. When there is an event generated, the keyPressed or mouse event functions will just set these flags to true, and the draw() routine will check the flags. If they are true, then it will make things happen at appropriate times. Is this a good approach?

Thanks...
Re: Order of operations on keypress or mousemove e
Reply #1 - Mar 19th, 2008, 11:57am
 
Event handlers are called whenever the event occurs, with no regard to what is going on in the rest of your sketch. That's why it's a bad idea to call any drawing commands or change any data that is used inside draw() inside an even handler.

Your suggested approach is the recommended solution: Declare boolean flags for whatever operation you need to happen in a predictable manner, and handle the operations accordingly inside draw().

The example in "Examples > Libraries > PDF Export > MousePress" shows how to use this strategy to save a single frame to PDF. That logic can be extended to various actions triggered by different event handlers.
Re: Order of operations on keypress or mousemove e
Reply #2 - Mar 19th, 2008, 8:58pm
 
Hey thanks! Valuable info. It works now. So is it safe to say that event handlers are running in a seperate thread than the thread of the draw operation?

Are there any similar situations that are essential to know about?
Page Index Toggle Pages: 1