Docs / C# Projects/Custom Mouse-Macro Tool

Custom Mouse-Macro Tool

C# ProjectsC#.NETNewtonsoft.json

Overview

Tool which lets people create custom mouse macros, which can then be dynamically loaded and used whenever it's needed. Creatd macros are stored via .json files, and can be retrieved and edited at any given time.

Key Systems & Features

  • Macro creator
  • Json serialization/deserialization
  • Mouse movement

Code Examples

Below are some code snippets from this project.

Mouse mover
CSHARP
[DllImport("user32.dll")]
        private static extern short GetAsyncKeyState(Keys vKey);

        [DllImport("user32.dll")]
        static extern void mouse_event(int dwFlags, int dx, int dy, uint dwData, UIntPtr dwExtraInfo);

        public static void RecoilWorker()
        {
            while (true)
            {
                if (GetAsyncKeyState(Keys.LButton) < 0 && GetAsyncKeyState(Keys.RButton) < 0)
                {
                    while (GetAsyncKeyState(Keys.LButton) < 0)
                    {
                        for (int i = 0; i < 1; i++)
                        {
                            if(SessionData.IsBuilding == false && SessionData.LoadedMacro != null)
                            {
                                mouse_event(0x0001, SessionData.LoadedMacro.XValue, SessionData.LoadedMacro.YValue, 0, UIntPtr.Zero);
                                Thread.Sleep(SessionData.LoadedMacro.Speed);
                            }
                            else if(SessionData.IsBuilding == true)
                            {
                                mouse_event(0x0001, SessionData.BuilderX, SessionData.BuilderY, 0, UIntPtr.Zero);
                                Thread.Sleep(SessionData.BuilderSpeed);
                            }
                        }
                    }
                }
                Thread.Sleep(1);
            }
        }
Load session data
CSHARP
public static void LoadSessionData(MainForm form)
        {
            if (File.Exists(AppContext.BaseDirectory + @"SessionDetails.json"))
            {
                dynamic sessionData = JsonConvert.DeserializeObject(File.ReadAllText(AppContext.BaseDirectory + @"SessionDetails.json"));

                foreach(JObject obj in sessionData)
                {
                    string path = Convert.ToString(obj["Location"]);
                    Keys keybind = (Keys)Enum.Parse(typeof(Keys), obj["Keybind"].ToString());

                    if (File.Exists(path))
                    {
                        Macro loadedMacro = new Macro();
                        dynamic macFile = JsonConvert.DeserializeObject(File.ReadAllText(path));

                        loadedMacro.XValue = Convert.ToInt32(macFile["X"]);
                        loadedMacro.YValue = Convert.ToInt32(macFile["Y"]);
                        loadedMacro.MacroName = Path.GetFileNameWithoutExtension(path);
                        loadedMacro.Speed = Convert.ToInt32(macFile["Speed"]);
                        loadedMacro.Keybind = keybind;
                        loadedMacro.FileLocation = path;

                        form.CreateButtons(loadedMacro);
                    }
                }
            }
        }

Links