Question

I'm a big fan of automation, so whenever I'm given the chance I like to try and automate tasks that I have to repeat. The following code usually allows me to click the mouse wherever it is on the screen (and in whatever application that may be):

Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long

Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long

Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (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 ' left button down

Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down

Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up

Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down

Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up

This usually works no problem. I've used it in a range of applications to great effect. However I'm now trying to do some mouse clicking (and key stroke sending) to an application that just seems to completely ignore the mouse and keyboard commands. Once the mouse has move into the area of the screen covered by the program it doesn't move, click etc, but it I move the mouse away manually it continue with the process as if it had been working correctly.

So is there any other way to control the mouse programmatically, that would simulate the mouse in a way that is indistinguishable from me moving it about by hand?

Was it helpful?

Solution

What version of windows are you using?

If you are using Vista+, is the target application running with elevated permissions?

If yes, is your application also running with elevated permissions?

If no, see : UIPI

Newer versions of Windows actively disallow an application with a lower privilege sending messages to an application with higher privilege. There are a few ways around this, either by making your application run with elevated permissions or by including an application manifest that sets uiAccess to true. In the latter case, the application must be authenticode signed and must be executed from a trusted location (ie: Program Files directory, etc.).

Otherwise, there is no way to simulate mouse activity at the low level without introducing a new hardware driver. Hardware drivers run in Ring-0 and to completely simulate real mouse activity (not just 'fake' messages) you would need to do it at this level.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top