Consider the following example. First, I print the dynamically updated value of the actual position of the mouse, then I print pt, which is the same but is only updated when the EventHandler object (a grey rectangle) is clicked and dragged.

  Graphics[{Gray, Rectangle[]}, ImageSize -> 50, AspectRatio -> 2],
  {"MouseDragged" :> (pt = MousePosition@"ScreenScaled")}]

This has two issues:

  1. While Dynamic@MousePosition@"ScreenScaled" updates correctly when the mouse moves anywhere on the screen, pt is constrained to be updated only in the cell where the EventHandler is when the rectangle is dragged. The EventHandler DOES register and display movement outside the gray Rectangle (and thus outside the EventHandler), though it DOES NOT register movement outside of the cell. Whenever the dragged mouse leaves the cell where the EventHandler resides, the updating stops. Whenever the dragged mouse enters the cell, the updating continues.
  2. When the rectangle is dragged, not even Dynamic@MousePosition@"ScreenScaled" gets updated outside of the cell, though it has nothing to do with the EventHandler, and it is updated correctly on its own (i.e. when EventHandler is not triggered).

The problem is more general: the mouse coordinates over the full screen cannot be accessed in any construction I've tried, not just in EventHandlers. Note that MousePosition["coords"] is equivalent to CurrentValue[{"MousePosition","coords"}], and as such, the latter fails as well.

Question: How to access global mouse coordinates in a dynamic structure such as an EventHandler?


What I want is to create an object (e.g. a button) that accepts/processes/displays the global mousecoordinates only when the mouse is dragged.

  • 1
    $\begingroup$ As to point 2. This also happens if you drag anything outside of the rectangle cell, so it's unrelated to the EventHandler. Dragged mouse movements are simply not seen by MousePosition. Point 1 is the designed behaviour. Events are restricted to the objects used as the first argument. $\endgroup$ Jul 3, 2012 at 20:20
  • $\begingroup$ As per @SjoerdC.deVries comment the mouse coordinates will be limited to the bounding coordinates of the event handler argument. What real world thing would you like to be able to do? Perhaps there are work arounds (?) $\endgroup$ Jul 3, 2012 at 23:03
  • $\begingroup$ I gotta go now so no time to investigate or build an answer, but: $\endgroup$
    – Rojo
    Jul 3, 2012 at 23:05
  • $\begingroup$ @Sjoerd, Mike: But this is simply not true. I also expected that the EventHandler can only register movement inside the EventHandler's argument, but it can register it throughout the whole cell outside the rectangle, i.e. right of it. $\endgroup$ Jul 3, 2012 at 23:07
  • 1
    $\begingroup$ As to point 2, I don't think it's not accessible, but that the front end just doesn't show you updated stuff when you are dragging. Try any dynamic thing like Dynamic@Clock[] and drag your mouse. It probably updates the variables properly anyway. Not sure $\endgroup$
    – Rojo
    Jul 3, 2012 at 23:08

2 Answers 2

DynamicModule[{pressed = False},
    Graphics[{Dynamic[If[pressed, Gray, Black]], Rectangle[]}, 
     ImageSize -> 50, AspectRatio -> 2],
    If[pressed, pt = f[MousePosition@"ScreenScaled"]]
   {"MouseDown" :> (pressed = True), "MouseUp" :> (pressed = False)}]


I am still not totally sure that I understand what you want but it seems like you want to be able to drag the mouse anywhere -- not just in a specific event region -- and have those coordinates displayed or do something with the coordinates. If that is the case then I think that NotebookEventAction might be useful. This button toggles between two states:

on = True;
   NotebookEventActions :> {"MouseDragged" :> (pt1 = 
        MousePosition@"ScreenScaled")}]; on = ! on,
  SetOptions[EvaluationNotebook[], NotebookEventActions :> {}];
  on = ! on
 ImageSize -> 100


When you switch it on you will find that you can drag the mouse anywhere in the notebook and see the coordinates displayed while the mouse is being dragged. You need to switch this on and off because assigning behaviour to a dragged mouse deactivates the normal mouse dragged functionality -- which is why this is normally confined only to an event region.

  • $\begingroup$ While it works nicely, I prefer Rojo's solution as it does the same without manipulating Notebook options. $\endgroup$ Jul 6, 2012 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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