public VirtualKeyCode Get(Input input, int player) { if (input == Input.Back) { return ResolvePlayerBackwardInput(player); } else if (input == Input.Forward) { return ResolvePlayerForwardInput(player); } else if (PlayerOneInputMap.ContainsKey(input)) { if (player == 1) { return PlayerOneInputMap[input]; } else { return PlayerTwoInputMap[input]; } } else { throw new KeyNotFoundException("Cannot find input key for " + input); } }
public VirtualKeyCode get(Input input) { if (input == Input.P1_BK) { return getP1_BK(); } else if (input == Input.P1_FW) { return getP1_FW(); } else if (inputMap.ContainsKey(input)) { return inputMap[input]; } else { throw new KeyNotFoundException("Cannot find input key for " + input); } }
static extern UInt32 SendInput(UInt32 numberOfInputs, Input[] inputs, Int32 sizeOfInputStructure);
/// <summary> /// Calls the Win32 SendInput method with a stream of KeyDown and KeyUp messages in order to simulate uninterrupted text entry via the keyboard. /// </summary> /// <param name="text">The text to be simulated.</param> public static void SimulateTextEntry(string text) { if (text.Length > UInt32.MaxValue / 2) throw new ArgumentException(string.Format("The text parameter is too long. It must be less than {0} characters.", UInt32.MaxValue / 2), "text"); var chars = Encoding.ASCII.GetBytes(text); var len = chars.Length; Input[] inputList = new Input[len * 2]; for (int x = 0; x < len; x++) { UInt16 scanCode = chars[x]; var down = new Input { Type = (UInt32) InputType.Keyboard, Data = { Keyboard = new KeyboardInput { Vk = 0, Scan = scanCode, Flags = (UInt32) KeyboardFlag.Unicode, Time = 0, ExtraInfo = IntPtr.Zero } } }; var up = new Input { Type = (UInt32) InputType.Keyboard, Data = { Keyboard = new KeyboardInput { Vk = 0, Scan = scanCode, Flags = (UInt32) (KeyboardFlag.KeyUp | KeyboardFlag.Unicode), Time = 0, ExtraInfo = IntPtr.Zero } } }; // Handle extended keys: // If the scan code is preceded by a prefix byte that has the value 0xE0 (224), // we need to include the KEYEVENTF_EXTENDEDKEY flag in the Flags property. if ((scanCode & 0xFF00) == 0xE000) { down.Data.Keyboard.Flags |= (UInt32)KeyboardFlag.ExtendedKey; up.Data.Keyboard.Flags |= (UInt32)KeyboardFlag.ExtendedKey; } inputList[2*x] = down; inputList[2*x + 1] = up; } SendInput((UInt32)len*2, inputList, Marshal.SizeOf(typeof(Input))); }
/// <summary> /// Calls the Win32 SendInput method to simulate a Key UP. /// </summary> /// <param name="keyCode">The VirtualKeyCode to lift up</param> public static void SimulateKeyUp(VirtualKeyCode keyCode) { var up = new Input { Type = (UInt32) InputType.Keyboard, Data = { Keyboard = new KeyboardInput { Vk = (UInt16) keyCode, Scan = GetScanCode(keyCode), Flags = (UInt32) KeyboardFlag.KeyUp, Time = 0, ExtraInfo = IntPtr.Zero } } }; Input[] inputList = new Input[1]; inputList[0] = up; var numberOfSuccessfulSimulatedInputs = SendInput(1, inputList, Marshal.SizeOf(typeof(Input))); if(numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The key up simulation for {0} was not successful.", keyCode)); }
/// <summary> /// Calls the Win32 SendInput method with a ClickDown and ClickUp message in the same input sequence in order to simulate a Key PRESS. /// </summary> /// <param name="clickDown">Click down info</param> /// <param name="clickUp">Click up info</param> /// <param name="mouseData">Additional data.</param> /// <param name="x">X coordinate.</param> /// <param name="y">Y coordinate.</param> public static void SimulateClickPress(MouseFlag clickDown, MouseFlag clickUp, int mouseData, int x = 0, int y = 0) { var down = new Input { Type = (UInt32) InputType.Mouse, Data = { Mouse = new MouseInput { Flags = (uint) clickDown, X = x, Y = y, MouseData = mouseData, Time = 0, ExtraInfo = IntPtr.Zero } } }; var up = new Input { Type = (UInt32) InputType.Mouse, Data = { Mouse = new MouseInput { Flags = (uint) clickUp, X = x, Y = y, MouseData = mouseData, Time = 0, ExtraInfo = IntPtr.Zero } } }; Input[] inputList = new Input[2]; inputList[0] = down; inputList[1] = up; var numberOfSuccessfulSimulatedInputs = SendInput(2, inputList, Marshal.SizeOf(typeof(Input))); if(numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The click press simulation for {0} & {1} was not successful.", clickDown, clickUp)); }