Detect Mouse Movement

JohnPoole

Active Member
Joined
Jun 9, 2005
Messages
267
Hi all, I was wondering if anybody knows how to detect to see if the mouse has moved? I'm not sure if this possible, or indeed exactly how sensitive it would need to be, so I guess any solution would need to be able to determine how many pixels the position has changed before executing further code.

Any advice appreciated as always.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Depends on how you want to detect the mouse movement.

If you are interested in seeing the coordinates in a cell on a constant basis such as in cell A1, run the macro named PositionXY and move the mouse around.

To exit the procedure, double click any cell and then either hit Esc, Enter, or select any cell.

All this goes into a new fresh standard module:

Code:
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Type POINTAPI
x As Long
y As Long
End Type
 
Sub PositionXY()
Dim lngCurPos As POINTAPI
Do
GetCursorPos lngCurPos
Range("A1").Value = "X: " & lngCurPos.x & " Y: " & lngCurPos.y
DoEvents
Loop
End Sub
 
Upvote 0
Yes, you can detect mouse movement in Excel VBA. I've attached code to give you an idea of how it can work:

Code:
Private Type POINTAPI
    X As Long
    Y As Long
End Type
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
 
Sub MouseMoveTest()
    Dim lngCurPos As POINTAPI
    Dim DocZero As POINTAPI
    Dim PointsPerPixelY As Double
    Dim PointsPerPixelX As Double
    Dim hdc As Long
 
    hdc = GetDC(0)
    PointsPerPixelY = 72 / GetDeviceCaps(hdc, 90)
    PointsPerPixelX = 72 / GetDeviceCaps(hdc, 88)
    ReleaseDC 0, hdc
 
    DocZero.Y = ActiveWindow.PointsToScreenPixelsY(0)
    DocZero.X = ActiveWindow.PointsToScreenPixelsX(0)
 
    Do
 
        GetCursorPos lngCurPos
 
        Rowposition = (lngCurPos.Y - DocZero.Y) * PointsPerPixelY
        Colposition = (lngCurPos.X - DocZero.X) * PointsPerPixelX
 
        Cells(1, 1) = Rowposition 
        Cells(1, 2) = Colposition 
 
    DoEvents
 
    Loop
End Sub

Put this code in any sheet and run it. When you move the mouse you will see the values in cells A1 and B1 change. These number correspond to the cursor position relative to the worksheet, not the computer screen. You should be able to easily modify this to your needs.

Take care.

Owen
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top