Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mouse position recorder

35 views
Skip to first unread message

James

unread,
May 27, 2002, 5:30:47 PM5/27/02
to
Hi,

Does anyone have any idea how I can write a mouse recorder
which listens to mouse clicks on Excel application and
send the XY position to a file?

Can this be done using visual basic, or is VC++ preferred?
Any helpful API's available? Please help.

Thanks,
James

Vasant Nanavati

unread,
May 27, 2002, 8:47:37 PM5/27/02
to
Hi James:

The following will get you the mouse coordinates in pixels. However, at
least within VBA, there is no Click event for an Excel sheet, so I don't
know how you would trap the clicks.

Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long

Type POINTAPI
X As Long
Y As Long
End Type

Sub ShowPos()
Dim lRetVal As Long
Dim Pos As POINTAPI
lRetVal = GetCursorPos(Pos)
MsgBox Pos.X & ", " & Pos.Y
End Sub

"James" <kse...@yahoo.com> wrote in message
news:94ca01c205c5$c41d0e90$35ef2ecf@TKMSFTNGXA11...

Victor Eldridge

unread,
May 28, 2002, 12:54:44 AM5/28/02
to
That's an interesting idea, a sort of macro recorder for the VB illiterate.

As Vasant mentioned, there are no built-in events to trigger your code to
record a click position, so the only way to do something like this would be
to have a loop that is constantly running, examining your mouse coordinates
and buttons. Such a loop is generally not a good idea as it will hog your
CPU at 100% and can cause other more subtle problems. In this situation
however, it may just do the trick, as I imagine you'll only be recording for
short amounts of time. There's only one way to find out for sure - suck it
and see.

Vasant has shown you how to get the mouse's x y coordinates. The following
code shows a way of examining the mouse buttons.

Public Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer

Public Const VK_LBUTTON = &H1
Public Const VK_MBUTTON = &H4
Public Const VK_RBUTTON = &H2

Sub Looper()
Do
Range("A1") = GetAsyncKeyState(VK_LBUTTON)
Range("B1") = GetAsyncKeyState(VK_MBUTTON)
Range("C1") = GetAsyncKeyState(VK_RBUTTON)
Loop
End Sub


As for writing your recordings to a file, there are many ways depending on
what you want. I'd probably use the techniques described here,
http://support.microsoft.com/support/excel/content/fileio/fileio.asp


When it comes to playing back your recordings, You'd use SetCursorPos to
move the mouse to the coordinates that were recorded by GetCursorPos. There
are several API calls that can click the mouse buttons, the most appropriate
would probably be mouse_event. The code below will move the mouse to a
given position then click the left mouse button.

Public Declare Function SetCursorPos Lib "user32" _
(ByVal x As Long, _
ByVal y As Long) As Long

Public Declare Sub mouse_event Lib "user32" _
(ByVal dwFlags As Long, _
ByVal dx As Long, _
ByVal dy As Long, _
ByVal cButtons As Long, _
ByVal dwExtraInfo As Long)

Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20
Public Const MOUSEEVENTF_MIDDLEUP = &H40
Public Const MOUSEEVENTF_MOVE = &H1
Public Const MOUSEEVENTF_ABSOLUTE = &H8000
Public Const MOUSEEVENTF_RIGHTDOWN = &H8
Public Const MOUSEEVENTF_RIGHTUP = &H10

Sub MoveAndClick()
SetCursorPos 500, 500
mouse_event MOUSEEVENTF_LEFTDOWN Or _
MOUSEEVENTF_LEFTUP, 0&, 0&, cButt, dwEI
End Sub


Have Fun !
Vic Eldridge

"James" <kse...@yahoo.com> wrote in message
news:94ca01c205c5$c41d0e90$35ef2ecf@TKMSFTNGXA11...

0 new messages