Using SendMessage to Simulate Mouse Clicks

What I need

Simulate messages (mostly mouse, but keyboard too) without moving the cursor . The message goes to the focused window (I want the windows to be minimized first, but I haven't found how to do that, so I use SetForegroundWindow

to focus it).

What i found

In this question, I realized that I need to use pInvoked from user32.dll

. Also I found a small sample code, but it didn't work.

I also found this similar question. The guy is using mouse_event

but it is outdated. This feature takes X and Y crusts in mickey. I tried converting my coordinates (using SendMessage

) but I failed.

An example of Icanhaz?

Sure. It will open notepad.exe

and I need to right click on the button located at (1210, 460).

What i tried

Based on the sample I found here, I made this code:

IntPtr hWnd = (IntPtr)FindWindow("notepad.exe", null);
SetForegroundWindow(hWnd);

var screenPoint = this.PointToScreen(new Point(1210, 460));
var handle = WindowFromPoint(screenPoint);

if (handle != IntPtr.Zero)
{
    //Right Button Down
    SendMessage(handle, 0x0204, IntPtr.Zero, IntPtr.Zero);
    //Right Button Up
    SendMessage(handle, 0x0205, IntPtr.Zero, IntPtr.Zero);
}

      

I also tried using my previous handle hWnd

in SendMessage

, but didn't work. You can find all the code here .

Thanks in advance.

+3


source to share


3 answers


Raymond Chen wrote a blog post just for you: You cannot simulate keyboard input with PostMessage . I know you want a mouse, not keys, and you are using Send not Post, but the exact same problems apply to you. Read the link. Fortunately, it also contains a solution: use SendInput

.



Synthesizes keystrokes, mouse movements, and button presses.

+3


source


This is some kind of mouse and keyboard manipulator made when I was younger to find the color on the game screen and right click + option: P

