-
Sep 13th, 2013, 09:31 AM
#1
Thread Starter
Member
Mouse Move and Click with Windows API Function SendInput
Dear folks, I need your help on this.
I need to move the mouse pointer to a give x,y coordinate, and then perform a click.
I've found several samples of how to use Windows Api "SendInput" function in order to do so, but they're all in C++/C#, which I can't straightly understand. Nevertheless, I've made a try of adapting those (code below), and it successfully moves the pointer, but doesn't perform the click. Can anyone help me to find the flaw?
Thank you very much!
vb Code:
Private Sub Test()
MouseMove(170, 170)
Application.DoEvents()
MouseLeftClick()
End Sub
Public Shared Sub MouseMove(ByVal x As Integer, ByVal y As Integer)
Dim fx = x * (65535.0# / (GetSystemMetrics(SystemMetric.SM_CXSCREEN) - 1))
Dim fy = y * (65535.0# / (GetSystemMetrics(SystemMetric.SM_CYSCREEN) - 1))
Dim it As New INPUT
it.dwType = INPUT_MOUSE
it.mkhi.mi.dwFlags = MOUSEEVENTF_MOVE Or MOUSEEVENTF_ABSOLUTE
it.mkhi.mi.dx = fx
it.mkhi.mi.dy = fy
SendInput(1, it, Marshal.SizeOf(it))
End Sub
Public Shared Sub MouseLeftClick()
Dim it As New INPUT
it.dwType = INPUT_MOUSE
it.mkhi.mi.dwFlags = MOUSEEVENTF_LEFTDOWN
SendInput(1, it, Marshal.SizeOf(it))
End Sub
Private Structure INPUT
Dim dwType As Integer
Dim mkhi As MOUSEKEYBDHARDWAREINPUT
End Structure
<StructLayout(LayoutKind.Explicit)> _
Private Structure MOUSEKEYBDHARDWAREINPUT
<FieldOffset(0)> Public mi As MOUSEINPUT
<FieldOffset(0)> Public ki As KEYBDINPUT
<FieldOffset(0)> Public hi As HARDWAREINPUT
End Structure
Private Structure MOUSEINPUT
Public dx As Integer
Public dy As Integer
Public mouseData As Integer
Public dwFlags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
Private Structure KEYBDINPUT
Public wVk As Short
Public wScan As Short
Public dwFlags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
Private Structure HARDWAREINPUT
Public uMsg As Integer
Public wParamL As Short
Public wParamH As Short
End Structure
Const INPUT_MOUSE As UInt32 = 0
Const INPUT_KEYBOARD As Integer = 1
Const INPUT_HARDWARE As Integer = 2
Const XBUTTON1 As UInt32 = &H1
Const XBUTTON2 As UInt32 = &H2
Const MOUSEEVENTF_MOVE As UInt32 = &H1
Const MOUSEEVENTF_LEFTDOWN As UInt32 = &H2
Const MOUSEEVENTF_LEFTUP As UInt32 = &H4
Const MOUSEEVENTF_RIGHTDOWN As UInt32 = &H8
Const MOUSEEVENTF_RIGHTUP As UInt32 = &H10
Const MOUSEEVENTF_MIDDLEDOWN As UInt32 = &H20
Const MOUSEEVENTF_MIDDLEUP As UInt32 = &H40
Const MOUSEEVENTF_XDOWN As UInt32 = &H80
Const MOUSEEVENTF_XUP As UInt32 = &H100
Const MOUSEEVENTF_WHEEL As UInt32 = &H800
Const MOUSEEVENTF_VIRTUALDESK As UInt32 = &H4000
Const MOUSEEVENTF_ABSOLUTE As UInt32 = &H8000
<DllImport("user32.dll")> Private Shared Function GetSystemMetrics(ByVal smIndex As Integer) As Integer
End Function
<DllImport("user32.dll")> Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer
End Function
Public Enum SystemMetric As Integer
''' <summary>
''' Width of the screen of the primary display monitor in pixels. This is the same values obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor HORZRES).
''' </summary>
SM_CXSCREEN = 0
''' <summary>
''' Height of the screen of the primary display monitor in pixels. This is the same values obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor VERTRES).
''' </summary>
SM_CYSCREEN = 1
End Enum
Last edited by VBobCat; Sep 13th, 2013 at 10:15 AM.
Reason: code lacking
VBobCat
VS2013 & Office-VBA
Autodidact, part-time programmer for job needs
-
Sep 13th, 2013, 03:03 PM
#2
Re: Mouse Move and Click with Windows API Function SendInput
Try mouse_event instead. Does it all in one go with far fewer complications.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Sep 13th, 2013, 05:10 PM
#3
Thread Starter
Member
Re: Mouse Move and Click with Windows API Function SendInput
 Originally Posted by dunfiddlin
Try mouse_event instead. Does it all in one go with far fewer complications.
Yes, I'll try and learn to do that, for you've helped me before and I trust your advice. And Pinvoke.net sure is the place to go first. But...
MSDN states that "This function has been superseded. Use SendInput instead.". What is this but a mislead?
Besides of all that, why can VB.NET be such a marvelous tool to code fast and straightforward as you learn more and more about it, and at same time be so puny when interacting with its own MS-brand operating system and ActiveX components? Shouldn't it have most of those API functions wrapped in managed classes?
I say that because it's quite frustrating that a simple "Alert()" javascript that occasionally pops from the document exhibited by WebBrowser control raises no event through which we could properly handle it, and you'll need a timer and some API functions cleverly used (as seen here) simply to close it. It gets worse if you need to answer it "Ok", and worser if the answer needs to be whatever else.
That said, I thank you very much for the advice. I'll post back my resulting code as soon as I have a result, which I hope is positive.
VBobCat
VS2013 & Office-VBA
Autodidact, part-time programmer for job needs
-
Sep 13th, 2013, 06:02 PM
#4
Re: Mouse Move and Click with Windows API Function SendInput
What is this but a mislead?
Depends how you look at it. Technically it is true that the new method does supersede but at the same time the old one is still available and it works so why not use it? And it does not require any marshalling which makes it the better choice for VB anyway.
Shouldn't it have most of those API functions wrapped in managed classes?
Well strictly speaking VB is an abstraction from the Windows API not a complete replacement for it. The API represents the language that the OS speaks across the board whilst VB (and the other .Net languages) are more constrained in intent and operation. Having said that the coverage of API functions is really quite comprehensive. It is only a few slightly unusual circumstances in which you are required to use the API directly, most often for operations that take place outside the application window or when it does not have focus. You can of course move the cursor in managed code ...
Cursor.Position = New Point(100, 100) (NB. these are screen co-ordinates not form or control co-ordinates)
... it's just clicking which requires API.
I say that because it's quite frustrating that a simple "Alert()" javascript that occasionally pops from the document exhibited by WebBrowser control raises no event through which we could properly handle it
I understand the frustration but javascript is platform independent and works entirely within the browser environment. It has no direct interaction with the OS from which to raise such an event.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Sep 14th, 2013, 01:59 PM
#5
Thread Starter
Member
[SOLVED] Re: Mouse Move and Click with Windows API Function SendInput
Then, again, it was my fault not to see that everything that goes down must come up, and both keyboard keys and mouse buttons must have a pair of press and release messages in order to simulate the user movement. That is, the correct sub MouseLeftClick stays so:
vb Code:
Public Shared Sub MouseLeftClick() Dim it As New INPUT it.dwType = INPUT_MOUSE it.mkhi.mi.dwFlags = MOUSEEVENTF_LEFTDOWN SendInput(1, it, Marshal.SizeOf(it)) Application.DoEvents() it = New INPUT it.dwType = INPUT_MOUSE it.mkhi.mi.dwFlags = MOUSEEVENTF_LEFTUP SendInput(1, it, Marshal.SizeOf(it)) End Sub
VBobCat
VS2013 & Office-VBA
Autodidact, part-time programmer for job needs
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|