locked
Get the mouse position RRS feed

  • Question

  • Hello, I'm not familiar with javascript, and I'd liked to get the position of my cursor, and to display it in my gadget.
    My goal is to create a gadget that calculates the distance traversed by your mouse ...
    Thanks for your responses.
    Sunday, February 25, 2007 4:02 PM

Answers

  • The only time you can track the mouse outside of a Gadget is through setCapture().  But it only works whilst the initiating mouse button is pressed, or the mouse goes over a window relating to Sidebar.
    Sunday, February 25, 2007 11:23 PM

All replies

  • You'll only be able to capture the mouse movement when it's inside your Gadget:

    First you want to trigger a function when the mouse moves and within that function grab the mouse coordinates:

    document.onmousemove=mouseMoved;

    function mouseMoved() {
        mouseXpos = event.clientX;
        mouseYpos = event.clientY;
    }

    clientX, clientY will return the mouse pos relative to your Gadget window. See the MSDN article on the onmousemove event for more options.
    Sunday, February 25, 2007 4:18 PM
  • Thank you very much, it helped me to understand how it works ...
    So your saying that I will never be able to get the position of the cursor when it's out of the gadget, right ?
    Sunday, February 25, 2007 4:32 PM
  • The only time you can track the mouse outside of a Gadget is through setCapture().  But it only works whilst the initiating mouse button is pressed, or the mouse goes over a window relating to Sidebar.
    Sunday, February 25, 2007 11:23 PM
  • A bit late, but I found this in a post by Luc101and changed it s bit to get it functioning in VB2010:

    Public Class Form1
        Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Integer
            Dim mousepos As Point
            ' This stores the cordinates of the mouse cursors location
            Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
                Dim R As Long = GetCursorPos(mousepos) ' You'll get your location in mousepos
                Label1.Text = "Mouse Cursor Location : " & mousepos.X & "x" & mousepos.Y
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Timer1.Enabled = True
            Timer1.Start()
        End Sub
    End Class


    • Edited by drfj Saturday, November 8, 2014 4:13 AM addition
    Saturday, November 8, 2014 4:10 AM