({{, (RefLink[EventHandler, paclet:ref/EventHandler])[expr,{"(Subscript[event, 1])":>(Subscript[action, 1]),"(Subscript[event, 2])":>(Subscript[action, 2]),…}] 
displays as expr, evaluating (Subscript[action, i]) whenever "(Subscript[event, i])" occurs in connection with expr. }})

OpenersNotesSection(Spacer1)Details and Options

Standard mouse-related event specifications include:

For mouse events, (RefLink[EventHandler, paclet:ref/EventHandler])[expr, …] handles events for which the mouse is within the rectangular region defined by the display of expr.

Standard keyboard-related event specifications include:

Standard interface events include:

If functions like (RefLink[MousePosition, paclet:ref/MousePosition]) or (RefLink[CurrentValue, paclet:ref/CurrentValue]) are evaluated in a particular (Subscript[action, i]), the values they give are those associated with the event that triggered that (Subscript[action, i]).

When (RefLink[EventHandler, paclet:ref/EventHandler]) expressions are nested, events are by default shared by all the expressions, with the innermost expression operating first on a particular event.

The following options can be given:

Typical possible settings for the (RefLink[Method, paclet:ref/Method]) option include "Preemptive" and "Queued".

With the default setting (RefLink[PassEventsDown, paclet:ref/PassEventsDown])->(RefLink[Automatic, paclet:ref/Automatic]), events handled by an (RefLink[EventHandler, paclet:ref/EventHandler]) are not also passed to built-in event handlers in the Wolfram System front end.

OpenersPrimaryExamplesSection(Spacer1)Examples  (12)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Create text that turns red when clicked:

DynamicModule[{col=Green},EventHandler[Style["text",FontColor->Dynamic[col]],{"MouseClicked":>(col=Red)}]]

Create text that toggles between red and green when clicked repeatedly:

DynamicModule[{col=Green},EventHandler[Style["text",FontColor->Dynamic[col]],{"MouseClicked":>(col=col/.{Red->Green,Green->Red})}]]

OpenersExampleSection1(Spacer1)Scope  (1)

Create a graphic that changes color when clicked:

DynamicModule[{col=Green},EventHandler[Dynamic[Graphics[{col,Disk[]},ImageSize->Tiny]],{"MouseClicked":>(col=col/.{Red->Green,Green->Red})}]]

Create a graphic that changes color only while the mouse button is pressed:

DynamicModule[{col=Green},EventHandler[Dynamic[Graphics[{col,Disk[]},ImageSize->Tiny]],{"MouseDown":>(col=col/.{Red->Green,Green->Red}),
"MouseUp":>(col=col/.{Red->Green,Green->Red})}]]

OpenersExampleSection2(Spacer1)Options  (5)

OpenersExampleSubsection0(Spacer1)Method  (1)

By default, event handlers are evaluated on a preemptive link and time out after 5 seconds:

DynamicModule[{a="start"},{EventHandler[Panel["does time out"],"MouseClicked":>(Pause[6];a="end")],Dynamic[a]}]

Use (RefLink[Method, paclet:ref/Method])->"Queued" to evaluate button functions on the main link, which never times out:

DynamicModule[{a="start"},{EventHandler[Panel["does not time out"],"MouseClicked":>(Pause[6];a="end"),Method->"Queued"],Dynamic[a]}]

OpenersExampleSubsection1(Spacer1)PassEventsDown  (2)

Allow events to pass down to inner event handlers, changing the color as well as position:

DynamicModule[{p={0,0},c=Green},EventHandler[EventHandler[Framed@Dynamic[Graphics[{c,Disk[p,0.2]},PlotRange->2]],{"MouseDown":>(c=c/.{Red->Green,Green->Red})}],{"MouseDown":>(p=MousePosition["Graphics"])},PassEventsDown->True]]

Do not pass mouse events to the inner event handler:

DynamicModule[{p={0,0},c=Green},EventHandler[EventHandler[Framed@Dynamic[Graphics[{c,Disk[p,0.2]},PlotRange->2]],{"MouseDown":>(c=c/.{Red->Green,Green->Red})}],{"MouseDown":>(p=MousePosition["Graphics"])},PassEventsDown->False]]

OpenersExampleSubsection2(Spacer1)PassEventsUp  (2)

By default, nested (RefLink[EventHandler, paclet:ref/EventHandler]) actions are triggered upon the specified action:

DynamicModule[{i=1,j=1},Style[EventHandler[Framed@{Dynamic[i],EventHandler[Framed[Dynamic[j]],"MouseDown":>++j]},"MouseDown":>++i],36]]

