Beispiel #1
0
 /// <summary>
 /// Calls the Win32 SendInput method with a KeyDown and KeyUp message in the same input sequence in order to simulate a Key PRESS.
 /// 
 /// </summary>
 /// <param name="keyCode">The KeyCode to press</param>
 public static void SimulateKeyPress(this KeyCode keyCode)
 {
     var input1 = new INPUT();
     input1.Type = 1U;
     input1.Data.Keyboard = new KEYBDINPUT();
     input1.Data.Keyboard.Vk = (ushort)keyCode;
     input1.Data.Keyboard.Scan = 0;
     input1.Data.Keyboard.Flags = 0U;
     input1.Data.Keyboard.Time = 0U;
     input1.Data.Keyboard.ExtraInfo = IntPtr.Zero;
     var input2 = new INPUT();
     input2.Type = 1U;
     input2.Data.Keyboard = new KEYBDINPUT();
     input2.Data.Keyboard.Vk = (ushort)keyCode;
     input2.Data.Keyboard.Scan = 0;
     input2.Data.Keyboard.Flags = 2U;
     input2.Data.Keyboard.Time = 0U;
     input2.Data.Keyboard.ExtraInfo = IntPtr.Zero;
     if ((int)NativeWin32.SendInput(2U, new INPUT[2] {
                                              input1,
                                              input2
                                          }, Marshal.SizeOf(typeof(INPUT))) == 0)
         throw new Exception(string.Format("The key press simulation for {0} was not successful.", keyCode));
 }
Beispiel #2
0
 /// <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 > (long)int.MaxValue)
     {
         throw new ArgumentException(
             string.Format("The text parameter is too long. It must be less than {0} characters.",
                           (uint)int.MaxValue), "text");
     }
     byte[] bytes = Encoding.ASCII.GetBytes(text);
     int length = bytes.Length;
     var inputs = new INPUT[length * 2];
     for (int index = 0; index < length; ++index)
     {
         ushort num = bytes[index];
         var input1 = new INPUT();
         input1.Type = 1U;
         input1.Data.Keyboard = new KEYBDINPUT();
         input1.Data.Keyboard.Vk = 0;
         input1.Data.Keyboard.Scan = num;
         input1.Data.Keyboard.Flags = 4U;
         input1.Data.Keyboard.Time = 0U;
         input1.Data.Keyboard.ExtraInfo = IntPtr.Zero;
         var input2 = new INPUT();
         input2.Type = 1U;
         input2.Data.Keyboard = new KEYBDINPUT();
         input2.Data.Keyboard.Vk = 0;
         input2.Data.Keyboard.Scan = num;
         input2.Data.Keyboard.Flags = 6U;
         input2.Data.Keyboard.Time = 0U;
         input2.Data.Keyboard.ExtraInfo = IntPtr.Zero;
         if ((num & 65280) == 57344)
         {
             input1.Data.Keyboard.Flags |= 1U;
             input2.Data.Keyboard.Flags |= 1U;
         }
         inputs[2 * index] = input1;
         inputs[2 * index + 1] = input2;
     }
     var num1 = (int)NativeWin32.SendInput((uint)(length * 2), inputs, Marshal.SizeOf(typeof(INPUT)));
 }
Beispiel #3
0
 internal static extern uint SendInput(uint numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);