Results 1 to 13 of 13

Thread: [RESOLVED] SendMessage API to move mouse?

  1. #1

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Resolved [RESOLVED] SendMessage API to move mouse?

    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!
    Chris

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: SendMessage API to move mouse?

    SetCursorPos works fine for both but why wouldnt you want to use it?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: SendMessage API to move mouse?

    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
    Last edited by bmahler; May 10th, 2007 at 11:30 AM.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  4. #4

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: SendMessage API to move mouse?

    Can't I do it with SendMessage or PostMessage? I want to send the mouse move to a specific window
    Chris

  5. #5
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: SendMessage API to move mouse?

    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?
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  6. #6

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: SendMessage API to move mouse?

    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
    Chris

  7. #7
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: SendMessage API to move mouse?

    There, I changed it to code tags
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  8. #8
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: SendMessage API to move mouse?

    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
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  9. #9

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: SendMessage API to move mouse?

    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?
    Chris

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: SendMessage API to move mouse?

    Still, why use almost 200+ lines of code when you can do it other ways so easily?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: SendMessage API to move mouse?

    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.
    Chris

  12. #12
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: SendMessage API to move mouse?

    Quote Originally Posted by RobDog888
    Still, why use almost 200+ lines of code when you can do it other ways so easily?
    I use this class for sending keys to a game that uses directinput and for moving the mouse around on that game.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [RESOLVED] SendMessage API to move mouse?

    Oh I see. I never dealt with DX. Always business type apps etc. for me.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width