By disabling (RefLink[PassEventsUp, paclet:ref/PassEventsUp]), prevent the outer (RefLink[EventHandler, paclet:ref/EventHandler]) action from being triggered:

DynamicModule[{i=1,j=1},Style[EventHandler[Framed@{Dynamic[i],EventHandler[Framed[Dynamic[j]],"MouseDown":>++j,PassEventsUp->False]},"MouseDown":>++i],36]]

Allow the outer (RefLink[EventHandler, paclet:ref/EventHandler]) action when the (Key0[shift]) key is pressed:

DynamicModule[{i=1,j=1},Style[EventHandler[Framed@{Dynamic[i],EventHandler[Framed[Dynamic[j]],"MouseDown":>++j,PassEventsUp:>CurrentValue["ShiftKey"]]},"MouseDown":>++i],36]]

OpenersExampleSection3(Spacer1)Applications  (2)

Automatically fit a line to a set of points:

lineFit[points_,{{xmin_,xmax_},{ymin_,ymax_}}]:=Module[{x,lf},lf=Function@@{{x},Fit[points,{x,1},{x}]};
Line[{{xmin,lf[xmin]},{xmax,lf[xmax]}}]];

DynamicModule[{p={},l={}},
EventHandler[Dynamic@Graphics[{Point[p],l},PlotRange->1,Frame->True],"MouseDown":>(AppendTo[p,MousePosition["Graphics"]];
If[Length[p]≥2,l=lineFit[p,{{-1,1},{-1,1}}]])]]

Piecewise interpolation through a set of points:

interpolationCurve[p_,n_]:=Module[{x,f=Interpolation[p,InterpolationOrder->n ]},First@Plot[Evaluate@f[x],{x,Min[p[[All,1]]],Max[p[[All,1]]]}]];

DynamicModule[{n=2,p={},c={}},
EventHandler[Dynamic@Graphics[{Point[p],c},PlotRange->1,Frame->True],
"MouseDown" :> 
(p=Union[Sort@Append[p,MousePosition["Graphics"]],SameTest->(First[#1]==First[#2]&)];
If[Length[p] ≥n+1,c=interpolationCurve[p,n]])]]

OpenersExampleSection4(Spacer1)Properties & Relations  (1)

(RefLink[EventHandler, paclet:ref/EventHandler]) can be used with (RefLink[MousePosition, paclet:ref/MousePosition]):

DynamicModule[{pos={0,0}},EventHandler[Framed@Graphics[{Disk[Dynamic[pos],0.2]},PlotRange->2],{"MouseDown":>(pos=MousePosition["Graphics"])}]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Mouseover, paclet:ref/Mouseover])  ▪  (RefLink[Button, paclet:ref/Button])  ▪  (RefLink[ClickPane, paclet:ref/ClickPane])  ▪  (RefLink[LocatorPane, paclet:ref/LocatorPane])  ▪  (RefLink[GestureHandler, paclet:ref/GestureHandler])  ▪  (RefLink[CurrentValue, paclet:ref/CurrentValue])  ▪  (RefLink[Manipulate, paclet:ref/Manipulate])  ▪  (RefLink[MousePosition, paclet:ref/MousePosition])  ▪  (RefLink[CellEventActions, paclet:ref/CellEventActions])  ▪  (RefLink[NotebookEventActions, paclet:ref/NotebookEventActions])  ▪  (RefLink[FrontEndEventActions, paclet:ref/FrontEndEventActions])  ▪  (RefLink[ControlActive, paclet:ref/ControlActive])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Introduction to Dynamic, paclet:tutorial/IntroductionToDynamic])}, {▪(RefLinkPlain[Advanced Dynamic Functionality, paclet:tutorial/AdvancedDynamicFunctionality])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Control Objects, paclet:guide/ControlObjects])}, {▪(RefLinkPlain[Low-Level Interface Control, paclet:guide/LowLevelInterfaceControl])}, {▪(RefLinkPlain[Click-Interactive Panels, paclet:guide/ClickInteractivePanels])}, {▪(RefLinkPlain[Custom Interface Construction, paclet:guide/CustomInterfaceConstruction])}, {▪(RefLinkPlain[Standalone Interfaces, paclet:guide/StandAloneInterfaces])}, {▪(RefLinkPlain[Programmatic Notebook & Interface Customization, paclet:guide/ProgrammaticNotebookAndInterfaceCustomization])}, {▪(RefLinkPlain[Graph Visualization, paclet:guide/GraphVisualization])}, {▪(RefLinkPlain[Graphics Interactivity & Drawing, paclet:guide/GraphicsInteractivityAndDrawing])}, {▪(RefLinkPlain[Graph Styling, Labeling, and Layout, paclet:guide/GraphStylingAndLabeling])}})}})