title: Sending input to different window using User32.dll
body: <p>I am trying to send key commands from my program to a different one but it is currently not working.
  This regardless of the exact same implementation working completely fine in a different program.</p>
  
  <p>The activation of the other window works fine, but there is no scancodes being sent to it.</p>
  
  <p>Help is very much appreciated.</p>
  
  <p>This is my code</p>
  
  <pre><code>[DllImport( "User32.dll" )]
  private static extern int SetForegroundWindow( IntPtr point );
  
  [DllImport( "User32.dll" )]
  private static extern bool ShowWindow( IntPtr hWnd, int nCmdShow );
  
  public void SendKeyBind( ) {
          if ( Main.Device == DeviceType.Keyboard ) {
              SetForegroundWindow( EliteDangerousPointer );
              ShowWindow( EliteDangerousPointer, 5 );
  
              List&lt;uint&gt; modifiersSC = Modifiers.ConvertAll( x =&gt; ConvertKeyToSC( x.Key ) );
              uint mainSC = ConvertKeyToSC( Main.Key );
  
              foreach ( uint msc in modifiersSC )
                  SendKey( msc, KeyFlag.KeyDown | KeyFlag.Scancode );
              SendKey( mainSC, KeyFlag.KeyDown | KeyFlag.Scancode );
              System.Threading.Thread.Sleep( 100 );
              SendKey( mainSC, KeyFlag.KeyUp | KeyFlag.Scancode );
              foreach ( uint msc in modifiersSC )
                  SendKey( msc, KeyFlag.KeyUp | KeyFlag.Scancode );
          }
      }
  
  [DllImport( "User32.dll" )]
  private static extern uint MapVirtualKey( uint uCode, uint uMapType );
  
  private uint ConvertKeyToSC( Keys key ) {
      uint keyCode = (uint)key;
      uint scanCode = MapVirtualKey(keyCode, 0);
      return scanCode;
  }
  </code></pre>
  
  <p>Which uses this class with <code>using static</code> to actually send the scancodes</p>
  
  <pre><code>public static class VirtualKeyboard {
  
      [StructLayout( LayoutKind.Sequential )]
      internal struct KEYBOARDINPUT {
          public uint type;
          public ushort vk;
          public ushort scanCode;
          public uint flags;
          public uint time;
          public uint extrainfo;
          public uint padding1;
          public uint padding2;
      }
  
      [StructLayout( LayoutKind.Sequential )]
      internal struct MOUSEINPUT {
          public uint dx;
          public uint dy;
          public uint mouseData;
          public uint dwFlags;
          public uint time;
          public IntPtr dwExtraInfo;
      }
  
      [StructLayout( LayoutKind.Sequential )]
      internal struct HARDWAREINPUT {
          public int uMsg;
          public short wParamL;
          public short wParamH;
      }
  
      [StructLayout( LayoutKind.Explicit )]
      internal struct INPUT {
  
          [FieldOffset(0)]
          public int type;
  
          [FieldOffset(4)]
          public MOUSEINPUT mi;
  
          [FieldOffset(4)]
          public KEYBOARDINPUT ki;
  
          [FieldOffset(4)]
          public HARDWAREINPUT hi;
      }
  
      [DllImport( "User32.dll" )]
      private static extern uint SendInput( uint numberOfInputs, ref INPUT input,
      int structSize );
  
      [Flags]
      public enum KeyFlag {
          KeyDown = 0x0000,
          KeyUp = 0x0002,
          Scancode = 0x0008
      }
  
      public static void SendKey( uint keyCode, KeyFlag keyFlag ) {
          INPUT InputData = new INPUT();
  
          InputData.type = 1;
          InputData.ki.scanCode = ( ushort ) keyCode;
          InputData.ki.flags = ( uint ) keyFlag;
          Console.WriteLine( InputData.ki.scanCode );
          Console.WriteLine( keyCode );
  
          SendInput( ( uint ) 1, ref InputData, ( int ) Marshal.SizeOf( typeof( INPUT ) ) );
      }
  }
  </code></pre>