shiny: get mouse position relative to window

801 views
Skip to first unread message

kty...@gmail.com

unread,
Jan 14, 2017, 11:47:17 AM1/14/17
to golang-nuts
How do I get the position for a mouse.Event in shiny using golang.org/x/mobile/event.mouse?

The position returned is the global position on the screen, which changes if I move the window.
The comments in https://github.com/golang/mobile/blob/master/event/mouse/mouse.go#L18 say it's:
"// X and Y are the mouse location, in pixels.". It doesn't mention if they are relative to the screen or the window.

How do I get the position relative to the window?
Or is it an error of the driver (I'm using windows)?
Neither shiny.Screen nor shiny.Window have methods to get the current position of the window.

as....@gmail.com

unread,
Jan 16, 2017, 3:08:50 AM1/16/17
to golang-nuts
I haven't seen this either, the solution depends on what platform you're trying to support. 

I have a package that does this, for Win32. If you're using Windows, you use the the Rect() or ClientAbs() in my package to get the position of the window (or the client area).

https://godoc.org/github.com/as/ms/win

Dave MacFarlane

unread,
Jan 17, 2017, 4:39:34 PM1/17/17
to kty...@gmail.com, golang-nuts
That doesn't sound right to me.

I just added a 'fmt.Printf("X: %v Y: %v\n", e.X, e.Y)' to the switch
case handling mouse.Event in one of my shiny programs, and the mouse
coordinates are reported relative to the top-left corner of the
window.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
- Dave

ktye

unread,
Jan 17, 2017, 5:45:39 PM1/17/17
to Dave MacFarlane, golang-nuts
That's a good hint.
I can confirm that on linux everything works as expected.
The issue is only on windows.
The problem is only with mouse wheel events:
https://github.com/golang/exp/blob/master/shiny/driver/internal/win32/win32.go#L168

lparam is used to get the coordinates of the mouse events.
However depending on the buttons, lparam has different meanings:

WM_MOUSEWHELL message:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms645616(v=vs.85).aspx
lParam
The low-order word specifies the x-coordinate of the pointer,
relative to the upper-left corner of the screen.
The high-order word specifies the y-coordinate of the pointer,
relative to the upper-left corner of the screen.

WM_MOUSEMOVE message:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms645616(v=vs.85).aspx
lParam
The low-order word specifies the x-coordinate of the cursor. The
coordinate is relative to the upper-left corner of the client area.
The high-order word specifies the y-coordinate of the cursor. The
coordinate is relative to the upper-left corner of the client area.

I'll file an issue.
Reply all
Reply to author
Forward
0 new messages