EventHandler[expr,{"event1":>action1,"event2":>action2,…}]
displays as expr, evaluating actioni whenever "eventi" occurs in connection with expr.
EventHandler
EventHandler[expr,{"event1":>action1,"event2":>action2,…}]
displays as expr, evaluating actioni whenever "eventi" occurs in connection with expr.
Details and Options
- Standard mouse-related event specifications include:
-
"MouseClicked" mouse clicked without moving {"MouseClicked",i} mouse button i clicked without moving "MouseDown" primary mouse button depressed {"MouseDown",i} mouse button i depressed "MouseDragged" mouse moving with primary button depressed {"MouseDragged",i} mouse moving with button i depressed "MouseMoved" mouse moves in the region defined by expr "MouseUp" primary mouse button released {"MouseUp",i} mouse button i released - For mouse events, 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:
-
"KeyDown" any key on the keyboard depressed {"KeyDown","x"} key x depressed "ReturnKeyDown"
or
key depressed"EscapeKeyDown"
key depressed"LeftArrowKeyDown" left arrow key depressed "RightArrowKeyDown" right arrow key depressed "UpArrowKeyDown" up arrow key depressed "DownArrowKeyDown" down arrow key depressed - Standard interface events include:
-
{"MenuCommand","name"} menu command with specified name chosen "WindowClose" closing of window requested - If functions like MousePosition or CurrentValue are evaluated in a particular actioni, the values they give are those associated with the event that triggered that actioni.
- When 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:
-
Method "Preemptive" the evaluation method to use PassEventsDown Automatic whether to pass events to inner event handlers PassEventsUp True whether to pass events to outer event handlers - Typical possible settings for the Method option include "Preemptive" and "Queued".
- With the default setting PassEventsDown->Automatic, events handled by an EventHandler are not also passed to built-in event handlers in the Wolfram System front end.
Examples
open all close allBasic Examples (2)
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})}]]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})}]]Options (5)
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 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]}]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]]PassEventsUp (2)
By default, nested 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 PassEventsUp, prevent the outer 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 EventHandler action when the
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]]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[{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]])]]Properties & Relations (1)
EventHandler can be used with MousePosition:
DynamicModule[{pos = {0, 0}}, EventHandler[Framed@Graphics[{Disk[Dynamic[pos], 0.2]}, PlotRange -> 2], {"MouseDown" :> (pos = MousePosition["Graphics"])}]]Neat Examples (4)
Click to add points to a BSplineCurve:
DynamicModule[{pts = {}, down = False},
EventHandler[
Graphics[{Thickness[.03], Red,
BSplineCurve[Dynamic[pts]]}, PlotRange -> 1, Frame -> True, FrameTicks -> False],
{"MouseDown" :> (AppendTo[pts, MousePosition["Graphics"]];
down = True), "MouseMoved" :> If[down, AppendTo[pts,
MousePosition["Graphics"]]], "MouseUp" :> (down = False)}]]Clicking the cone will reflect it across the
axis:
Graphics3D[DynamicModule[{b = False}, EventHandler[Cone[Dynamic[{{4, 4, 0}, {4, 4, If[b, -2, 2]}}]], {"MouseDown" :> (b = !b)}]], PlotRange -> {Automatic, Automatic, {-2, 2}}]Highlight prime numbers in a list when mousing over the list:
DynamicModule[{f = Identity}, (EventHandler[Dynamic[Row[f[#1]& /@ Range[15], ","]], {"MouseEntered" :> (f = Function[{y}, If[PrimeQ[y], Style[y, Red], y]]), "MouseExited" :> (f = Identity)}])]Display dynamic graphics and beep simultaneously:
DynamicModule[{obj = {}, x},
EventHandler[
EventHandler[Dynamic[If[x < 1, x = x + .01];Graphics[{obj, Orange, Disk[{0, 0}, .2]}, PlotRange -> 1]], {"MouseClicked" :> (obj = {Dynamic[Table[{Circle[{0, 0}, n Dynamic[x]]}, {n, 3}]]};Beep[])}], {"MouseDown" :> (x = 0)}, PassEventsDown -> False]]Tech Notes
Text
Wolfram Research (2007), EventHandler, Wolfram Language function, https://reference.wolfram.com/language/ref/EventHandler.html (updated 2008).
CMS
Wolfram Language. 2007. "EventHandler." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2008. https://reference.wolfram.com/language/ref/EventHandler.html.
APA
Wolfram Language. (2007). EventHandler. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/EventHandler.html
BibTeX
@misc{reference.wolfram_2026_eventhandler, author="Wolfram Research", title="{EventHandler}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/EventHandler.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_eventhandler, organization={Wolfram Research}, title={EventHandler}, year={2008}, url={https://reference.wolfram.com/language/ref/EventHandler.html}, note=[Accessed: 13-June-2026]}