# How to Use Mouse Events & EventHandler in Demonstrations

One of the primary criteria for an acceptable Demonstration is, of course, that multiple copies of the Manipulate Output Cells [MOCs] have to have independent controls and datasets: the operation of controls in one MOC can’t effect those in other ones or the corresponding data. Which is something I’m having some difficulty with as, in part, I don’t fully understand the scope and implications of various programming constructs. Particularly in cases where there are mouse event handlers attached to the displayed expression.

But the simplest example that I think shows the structure required for my purposes is the following code and images for the two MOCs (the "rslt = n2" bit isn't relevant - preliminary testing):

Manipulate[dsply$expr$2a,
Style[Text[Dynamic[mse$psn]], 12], {{n2, 2}, 1, 10, .1}, {mse$psn, ControlType -> None},
TrackedSymbols :> {n2},
Initialization :> (mse$psn = {0, 0}; dsply$expr$2a := Module[{rslt}, rslt = n2; EventHandler[ Dynamic[Plot[Sin[n2 x], {x, 0, 3 \[Pi]}, ImageSize -> {200, 150}]], {"MouseClicked" :> (mse$psn =
MousePosition["EventHandlerAbsolute"])}]])]


As suggested by the above images for the two MOCs, when they are first evaluated and before moving either of the sliders, clicking on the two different plot areas updates only the corresponding display variable (mse$psn), and this can be done multiple times. But as soon as either of the sliders is moved, the other slider is effectively disabled – no change in the corresponding Plot – and clicking on either Plot area updates only the display variable in the second MOC. I have taken a look at several Demonstrations (here and here), both of which use more or less the same structure – i.e., “EventHandler[Dynamic[Graphics … ]]” – so I must be missing some aspect or option. And I have tried various variations – DynamicModule, With, passing parameters, etc – but I think that those, absent a better understanding, have been mostly shots-in-the-dark. Somewhat in passing, while I am impressed with Mathematica in general, I’m also kind of overwhelmed by the frequently cryptic if not inconsistent nature of many of the commands, structures, and underlying processing algorithms. Consequently, while there seems to be quite a bit of detail available on the evaluation of expressions, I was wondering if there’s something available that describes those algorithms, the scope and values of various variables over the course of program execution – what’s happening underneath the hood. I have thought of purchasing the Work Bench program with the idea that it might help in understanding those aspects, but I saw a brief note on the Wolfram WB site suggesting that it is not able to profile or debug various dynamic structures. And Trace might be similarly limited though I haven’t delved much into that possibility. Thoughts? Suggestions? Pointers ## Addendum: As per Kuba's suggestions below, I've modified the program as follows, and which seems to give the required behaviours: Manipulate[ DynamicModule[{}, EventHandler[ Dynamic[Plot[Sin[n3 x], {x, 0, 3 \[Pi]}, ImageSize -> {200, 150}]], {"MouseClicked" :> (mse$psn =
MousePosition["EventHandlerAbsolute"])}]],
Style[Text[Dynamic[mse$psn]], 12], {{n3, 2}, 1, 10, .1}, {mse$psn, ControlType -> None},
TrackedSymbols :> {n2},
Initialization :> (mse$psn = {0, 0} )]  However, I am not at all sure why the first case didn't work while the second one does. Seems that even though the function dsply$expr$2a is defined in the Initialization section, its call when variables change should have created a new event handler updated with variables local to the instance of the MOC active at the time - obviously not the case but it might be useful, maybe to others as well, to know the differences between the two cases. • You are using dsply$expr$2a and not scoping it. And inside is mse$psn which is scoped. At the end you will have shared dsply.. with reference to psn that was evaluated last. So it will work correctly only in the last manipulate. – Kuba Mar 13 '16 at 9:52
• @Kuba Thanks for the suggestions. But while I don't fully understand the implications of your scoping comments, I tried replacing the dsply$expr$2a in the first parameter of the Manipulate with DynamicModule[{}, EventHandler[Dynamic[Plot ...]]] which does work as required. Even if I don't quite understand the why of it. – Steersman Mar 13 '16 at 20:57
• @Kuba Thanks. As a point of reference, I've edited the original question to show the changes I've made. – Steersman Mar 13 '16 at 21:59
• Sorry I still don't have a good idea how to write good answer but if you can use that to self answer, feel free to do so. So here's a minimal example that shows what I've explained: Manipulate[ {a, expr}, {{a, 1}, ControlType -> None}, Initialization :> ( expr = EventHandler[ Framed@Dynamic@a, {"MouseClicked" :> (Print@1; a++)}])] – Kuba Mar 15 '16 at 20:23

Perhaps the following solution will help.

Manipulate[
EventHandler[
Plot[Sin[n2 x], {x, 0, 3 \[Pi]}],
{"MouseClicked" :> (
mse$psn = MousePosition["EventHandlerAbsolute"] )} ], Style[Text[Dynamic[mse$psn]], 12],
{{n2, 2}, 1, 10, .1},
{{mse$psn,{0,0}}, ControlType -> None}, ]  This solution works with two copies of the code. The mouse can be clicked independently in each copy. The essence of the solution is the line {mse$psn, ControlType -> None}. My first reaction in seeing this construct was to ask why was there a control that did not display? I then realized that the statement was providing a scope to the variable mse\$psn, limiting it to the reach of the Manipulate. If the statement is removed, the two copies become linked as in the original example.

Note: I didn't see any reason for the TrackedSymbols statement; Aren't control variables automatically tracked in a Manipulate?

Note 2: I also didn't see any reason for the DynamicModule. I thought I read somewhere in the documentation that Manipulate was rewritten as a DynamicModule, making the explicit one unnecessary. Or am I missing something?

• Why will it =perhaps= help? – nilo de roock Mar 8 '17 at 13:45
• The OP asked for thoughts to increase understanding. "Perhaps" my post will aid that understanding. – Spencer Rugaber Mar 8 '17 at 14:17