title: Send keys through SendInput in user32.dll
body: <p>I am using <a href="http://www.elektronikpraxis.vogel.de/imgserver/bdb/490000/490052/4.jpg" rel="noreferrer">this board</a> as a keyboard for demo purposes.</p>
  <p>Anyways to make the long story short everything works fine except for very few cases. I send keystrokes with the <strong><a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx" rel="noreferrer">SendInput</a></strong> function located in user32.dll.</p>
  <p>So my program looks like:</p>
  <pre><code>static void Main(string[] args)
  {
      Console.Write(&quot;Press enter an on the next secont the key combination shift+end will be send&quot;);
      Console.Read();
  
      Thread.Sleep(1000);
  
      SendKeyDown(KeyCode.SHIFT);
      SendKeyPress(KeyCode.END);
      SendKeyUp(KeyCode.SHIFT);
  
      Console.Read(); 
      Console.Read();
  }
  
  [DllImport(&quot;user32.dll&quot;, SetLastError = true)]
  private static extern uint SendInput(uint numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);
  
  /// &lt;summary&gt;
  /// simulate key press
  /// &lt;/summary&gt;
  /// &lt;param name=&quot;keyCode&quot;&gt;&lt;/param&gt;
  public static void SendKeyPress(KeyCode keyCode)
  {
      INPUT input = new INPUT {
          Type = 1
      };
      input.Data.Keyboard = new KEYBDINPUT() {
          Vk = (ushort)keyCode,
          Scan = 0,
          Flags = 0,
          Time = 0,
          ExtraInfo = IntPtr.Zero,
      };
  
      INPUT input2 = new INPUT {
          Type = 1
      };
      input2.Data.Keyboard = new KEYBDINPUT() {
          Vk = (ushort)keyCode,
          Scan = 0,
          Flags = 2,
          Time = 0,
          ExtraInfo = IntPtr.Zero
      };
      INPUT[] inputs = new INPUT[] { input, input2 };
      if (SendInput(2, inputs, Marshal.SizeOf(typeof(INPUT))) == 0)
          throw new Exception();            
  }
  
  /// &lt;summary&gt;
  /// Send a key down and hold it down until sendkeyup method is called
  /// &lt;/summary&gt;
  /// &lt;param name=&quot;keyCode&quot;&gt;&lt;/param&gt;
  public static void SendKeyDown(KeyCode keyCode)
  {
      INPUT input = new INPUT{
          Type = 1
      };
      input.Data.Keyboard = new KEYBDINPUT();
      input.Data.Keyboard.Vk = (ushort)keyCode;
      input.Data.Keyboard.Scan = 0;
      input.Data.Keyboard.Flags = 0;
      input.Data.Keyboard.Time = 0;
      input.Data.Keyboard.ExtraInfo = IntPtr.Zero;
      INPUT[] inputs = new INPUT[] { input };
      if (SendInput(1, inputs, Marshal.SizeOf(typeof(INPUT))) == 0)
      {
          throw new Exception();
      }
  }
  
  /// &lt;summary&gt;
  /// Release a key that is being hold down
  /// &lt;/summary&gt;
  /// &lt;param name=&quot;keyCode&quot;&gt;&lt;/param&gt;
  public static void SendKeyUp(KeyCode keyCode)
  {
      INPUT input = new INPUT {
          Type = 1
      };
      input.Data.Keyboard = new KEYBDINPUT();
      input.Data.Keyboard.Vk = (ushort)keyCode;
      input.Data.Keyboard.Scan = 0;
      input.Data.Keyboard.Flags = 2;
      input.Data.Keyboard.Time = 0;
      input.Data.Keyboard.ExtraInfo = IntPtr.Zero;
      INPUT[] inputs = new INPUT[] { input };
      if (SendInput(1, inputs, Marshal.SizeOf(typeof(INPUT))) == 0)
          throw new Exception();
  
  }
  </code></pre>
  <p>And here are the structs that I found online that those methods use and also the key codes: <strong>(note it looks like a lot of code and is because there are a lot of key codes in an Enum)</strong></p>
  <pre><code>    /// &lt;summary&gt;
      /// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270(v=vs.85).aspx
      /// &lt;/summary&gt;
      [StructLayout(LayoutKind.Sequential)]
      internal struct INPUT
      {
          public uint Type;
          public MOUSEKEYBDHARDWAREINPUT Data;
      }
  
      /// &lt;summary&gt;
      /// http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/f0e82d6e-4999-4d22-b3d3-32b25f61fb2a
      /// &lt;/summary&gt;
      [StructLayout(LayoutKind.Explicit)]
      internal struct MOUSEKEYBDHARDWAREINPUT
      {
          [FieldOffset(0)]
          public HARDWAREINPUT Hardware;
          [FieldOffset(0)]
          public KEYBDINPUT Keyboard;
          [FieldOffset(0)]
          public MOUSEINPUT Mouse;
      }
  
      /// &lt;summary&gt;
      /// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
      /// &lt;/summary&gt;
      [StructLayout(LayoutKind.Sequential)]
      internal struct HARDWAREINPUT
      {
          public uint Msg;
          public ushort ParamL;
          public ushort ParamH;
      }
  
      /// &lt;summary&gt;
      /// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
      /// &lt;/summary&gt;
      [StructLayout(LayoutKind.Sequential)]
      internal struct KEYBDINPUT
      {
          public ushort Vk;
          public ushort Scan;
          public uint Flags;
          public uint Time;
          public IntPtr ExtraInfo;
      }
  
      /// &lt;summary&gt;
      /// http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/2abc6be8-c593-4686-93d2-89785232dacd
      /// &lt;/summary&gt;
      [StructLayout(LayoutKind.Sequential)]
      internal struct MOUSEINPUT
      {
          public int X;
          public int Y;
          public uint MouseData;
          public uint Flags;
          public uint Time;
          public IntPtr ExtraInfo;
      }
  
      public enum KeyCode : ushort
      {
          #region Media
  
          /// &lt;summary&gt;
          /// Next track if a song is playing
          /// &lt;/summary&gt;
          MEDIA_NEXT_TRACK = 0xb0,
  
          /// &lt;summary&gt;
          /// Play pause
          /// &lt;/summary&gt;
          MEDIA_PLAY_PAUSE = 0xb3,
  
          /// &lt;summary&gt;
          /// Previous track
          /// &lt;/summary&gt;
          MEDIA_PREV_TRACK = 0xb1,
  
          /// &lt;summary&gt;
          /// Stop
          /// &lt;/summary&gt;
          MEDIA_STOP = 0xb2,
  
          #endregion
  
          #region math
  
          /// &lt;summary&gt;Key &quot;+&quot;&lt;/summary&gt;
          ADD = 0x6b,
          /// &lt;summary&gt;
          /// &quot;*&quot; key
          /// &lt;/summary&gt;
          MULTIPLY = 0x6a,
  
          /// &lt;summary&gt;
          /// &quot;/&quot; key
          /// &lt;/summary&gt;
          DIVIDE = 0x6f,
  
          /// &lt;summary&gt;
          /// Subtract key &quot;-&quot;
          /// &lt;/summary&gt;
          SUBTRACT = 0x6d,
  
          #endregion
  
          #region Browser
          /// &lt;summary&gt;
          /// Go Back
          /// &lt;/summary&gt;
          BROWSER_BACK = 0xa6,
          /// &lt;summary&gt;
          /// Favorites
          /// &lt;/summary&gt;
          BROWSER_FAVORITES = 0xab,
          /// &lt;summary&gt;
          /// Forward
          /// &lt;/summary&gt;
          BROWSER_FORWARD = 0xa7,
          /// &lt;summary&gt;
          /// Home
          /// &lt;/summary&gt;
          BROWSER_HOME = 0xac,
          /// &lt;summary&gt;
          /// Refresh
          /// &lt;/summary&gt;
          BROWSER_REFRESH = 0xa8,
          /// &lt;summary&gt;
          /// browser search
          /// &lt;/summary&gt;
          BROWSER_SEARCH = 170,
          /// &lt;summary&gt;
          /// Stop
          /// &lt;/summary&gt;
          BROWSER_STOP = 0xa9,
          #endregion
  
          #region Numpad numbers
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          NUMPAD0 = 0x60,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          NUMPAD1 = 0x61,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          NUMPAD2 = 0x62,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          NUMPAD3 = 0x63,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          NUMPAD4 = 100,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          NUMPAD5 = 0x65,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          NUMPAD6 = 0x66,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          NUMPAD7 = 0x67,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          NUMPAD8 = 0x68,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          NUMPAD9 = 0x69,
  
          #endregion
  
          #region Fkeys
          /// &lt;summary&gt;
          /// F1
          /// &lt;/summary&gt;
          F1 = 0x70,
          /// &lt;summary&gt;
          /// F10
          /// &lt;/summary&gt;
          F10 = 0x79,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F11 = 0x7a,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F12 = 0x7b,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F13 = 0x7c,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F14 = 0x7d,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F15 = 0x7e,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F16 = 0x7f,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F17 = 0x80,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F18 = 0x81,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F19 = 130,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F2 = 0x71,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F20 = 0x83,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F21 = 0x84,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F22 = 0x85,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F23 = 0x86,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F24 = 0x87,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F3 = 0x72,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F4 = 0x73,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F5 = 0x74,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F6 = 0x75,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F7 = 0x76,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F8 = 0x77,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          F9 = 120,
  
          #endregion
  
          #region Other
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_1 = 0xba,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_102 = 0xe2,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_2 = 0xbf,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_3 = 0xc0,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_4 = 0xdb,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_5 = 220,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_6 = 0xdd,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_7 = 0xde,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_8 = 0xdf,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_CLEAR = 0xfe,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_COMMA = 0xbc,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_MINUS = 0xbd,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_PERIOD = 190,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          OEM_PLUS = 0xbb,
  
          #endregion
  
          #region KEYS
  
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_0 = 0x30,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_1 = 0x31,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_2 = 50,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_3 = 0x33,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_4 = 0x34,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_5 = 0x35,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_6 = 0x36,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_7 = 0x37,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_8 = 0x38,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_9 = 0x39,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_A = 0x41,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_B = 0x42,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_C = 0x43,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_D = 0x44,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_E = 0x45,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_F = 70,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_G = 0x47,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_H = 0x48,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_I = 0x49,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_J = 0x4a,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_K = 0x4b,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_L = 0x4c,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_M = 0x4d,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_N = 0x4e,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_O = 0x4f,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_P = 80,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_Q = 0x51,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_R = 0x52,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_S = 0x53,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_T = 0x54,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_U = 0x55,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_V = 0x56,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_W = 0x57,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_X = 0x58,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_Y = 0x59,
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          KEY_Z = 90,
  
          #endregion
  
          #region volume
          /// &lt;summary&gt;
          /// Decrese volume
          /// &lt;/summary&gt;
          VOLUME_DOWN = 0xae,
  
          /// &lt;summary&gt;
          /// Mute volume
          /// &lt;/summary&gt;
          VOLUME_MUTE = 0xad,
  
          /// &lt;summary&gt;
          /// Increase volue
          /// &lt;/summary&gt;
          VOLUME_UP = 0xaf,
  
          #endregion
  
  
          /// &lt;summary&gt;
          /// Take snapshot of the screen and place it on the clipboard
          /// &lt;/summary&gt;
          SNAPSHOT = 0x2c,
  
          /// &lt;summary&gt;Send right click from keyboard &quot;key that is 2 keys to the right of space bar&quot;&lt;/summary&gt;
          RightClick = 0x5d,
  
          /// &lt;summary&gt;
          /// Go Back or delete
          /// &lt;/summary&gt;
          BACKSPACE = 8,
  
          /// &lt;summary&gt;
          /// Control + Break &quot;When debuging if you step into an infinite loop this will stop debug&quot;
          /// &lt;/summary&gt;
          CANCEL = 3,
          /// &lt;summary&gt;
          /// Caps lock key to send cappital letters
          /// &lt;/summary&gt;
          CAPS_LOCK = 20,
          /// &lt;summary&gt;
          /// Ctlr key
          /// &lt;/summary&gt;
          CONTROL = 0x11,
  
          /// &lt;summary&gt;
          /// Alt key
          /// &lt;/summary&gt;
          ALT = 18,
  
          /// &lt;summary&gt;
          /// &quot;.&quot; key
          /// &lt;/summary&gt;
          DECIMAL = 110,
  
          /// &lt;summary&gt;
          /// Delete Key
          /// &lt;/summary&gt;
          DELETE = 0x2e,
  
  
          /// &lt;summary&gt;
          /// Arrow down key
          /// &lt;/summary&gt;
          DOWN = 40,
  
          /// &lt;summary&gt;
          /// End key
          /// &lt;/summary&gt;
          END = 0x23,
  
          /// &lt;summary&gt;
          /// Escape key
          /// &lt;/summary&gt;
          ESC = 0x1b,
  
          /// &lt;summary&gt;
          /// Home key
          /// &lt;/summary&gt;
          HOME = 0x24,
  
          /// &lt;summary&gt;
          /// Insert key
          /// &lt;/summary&gt;
          INSERT = 0x2d,
  
          /// &lt;summary&gt;
          /// Open my computer
          /// &lt;/summary&gt;
          LAUNCH_APP1 = 0xb6,
          /// &lt;summary&gt;
          /// Open calculator
          /// &lt;/summary&gt;
          LAUNCH_APP2 = 0xb7,
  
          /// &lt;summary&gt;
          /// Open default email in my case outlook
          /// &lt;/summary&gt;
          LAUNCH_MAIL = 180,
  
          /// &lt;summary&gt;
          /// Opend default media player (itunes, winmediaplayer, etc)
          /// &lt;/summary&gt;
          LAUNCH_MEDIA_SELECT = 0xb5,
  
          /// &lt;summary&gt;
          /// Left control
          /// &lt;/summary&gt;
          LCONTROL = 0xa2,
  
          /// &lt;summary&gt;
          /// Left arrow
          /// &lt;/summary&gt;
          LEFT = 0x25,
  
          /// &lt;summary&gt;
          /// Left shift
          /// &lt;/summary&gt;
          LSHIFT = 160,
  
          /// &lt;summary&gt;
          /// left windows key
          /// &lt;/summary&gt;
          LWIN = 0x5b,
  
  
          /// &lt;summary&gt;
          /// Next &quot;page down&quot;
          /// &lt;/summary&gt;
          PAGEDOWN = 0x22,
  
          /// &lt;summary&gt;
          /// Num lock to enable typing numbers
          /// &lt;/summary&gt;
          NUMLOCK = 0x90,
  
          /// &lt;summary&gt;
          /// Page up key
          /// &lt;/summary&gt;
          PAGE_UP = 0x21,
  
          /// &lt;summary&gt;
          /// Right control
          /// &lt;/summary&gt;
          RCONTROL = 0xa3,
  
          /// &lt;summary&gt;
          /// Return key
          /// &lt;/summary&gt;
          ENTER = 13,
  
          /// &lt;summary&gt;
          /// Right arrow key
          /// &lt;/summary&gt;
          RIGHT = 0x27,
  
          /// &lt;summary&gt;
          /// Right shift
          /// &lt;/summary&gt;
          RSHIFT = 0xa1,
  
          /// &lt;summary&gt;
          /// Right windows key
          /// &lt;/summary&gt;
          RWIN = 0x5c,
  
          /// &lt;summary&gt;
          /// Shift key
          /// &lt;/summary&gt;
          SHIFT = 0x10,
  
          /// &lt;summary&gt;
          /// Space back key
          /// &lt;/summary&gt;
          SPACE_BAR = 0x20,
  
          /// &lt;summary&gt;
          /// Tab key
          /// &lt;/summary&gt;
          TAB = 9,
  
          /// &lt;summary&gt;
          /// Up arrow key
          /// &lt;/summary&gt;
          UP = 0x26,
  
      }
  </code></pre>
  <p><strong>So now my question is</strong> why when I send that key combination I do not get the same results as when I do it on a real keyboard? 98% of the things work. For example I am able to do:</p>
  <pre><code>    SendKeyDown(KeyCode.SHIFT);
      SendKeyPress(KeyCode.KEY_A );
      SendKeyUp(KeyCode.SHIFT);
  </code></pre>
  <p>And that will send a capital A.</p>
  <p>Should I use a different library?</p>
  <p><strong>The reason why I like this approach is because I do not know in advance if the user will send a key combination</strong> For example in windows forms If I do:</p>
  <p><code>System.Windows.Forms.SendKeys.SendWait(&quot;+{end}&quot;);</code> that will send shift + end  but maybe the user just wants to send shift...</p>