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:
- 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 theEventHandler
is when the rectangle is dragged. TheEventHandler
DOES register and display movement outside the grayRectangle
(and thus outside theEventHandler
), though it DOES NOT register movement outside of the cell. Whenever the dragged mouse leaves the cell where theEventHandler
resides, the updating stops. Whenever the dragged mouse enters the cell, the updating continues. - When the rectangle is dragged, not even
Dynamic@MousePosition@"ScreenScaled"
gets updated outside of the cell, though it has nothing to do with theEventHandler
, 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 EventHandler
s. 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.
MousePosition
. Point 1 is the designed behaviour. Events are restricted to the objects used as the first argument. $\endgroup$EventHandler
can only register movement inside theEventHandler
's argument, but it can register it throughout the whole cell outside the rectangle, i.e. right of it. $\endgroup$