Maybe some code can help you, even if it's in French ...

        using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Abyte0
    {
        public partial class ClavierVirtuel
        {
            [DllImport("user32.dll", EntryPoint = "FindWindow")]
            private static extern int FindWindow(string _ClassName, string _WindowName);

            [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
            private static extern int SetForegroundWindow(int hwnd);

            //int handle = FindWindow(null, "Facebook - Windows Internet Explorer");

            //Give focus to the screen with the wanted name
            public static void DonnerFocus(string pNomFenetre)


    {
            //Get the handle of the app you want to send keys to
            int handle = FindWindow(null, pNomFenetre);

            //Set it to the foreground
            SetForegroundWindow(handle);
        }

        //write the string
        public static void Ecrire(string pPhrase)
        {
            //Send the keys on over
            SendKeys.SendWait(pPhrase);
        }

        //write a string and press enter
        public static void ecrire_Enter(string pPhrase)
        {
            foreach (char lettre in pPhrase)
            {
                SendKeys.SendWait(lettre.ToString());
            }
            System.Threading.Thread.Sleep(10);
            SendKeys.SendWait("{ENTER}");
        }
    }
}

      



and

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace Abyte0
{
    static class MouseHandler
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool SetCursorPos(int X, int Y);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;


        public static void moveMouse(ref int currentx, ref int currenty, string whattodo, int pNombre)
        {
            switch (whattodo)
            {
                case "addX":
                    for (int i = 0; i < pNombre; i++)
                    {
                        currentx++;
                        SetCursorPos(currentx + Form1.m_Border_x, currenty + Form1.m_Border_y);
                    }
                    break;
                case "addY":
                    for (int i = 0; i < pNombre; i++)
                    {
                        currenty++;
                        SetCursorPos(currentx + Form1.m_Border_x, currenty + Form1.m_Border_y);
                    }
                    break;
                case "remX":
                    for (int i = 0; i < pNombre; i++)
                    {
                        currentx--;
                        SetCursorPos(currentx + Form1.m_Border_x, currenty + Form1.m_Border_y);
                    }
                    break;
                case "remY":
                    for (int i = 0; i < pNombre; i++)
                    {
                        currenty--;
                        SetCursorPos(currentx + Form1.m_Border_x, currenty + Form1.m_Border_y);
                    }
                    break;
                default:
                    break;
            }
        }
        #region Mouse Left, Right1, Right2 Clicks
        public static void DoMouseLeftClick(int nx, int ny)
        {
            Random objRandom = new Random();
            SetCursorPos(nx + Form1.m_Border_x, ny + Form1.m_Border_y);
            mouse_event(MOUSEEVENTF_LEFTDOWN, nx + Form1.m_Border_x, ny + Form1.m_Border_y, 0, 0);
            Thread.Sleep(objRandom.Next(1, 332));
            mouse_event(MOUSEEVENTF_LEFTUP, nx + Form1.m_Border_x, ny + Form1.m_Border_y, 0, 0);
           // Thread.Sleep(objRandom.Next(objRandom.Next(10, objRandom.Next(180, 600)), objRandom.Next(objRandom.Next(666, 4000), 5102)));
            Handler.getFocus();
        }

        public static void DoMouseLeftClick(int[] pTab)
        {
            int nx = pTab[0];
            int ny = pTab[1];
            Random objRandom = new Random();
            SetCursorPos(nx + Form1.m_Border_x, ny + Form1.m_Border_y);
            mouse_event(MOUSEEVENTF_LEFTDOWN, nx + Form1.m_Border_x, ny + Form1.m_Border_y, 0, 0);
            Thread.Sleep(objRandom.Next(1, 332));
            mouse_event(MOUSEEVENTF_LEFTUP, nx + Form1.m_Border_x, ny + Form1.m_Border_y, 0, 0);
          //  Thread.Sleep(objRandom.Next(objRandom.Next(10, objRandom.Next(180, 600)), objRandom.Next(objRandom.Next(666, 4000), 5102)));
            Handler.getFocus();
        }

        public static void DoMouseRightClickOp1(int nx, int ny)
        {
            Random objRandom = new Random();
            SetCursorPos(nx + Form1.m_Border_x, ny + Form1.m_Border_y);
            mouse_event(MOUSEEVENTF_RIGHTDOWN, nx + Form1.m_Border_x, ny + Form1.m_Border_y, 0, 0);
            Thread.Sleep(objRandom.Next(6, 237));
            mouse_event(MOUSEEVENTF_RIGHTUP, nx + Form1.m_Border_x, ny + Form1.m_Border_y, 0, 0);
            Handler.getFocus();
            Thread.Sleep(objRandom.Next(1, 332));
            moveMouse(ref nx, ref ny, "addY", 20);
            DoMouseLeftClick(nx, ny);
        }


        public static void DoMouseRightClickOp2(int nx, int ny)
        {
            Random objRandom = new Random();
            SetCursorPos(nx + Form1.m_Border_x, ny + Form1.m_Border_y);
            mouse_event(MOUSEEVENTF_RIGHTDOWN, nx + Form1.m_Border_x, ny + Form1.m_Border_y, 0, 0);
            Thread.Sleep(objRandom.Next(6, 237));
            mouse_event(MOUSEEVENTF_RIGHTUP, nx + Form1.m_Border_x, ny + Form1.m_Border_y, 0, 0);
            Handler.getFocus();
            Thread.Sleep(objRandom.Next(1, 332));
            moveMouse(ref nx, ref ny, "addY", 25);
            DoMouseLeftClick(nx, ny);
        }
        #endregion


        public static void DoSimpleClickNoFocus(int x,int y)
        {
            Random objRandom = new Random();
            SetCursorPos(x,y);
            mouse_event(MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0);
            Thread.Sleep(objRandom.Next(7, 258));
            mouse_event(MOUSEEVENTF_LEFTUP, x,y, 0, 0);
        }

    }
}

      

+2


source


If I understand correctly, you want to inject a click event inside the application without moving the mouse like a game bot, so the user doesn't see that the summation is using his mouse and keyboard ...

If im right then take a look at the answer on this link, this might be what you want ...

its in C ++, but not a very big question I think ...

http://social.msdn.microsoft.com/Forums/en-SG/vcgeneral/thread/e0071733-286a-4b4d-b294-685f8a788fb8

+2


source







All Articles