Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

Dynamic@MousePosition@"ScreenScaled"
Dynamic@pt
Deploy@EventHandler[
  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?

EDIT:

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.

share|improve this question
1  
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. –  Sjoerd C. de Vries Jul 3 '12 at 20:20
    
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 (?) –  Mike Honeychurch Jul 3 '12 at 23:03
    
I gotta go now so no time to investigate or build an answer, but: –  Rojo Jul 3 '12 at 23:05
    
@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. –  István Zachar Jul 3 '12 at 23:07
1  
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 –  Rojo Jul 3 '12 at 23:08
show 5 more comments

2 Answers

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

Dynamic@pt
share|improve this answer
add comment

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;
Button["On/Off",
 If[TrueQ[on],
  SetOptions[EvaluationNotebook[], 
   NotebookEventActions :> {"MouseDragged" :> (pt1 = 
        MousePosition@"ScreenScaled")}]; on = ! on,
  SetOptions[EvaluationNotebook[], NotebookEventActions :> {}];
  on = ! on
  ],
 ImageSize -> 100
 ]


Dynamic@pt1

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.

share|improve this answer
    
While it works nicely, I prefer Rojo's solution as it does the same without manipulating Notebook options. –  István Zachar Jul 6 '12 at 14:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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