Is it possible to use the SendMessage API to move the mouse to a set x,y coordinates?
Are there any others ways other than...
mouse_event
SetCursorPos
Using VB6 and 2005
Cheers!
Printable View
Is it possible to use the SendMessage API to move the mouse to a set x,y coordinates?
Are there any others ways other than...
mouse_event
SetCursorPos
Using VB6 and 2005
Cheers!
SetCursorPos works fine for both but why wouldnt you want to use it?
You can also use the sendinput api If you read on MSDN they have suggested that instead of using mouse_event to use sendinput
This is my sendinput class for keys and mouse movements.
Code:Imports System.Runtime.InteropServices
Imports Microsoft.DirectX.DirectInput
Namespace WindowsAPI
Public Class User32
#Region " CONSTANTS "
' These had no type and so were objects!!
Public Const INPUT_MOUSE = 0
Public Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
Public Const MOUSEEVENTF_LEFTUP As Integer = &H4
Public Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20
Public Const MOUSEEVENTF_MIDDLEUP As Integer = &H40
Public Const MOUSEEVENTF_MOVE As Integer = &H1
Public Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000
Public Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8
Public Const MOUSEEVENTF_RIGHTUP As Integer = &H10
Public Const HC_ACTION As Integer = 0
Public Const LLKHF_EXTENDED As Integer = &H1
Public Const LLKHF_INJECTED As Integer = &H10
Public Const LLKHF_ALTDOWN As Integer = &H20
Public Const LLKHF_UP As Integer = &H80
#End Region
#Region " STRUCTURES "
<StructLayout(LayoutKind.Explicit)> _
Public Structure INPUT
<FieldOffset(0)> Dim dwType As Integer
<FieldOffset(4)> Dim mouseInput As MOUSEINPUT
<FieldOffset(4)> Dim keyboardInput As KEYBDINPUT
<FieldOffset(4)> Dim hardwareInput As HARDWAREINPUT
End Structure
<StructLayout(LayoutKind.Explicit)> _
Public Structure KEYBDINPUT
<FieldOffset(0)> Public wVk As Short
<FieldOffset(2)> Public wScan As Short
<FieldOffset(4)> Public dwFlags As KEYEVENTF
<FieldOffset(8)> Public time As Integer
<FieldOffset(12)> Public dwExtraInfo As IntPtr
End Structure
<StructLayout(LayoutKind.Explicit)> _
Public Structure HARDWAREINPUT
<FieldOffset(0)> Public uMsg As Integer
<FieldOffset(4)> Public wParamL As Short
<FieldOffset(6)> Public wParamH As Short
End Structure
<StructLayout(LayoutKind.Explicit)> _
Public Structure MOUSEINPUT
<FieldOffset(0)> Public dx As Integer
<FieldOffset(4)> Public dy As Integer
<FieldOffset(8)> Public mouseData As Integer
<FieldOffset(12)> Public dwFlags As Integer
<FieldOffset(16)> Public time As Integer
<FieldOffset(20)> Public dwExtraInfo As IntPtr
End Structure
#End Region
#Region " SUBCLASS "
Public Class HookEventArgs
Inherits EventArgs
Public HookCode As Integer
Public wParam As IntPtr
Public lParam As IntPtr
End Class
#End Region
#Region " ENUMERATIONS "
Public Enum HookType : int
WH_JOURNALRECORD = 0
WH_JOURNALPLAYBACK = 1
WH_KEYBOARD = 2
WH_GETMESSAGE = 3
WH_CALLWNDPROC = 4
WH_CBT = 5
WH_SYSMSGFILTER = 6
WH_MOUSE = 7
WH_HARDWARE = 8
WH_DEBUG = 9
WH_SHELL = 10
WH_FOREGROUNDIDLE = 11
WH_CALLWNDPROCRET = 12
WH_KEYBOARD_LL = 13
WH_MOUSE_LL = 14
End Enum
Public Enum Input_Type
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2
End Enum
<Flags()> _
Public Enum MOUSEEVENTF As Integer
MOVE = &H1
LEFTDOWN = &H2
LEFTUP = &H4
RIGHTDOWN = &H8
RIGHTUP = &H10
MIDDLEDOWN = &H20
MIDDLEUP = &H40
XDOWN = &H80
XUP = &H100
VIRTUALDESK = &H400
WHEEL = &H800
ABSOLUTE = &H8000
End Enum
<Flags()> _
Public Enum KEYEVENTF As Integer
EXTENDEDKEY = 1
KEYUP = 2
[UNICODE] = 4
SCANCODE = 8
RELEASE = 10
End Enum
Public Enum MOUSEBUTTONS
LEFT
MIDDLE
RIGHT
End Enum
#End Region
#Region " API DECLERATIONS "
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function SendInput(ByVal cInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer
End Function
#End Region
End Class
Public Class Kernal32
End Class
End Namespace
'This is from my input class
Shared Function Press_Mouse_Key(ByVal procID As Integer, ByVal MouseKey As WindowsAPI.User32.MOUSEBUTTONS) As Boolean
Dim input As WindowsAPI.User32.INPUT
input.dwType = WindowsAPI.User32.INPUT_MOUSE
'FocusGameWindow(procID)
Select Case MouseKey
Case WindowsAPI.User32.MOUSEBUTTONS.LEFT
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_LEFTDOWN
WindowsAPI.User32.SendInput(1, input, cbSize)
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_LEFTUP
WindowsAPI.User32.SendInput(1, input, cbSize)
Case WindowsAPI.User32.MOUSEBUTTONS.MIDDLE
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_MIDDLEDOWN
WindowsAPI.User32.SendInput(1, input, cbSize)
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_MIDDLEUP
WindowsAPI.User32.SendInput(1, input, cbSize)
Case WindowsAPI.User32.MOUSEBUTTONS.RIGHT
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_RIGHTDOWN
WindowsAPI.User32.SendInput(1, input, cbSize)
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_RIGHTUP
WindowsAPI.User32.SendInput(1, input, cbSize)
End Select
Return Marshal.GetLastWin32Error > 0
End Function
Shared Function Mouse_Down(ByVal ProcID As Integer, ByVal MouseKey As WindowsAPI.User32.MOUSEBUTTONS) As Boolean
Dim input As WindowsAPI.User32.INPUT
input.dwType = WindowsAPI.User32.INPUT_MOUSE
'FocusGameWindow(ProcID)
Select Case MouseKey
Case WindowsAPI.User32.MOUSEBUTTONS.LEFT
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_LEFTDOWN
WindowsAPI.User32.SendInput(1, input, cbSize)
Case WindowsAPI.User32.MOUSEBUTTONS.MIDDLE
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_MIDDLEDOWN
WindowsAPI.User32.SendInput(1, input, cbSize)
Case WindowsAPI.User32.MOUSEBUTTONS.RIGHT
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_RIGHTDOWN
WindowsAPI.User32.SendInput(1, input, cbSize)
End Select
Return Marshal.GetLastWin32Error > 0
End Function
Shared Function Mouse_Up(ByVal ProcID As Integer, ByVal MouseKey As WindowsAPI.User32.MOUSEBUTTONS) As Boolean
Dim input As WindowsAPI.User32.INPUT
input.dwType = WindowsAPI.User32.INPUT_MOUSE
'FocusGameWindow(ProcID)
Select Case MouseKey
Case WindowsAPI.User32.MOUSEBUTTONS.LEFT
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_LEFTUP
WindowsAPI.User32.SendInput(1, input, cbSize)
Case WindowsAPI.User32.MOUSEBUTTONS.MIDDLE
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_MIDDLEUP
WindowsAPI.User32.SendInput(1, input, cbSize)
Case WindowsAPI.User32.MOUSEBUTTONS.RIGHT
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_RIGHTUP
WindowsAPI.User32.SendInput(1, input, cbSize)
End Select
Return Marshal.GetLastWin32Error > 0
End Function
Shared Function MoveMouse(ByVal ProcID As Integer, ByVal X As Integer, ByVal Y As Integer) As Boolean
Dim input As WindowsAPI.User32.INPUT
input.dwType = WindowsAPI.User32.INPUT_MOUSE
input.mouseInput.dx = X * (65535 / Windows.Forms.Screen.PrimaryScreen.Bounds.Width)
input.mouseInput.dy = Y * (65535 / Windows.Forms.Screen.PrimaryScreen.Bounds.Height)
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_ABSOLUTE Or WindowsAPI.User32.MOUSEEVENTF_MOVE
WindowsAPI.User32.SendInput(1, input, cbSize)
Return Marshal.GetLastWin32Error > 0
End Function
Can't I do it with SendMessage or PostMessage? I want to send the mouse move to a specific window
I am pretty sure that mouse movents have nothing to do with the window they are on. You can move a mouse over everything, so as long as you are over a specific window when the click event is fired, it will fire on that window. What exactly are you trying to do?
thanks for the code, I can't copy and paste it because it's copying the line numbers :|
Could you stick it in a txt please?
Cheers mate
There, I changed it to code tags
oh, one more thing, I pas sin the processId that I wan to bring in to focus and use AppActivate(ProcessID) to bring it to the front. That is in my focusgamewindow function
Ok, I paste the entire code into a blank .vb file, but it underlines the Imports Microsoft.DirectX.DirectInput line and lots of others, is there something missing?
Still, why use almost 200+ lines of code when you can do it other ways so easily?
It's for a game, so SetCursorPos and mouse_event don't work normally.
I figured it out. It wouldn't let me use the ABSOLUTE flag of mouse event, so I have to move it relative to the current pos. So if I get the current pos using GetCursorPos, I can just tell it to move relativley but move negative with the value of the current pos, this always moves the cursor to 0,0. and from there I can move it to the exact x,y just by calling it relatively.
I use this class for sending keys to a game that uses directinput and for moving the mouse around on that game.Quote:
Originally Posted by RobDog888
Oh I see. I never dealt with DX. Always business type apps etc. for me. ;)