Get position of mouse cursor on canvas

Get position of mouse cursor on canvas

thomasa88
Advocate Advocate
625 Views
2 Replies
Message 1 of 3

Get position of mouse cursor on canvas

thomasa88
Advocate
Advocate

I want to programmatically rotate the camera around the mouse position, so I need to know where the cursor is.

 

Is there any way to get the current mouse position on the canvas?

 

I have found going from Point2d, I have found MouseEvent and MouseEventArgs, but they require me to start a Command first. Is it possible to get the mouse position without starting a Command?

 

0 Likes
626 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor

Hi @thomasa88 .

 

In the past, I created an add-in that uses MouseMove Event.

https://github.com/kantoku-code/Fusion360_OnTheFly 

 

Of course, it was a command event.
I don't think there is any other way to call MouseEvent other than the command event.

 

I thought about using CustomEvent, but I don't know how to get the mouse cursor position.

 

Also, I couldn't find a way to confirm that the mouse cursor exists on the canvas.

 

0 Likes
Message 3 of 3

thomasa88
Advocate
Advocate

Thanks!

 

I went off on another track in my camera/matrix adventures, but for those who are interested, here's a quick way using the Windows APIs..

 

from ctypes import windll, Structure, c_long, byref

class POINT(Structure):
    _fields_ = [("x", c_long), ("y", c_long)]

def win32_mouse_pos():
    win_point = POINT()
    windll.user32.GetCursorPos(byref(win_point))
    return adsk.core.Point2D.create(win_point.x, win_point.y)

mouse_2d = app_.activeViewport.screenToView(win32_mouse_pos())
mouse_3d = app_.activeViewport.viewToModelSpace(mouse_2d)

Adapted from https://stackoverflow.com/a/24567802/106019 .

 

The crux is then to decide exactly where to place the mouse, in the screen in-out direction.