5
$\begingroup$

The documentation for LocatorPane says that LocatorPane is a special case of EventHandler, and gives the following code:

DynamicModule[{pt = {0, 0}}, 
  Deploy@EventHandler[
     Framed@Graphics[Disk[Dynamic[pt]], PlotRange -> 10], 
     "MouseDown" :> (pt = MousePosition["Graphics"]), 
     "MouseDragged" :> (pt = MousePosition["Graphics"])
  ]
]

I want to recreate the LocatorAutoCreate functionality of LocatorPane[] using EventHandler[]. That is, I want to create a graphic in which, when the user presses some key in combination with a mouse press, the mouse position is appended to the list of points being plotted. Here is my first attempt:

Dynamic[Deploy@
 EventHandler[
   Framed@ListPlot[pts, PlotRange -> {{-10, 10}, {-10, 10}}], 
   {"MouseDown" :> (pts = Append[pts, MousePosition["Graphics"]])}
 ]
]

This causes the mouse position to be appended when a user presses the mouse down.

Here are my two questions that I cannot find the answer to:

  1. How do you represent a compound event (e.g. pressing down a key and pressing the mouse down simultaneously) within EventHandler[]?

  2. How do you use a key press event when the expression (first argument of EventHandler[]) is a graphics object?

For example, this code works as intended

EventHandler[InputField[], {"KeyDown", "x"} :> Print["x has been pressed"]]

but the following code does not

Dynamic[Deploy@
  EventHandler[Framed@ListPlot[pts, PlotRange -> {{-10, 10}, {-10, 10}}], 
  {"KeyDown","x"} :> (pts = Append[pts, MousePosition["Graphics"]])}
  ]
]
$\endgroup$
3
$\begingroup$

Does it fit your needs?

pts = RandomReal[1, {3, 2}];

Deploy @ EventHandler[
  Framed @ Dynamic @ ListPlot[pts, PlotRange -> 1]
, "MouseDown" :> If[
     {"Control"} === CurrentValue@"ModifierKeys"
  ,  AppendTo[pts, CurrentValue[{"MousePosition", "Graphics"}]]
  ]
]
$\endgroup$
  • 1
    $\begingroup$ also If[CurrentValue["ControlKey"], ...] $\endgroup$ – kglr Feb 21 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.