using System; using System.Drawing; using System.Collections.Generic; public static class VoiceBotScript { private const string ScriptSettingName = "relative_position_driving_mode"; // The threshold of the mouse movement, in pixels private const int Threshold = 5; // The speed at which the script runs. Each loop will wait for // this many milliseconds (250 is 4 times a second) private const int IntervalMS = 250; // An enum to help keep track of the direction private enum DirectionEnum { None = 0, Left = 1, Right = 2 } // A dictionary to map the direction to the key. For Left/Right arrow use "{LEFT}" and "{RIGHT}", for // A and D use "A" and "D" private static Dictionary KeyMap = new Dictionary() { {DirectionEnum.Left, "A"}, {DirectionEnum.Right, "D"}, }; private static DirectionEnum CurrentDirection = DirectionEnum.None; private static int LastMousePosition = 0; public static void Run(IntPtr windowHandle) { // If the script is running, this will set the script to // stop running, and then exit if (BFS.ScriptSettings.ReadValueBool(ScriptSettingName)) { BFS.ScriptSettings.WriteValueBool(ScriptSettingName, false); return; } // If we got this far, that means the script is running BFS.ScriptSettings.WriteValueBool(ScriptSettingName, true); // Setup our initial values LastMousePosition = BFS.Input.GetMousePositionX(); // Run the loop while the script should be running while (BFS.ScriptSettings.ReadValueBool(ScriptSettingName)) { // Check how far the mouse is now int x = BFS.Input.GetMousePositionX(); int delta = x - LastMousePosition; // The mouse didn't move enough. Let go of any keys and continue if(Math.Abs(delta) < Threshold) { // Let go of any held down keys if(CurrentDirection != DirectionEnum.None) BFS.Input.SendKeyUp(KeyMap[CurrentDirection]); // Update the values CurrentDirection = DirectionEnum.None; LastMousePosition = x; // Wait for the specified time BFS.General.ThreadWait(IntervalMS); continue; } // The mouse moved left if(delta < 0) { // If no keys are pressed just press the left key if(CurrentDirection == DirectionEnum.None) BFS.Input.SendKeyDown(KeyMap[DirectionEnum.Left]); // If the right key is held down let it go and press the left key if(CurrentDirection == DirectionEnum.Right) { BFS.Input.SendKeyUp(KeyMap[DirectionEnum.Right]); BFS.Input.SendKeyDown(KeyMap[DirectionEnum.Left]); } // Keep track of the pressed key CurrentDirection = DirectionEnum.Left; } // The mouse moved right if(delta > 0) { // If no keys are pressed just press the right key if(CurrentDirection == DirectionEnum.None) BFS.Input.SendKeyDown(KeyMap[DirectionEnum.Right]); // If the left key is held down let it go and press the right key if(CurrentDirection == DirectionEnum.Left) { BFS.Input.SendKeyUp(KeyMap[DirectionEnum.Left]); BFS.Input.SendKeyDown(KeyMap[DirectionEnum.Right]); } // Keep track of the pressed key CurrentDirection = DirectionEnum.Right; } // Update the values LastMousePosition = x; // Wait for the specified time BFS.General.ThreadWait(IntervalMS); } // If we got this far that means the script has ended // Let go of any held down keys if(CurrentDirection != DirectionEnum.None) BFS.Input.SendKeyUp(KeyMap[CurrentDirection]); // Update the values CurrentDirection = DirectionEnum.None; } }