[Help] C# Move mouse while left / right button pressed. menu

User Tag List

Results 1 to 4 of 4
  1. #1
    j0achim's Avatar Active Member
    Reputation
    20
    Join Date
    Jan 2010
    Posts
    95
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Help] C# Move mouse while left / right button pressed.

    Hi guys!


    I have a small problem, for a bot i have written i have the problem that i cannot get mouse-down move to work unless the window is in focus. (PInvoke mouse_event)

    PostMessage click works.
    SendMessage click works.
    PostMessage mouse move works.
    PostMessage mouse down+move no effect. I.E painting in MS Paint. (example below)


    PostMessage to move mouse seemingly works i can have another window in focus then i see that the mouse move around in the game. So i have used Spy++ to replicate every single message the game receives when i do a manual mouse move. Which is a big soup of both PostMessage and SendMessage, but still right button down + move will not make the camera angle or character rotate.


    So I found a post on the web "Control mouse programmatically", this is a C++ snippet i rewrote it into C# (original snippet here) which will make a spiral in MS Paint. And not even this code works as intended in C# running Windows 7, x64 in VS 2010 Express. Are there anything im missing?


    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Threading;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Drawing;
    
    namespace ConsoleApplication1
    {
        class Helpers
        {
            [DllImport("user32.dll")]
            public static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
    
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
            public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, Int32 wParam, Int32 lParam);
    
            public static int MakeLParam(Point ptr)
            {
                return ((ptr.Y << 16) | (ptr.X & 0xffff));
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
    
                Process proc = Process.GetProcessesByName("mspaint").FirstOrDefault();
    
                try
                {
                    int px;
                    int py;
                    bool firstPoint = true;
    
                    for (double i = 0; i < 1; i += 0.001)
                    {
                        double rad = 10 * 2 * 3.14 * i;
                        double x = Math.Cos(rad) * 100 * i;
                        double y = Math.Sin(rad) * 100 * i;
    
                        px = (int)x + 300;
                        py = (int)y + 300;
    
                        if (firstPoint)
                        {
                            Helpers.PostMessage(proc.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN | (uint)VKeys.VK_LBUTTON, (int)0x1, (int)Helpers.MakeLParam(new Point(px, py)));
                            firstPoint = false;
                        }
                        Helpers.PostMessage(proc.MainWindowHandle, (uint)WMessages.WM_LBUTTONDOWN, (int)VKeys.VK_LBUTTON, (int)Helpers.MakeLParam(new Point(px, py)));
                        Thread.Sleep(10);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    
        #region Enumeration
        /// <summary>
        /// Virtual Keys
        /// </summary>
        public enum VKeys : int
        {
            //Keys used for gaming.
            Heal_Complete = VK_3,
            Heal_Normal = VK_4,
            Heal_Team = VK_5,
            Assist = VK_2,
            //-----END-----
            VK_LBUTTON = 0x01,   //Left mouse button 
            VK_RBUTTON = 0x02,   //Right mouse button 
            VK_CANCEL = 0x03,   //Control-break processing 
            VK_MBUTTON = 0x04,   //Middle mouse button (three-button mouse) 
            VK_BACK = 0x08,   //BACKSPACE key 
            VK_TAB = 0x09,   //TAB key 
            VK_CLEAR = 0x0C,   //CLEAR key 
            VK_RETURN = 0x0D,   //ENTER key 
            VK_SHIFT = 0x10,   //SHIFT key 
            VK_CONTROL = 0x11,   //CTRL key 
            VK_MENU = 0x12,   //ALT key 
            VK_PAUSE = 0x13,   //PAUSE key 
            VK_CAPITAL = 0x14,   //CAPS LOCK key 
            VK_ESCAPE = 0x1B,   //ESC key 
            VK_SPACE = 0x20,   //SPACEBAR 
            VK_PRIOR = 0x21,   //PAGE UP key 
            VK_NEXT = 0x22,   //PAGE DOWN key 
            VK_END = 0x23,   //END key 
            VK_HOME = 0x24,   //HOME key 
            VK_LEFT = 0x25,   //LEFT ARROW key 
            VK_UP = 0x26,   //UP ARROW key 
            VK_RIGHT = 0x27,   //RIGHT ARROW key 
            VK_DOWN = 0x28,   //DOWN ARROW key 
            VK_SELECT = 0x29,   //SELECT key 
            VK_PRINT = 0x2A,   //PRINT key
            VK_EXECUTE = 0x2B,   //EXECUTE key 
            VK_SNAPSHOT = 0x2C,   //PRINT SCREEN key 
            VK_INSERT = 0x2D,   //INS key 
            VK_DELETE = 0x2E,   //DEL key 
            VK_HELP = 0x2F,   //HELP key
            VK_OEMCOMMA = 0xBC, //OEMComma
            VK_0 = 0x30,   //0 key 
            VK_1 = 0x31,   //1 key 
            VK_2 = 0x32,   //2 key 
            VK_3 = 0x33,   //3 key 
            VK_4 = 0x34,   //4 key 
            VK_5 = 0x35,   //5 key 
            VK_6 = 0x36,    //6 key 
            VK_7 = 0x37,    //7 key 
            VK_8 = 0x38,   //8 key 
            VK_9 = 0x39,    //9 key 
            VK_A = 0x41,   //A key 
            VK_B = 0x42,   //B key 
            VK_C = 0x43,   //C key 
            VK_D = 0x44,   //D key 
            VK_E = 0x45,   //E key 
            VK_F = 0x46,   //F key 
            VK_G = 0x47,   //G key 
            VK_H = 0x48,   //H key 
            VK_I = 0x49,    //I key 
            VK_J = 0x4A,   //J key 
            VK_K = 0x4B,   //K key 
            VK_L = 0x4C,   //L key 
            VK_M = 0x4D,   //M key 
            VK_N = 0x4E,    //N key 
            VK_O = 0x4F,   //O key 
            VK_P = 0x50,    //P key 
            VK_Q = 0x51,   //Q key 
            VK_R = 0x52,   //R key 
            VK_S = 0x53,   //S key 
            VK_T = 0x54,   //T key 
            VK_U = 0x55,   //U key 
            VK_V = 0x56,   //V key 
            VK_W = 0x57,   //W key 
            VK_X = 0x58,   //X key 
            VK_Y = 0x59,   //Y key 
            VK_Z = 0x5A,    //Z key
            VK_NUMPAD0 = 0x60,   //Numeric keypad 0 key 
            VK_NUMPAD1 = 0x61,   //Numeric keypad 1 key 
            VK_NUMPAD2 = 0x62,   //Numeric keypad 2 key 
            VK_NUMPAD3 = 0x63,   //Numeric keypad 3 key 
            VK_NUMPAD4 = 0x64,   //Numeric keypad 4 key 
            VK_NUMPAD5 = 0x65,   //Numeric keypad 5 key 
            VK_NUMPAD6 = 0x66,   //Numeric keypad 6 key 
            VK_NUMPAD7 = 0x67,   //Numeric keypad 7 key 
            VK_NUMPAD8 = 0x68,   //Numeric keypad 8 key 
            VK_NUMPAD9 = 0x69,   //Numeric keypad 9 key 
            VK_SEPARATOR = 0x6C,   //Separator key 
            VK_SUBTRACT = 0x6D,   //Subtract key 
            VK_DECIMAL = 0x6E,   //Decimal key 
            VK_DIVIDE = 0x6F,   //Divide key
            VK_F1 = 0x70,   //F1 key 
            VK_F2 = 0x71,   //F2 key 
            VK_F3 = 0x72,   //F3 key 
            VK_F4 = 0x73,   //F4 key 
            VK_F5 = 0x74,   //F5 key 
            VK_F6 = 0x75,   //F6 key 
            VK_F7 = 0x76,   //F7 key 
            VK_F8 = 0x77,   //F8 key 
            VK_F9 = 0x78,   //F9 key 
            VK_F10 = 0x79,   //F10 key 
            VK_F11 = 0x7A,   //F11 key 
            VK_F12 = 0x7B,   //F12 key
            VK_SCROLL = 0x91,   //SCROLL LOCK key 
            VK_LSHIFT = 0xA0,   //Left SHIFT key
            VK_RSHIFT = 0xA1,   //Right SHIFT key
            VK_LCONTROL = 0xA2,   //Left CONTROL key
            VK_RCONTROL = 0xA3,    //Right CONTROL key
            VK_LMENU = 0xA4,      //Left MENU key
            VK_RMENU = 0xA5,   //Right MENU key
            VK_PLAY = 0xFA,   //Play key
            VK_ZOOM = 0xFB, //Zoom key 
        }
    
        public enum MKMessages : int
        {
            MK_LBUTTON = 0x0001
        }
    
        ///<summary>
        /// Virtual Messages
        /// </summary>
        public enum WMessages : int
        {
            WM_SETCURSOR = 0x0020,
            WM_MOUSEMOVE = 0x200, //Simulate mouse move.
            WM_MOUSEHOVER = 0x2A1,
            WM_ACTIVATE = 0x021,
            WM_LBUTTONDOWN = 0x201, //Left mousebutton down
            WM_LBUTTONUP = 0x202,  //Left mousebutton up
            WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
            WM_RBUTTONDOWN = 0x204, //Right mousebutton down
            WM_RBUTTONUP = 0x205,   //Right mousebutton up
            WM_RBUTTONDBLCLK = 0x206, //Right mousebutton doubleclick
            WM_KEYDOWN = 0x100,  //Key down
            WM_KEYUP = 0x101,   //Key up
            WM_CHAR = 0x105,
        }
    
        ///summary>
        /// Virtual Mouse Event Flags
        /// </summary>
        public enum MouseEventFlags : uint
        {
            LEFTDOWN = 0x00000002,
            LEFTUP = 0x00000004,
            MIDDLEDOWN = 0x00000020,
            MIDDLEUP = 0x00000040,
            MOVE = 0x00000001,
            ABSOLUTE = 0x00008000,
            RIGHTDOWN = 0x00000008,
            RIGHTUP = 0x00000010,
            WHEEL = 0x00000800,
            XDOWN = 0x00000080,
            XUP = 0x00000100
        }
        #endregion
    }
    Last edited by j0achim; 10-01-2012 at 03:08 AM.

    [Help] C# Move mouse while left / right button pressed.
  2. #2
    hp94's Avatar Legendary
    Reputation
    742
    Join Date
    Nov 2009
    Posts
    587
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:

    Code:
    #region mouse
            //this allows virtual mouse event creation and win32 api calls
            [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    
            //creating virtual mouse events here
            public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
    
            private const uint MOUSEEVENTF_MOVE = 0x0001;
            private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
            private const uint MOUSEEVENTF_LEFTUP = 0x0004;
            private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
            private const uint MOUSEEVENTF_RIGHTUP = 0x0010;
            private const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
            private const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
            private const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
    
            public static void DoMouseClick()
            {
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            }
    
            public static void MoveMouse(uint xDelta, uint yDelta)
            {
                mouse_event(MOUSEEVENTF_MOVE, xDelta, yDelta, 0, 0);
            }
    
            public static void MoveMouseTo(uint x, uint y)
            {
                mouse_event(MOUSEEVENTF_ABSOLUTE, x, y, 0, 0);
                mouse_event(MOUSEEVENTF_MOVE, x, y, 0, 0);
            }
    The way to use this (or use your code as is) is to only send the mouse down event until you need the release, after which you send mouse up. For mine it would look like:

    Code:
    MoveMouse(1,1);
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    MoveMouse(100,100);
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

  3. #3
    j0achim's Avatar Active Member
    Reputation
    20
    Join Date
    Jan 2010
    Posts
    95
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks hp94, unfortunately mouse_event is what i use pr.today, meaning that on one computer i can only run 1 bot at once. Tho i am not not taking into consideration virtual machines, game is DX7 so single instances take up pretty much 90% of machine resources so that's not a viable solution, it may sound weird but its true, due to the nature of DX7 combined with modern hardware they just don't go along. (When virtualized at least)

    I guess I'm stuck trying to figure out how to inject rotation for the character, that said im sure its now already taken me 2 years to learn how to efficiently relocate the offsets i need for my bot after each game update. Luckily for me the game i have written this bot for rarely change offsets. And when they do i am now able to locate new offsets in matter of 15~25 minutes. But injection is a whole new play-field for me :/

  4. #4
    j0achim's Avatar Active Member
    Reputation
    20
    Join Date
    Jan 2010
    Posts
    95
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Been putting in a lot of effort into my project lately, making it a solid foundation to work with, however i'm totally stuck on this mouse moving part. Since its pretty much the last piece of the puzzle to make this "bot" fully capable of running in the background.


    If anyone have any ideas or clues, or can point me in a direction, or alternative ways to achieve same goal, i would really appreciate it!

Similar Threads

  1. New to AutoIT need help with random mouse clicks
    By Eaassas in forum Diablo 3 Bots Questions & Requests
    Replies: 6
    Last Post: 06-11-2012, 12:16 PM
  2. [Help] Keyclone and mouse
    By DaigonoYouso in forum World of Warcraft General
    Replies: 0
    Last Post: 12-26-2010, 12:04 PM
  3. [Bug] Move around while sitting down
    By Spatz21 in forum Age of Conan Exploits|Hacks
    Replies: 9
    Last Post: 07-05-2008, 08:08 AM
  4. Curel Barb > Warglaves of Azinoth Left+Right
    By Helmhammer in forum WoW ME Questions and Requests
    Replies: 6
    Last Post: 10-21-2007, 01:15 PM
All times are GMT -5. The time now is 03:45 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search