#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN using System; using System.Runtime.InteropServices; using Starpelly.Enums.Windows; using System.Text; namespace Starpelly.OperatingSystem { public class Windows : IOperatingSystem { /// /// Gets the current title of the game window. /// /// The current title the window is, will probably only be used for changing the window in ChangeWindowTitle() private static string GetActiveWindowTitle() { const int nChars = 256; StringBuilder Buff = new StringBuilder(nChars); IntPtr handle = User32.GetForegroundWindow(); if (User32.GetWindowText(handle, Buff, nChars) > 0) { return Buff.ToString(); } return null; } /// /// Changes the game's window title. /// /// The title the window will be changed to. public void ChangeWindowTitle(string newTitle) { var windowPtr = User32.FindWindow(null, GetActiveWindowTitle()); User32.SetWindowText(windowPtr, newTitle); } #region Input /// /// Simulates a real key press passed in. /// public static void KeyPress(KeyCodeWin keyCode) { INPUT input = new INPUT { type = SendInputEventType.InputKeyboard, mkhi = new MOUSEANDKEYBOARDINPUT { ki = new KEYBOARDINPUT { wVk = (ushort)keyCode, wScan = 0, dwFlags = 0, // if nothing, key down time = 0, dwExtraInfo = IntPtr.Zero, } } }; INPUT input2 = new INPUT { type = SendInputEventType.InputKeyboard, mkhi = new MOUSEANDKEYBOARDINPUT { ki = new KEYBOARDINPUT { wVk = (ushort)keyCode, wScan = 0, dwFlags = 2, // key up time = 0, dwExtraInfo = IntPtr.Zero, } } }; INPUT[] inputs = new INPUT[] { input, input2 }; // Combined, it's a keystroke User32.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT))); } /// /// Sets your mouse pointer. /// public static void ChangeCursor(WindowsCursor cursor) { User32.SetCursor(User32.LoadCursor(IntPtr.Zero, (int)cursor)); } /// /// Immediately clicks the left mouse button. /// public static void ClickLeftMouseButton() { INPUT mouseDownInput = new INPUT(); mouseDownInput.type = SendInputEventType.InputMouse; mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_LEFTDOWN; User32.SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT())); INPUT mouseUpInput = new INPUT(); mouseUpInput.type = SendInputEventType.InputMouse; mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_LEFTUP; User32.SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT())); } /// /// Immediately clicks the right mouse button. /// public static void ClickRightMouseButton() { INPUT mouseDownInput = new INPUT(); mouseDownInput.type = SendInputEventType.InputMouse; mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_RIGHTDOWN; User32.SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT())); INPUT mouseUpInput = new INPUT(); mouseUpInput.type = SendInputEventType.InputMouse; mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_RIGHTUP; User32.SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT())); } /// /// Moves your cursor in the x and y params implemented, plus the current mouse pos. /// /// Direction X /// Direction Y public static void MouseMove(int dx, int dy) { INPUT mouseMove = new INPUT(); mouseMove.type = SendInputEventType.InputMouse; mouseMove.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_MOVE; mouseMove.mkhi.mi.dx = dx; mouseMove.mkhi.mi.dy = dy; User32.SendInput(1, ref mouseMove, Marshal.SizeOf(new INPUT())); } #endregion } } #endif