title: Using User32.dll SendMessage To Send Keys With ALT Modifier
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3080466/c-sharp-and-sendmessage-keys-is-not-working">C# and SendMessage (keys) is not working</a>  </p>
  </blockquote>
  
  
  
  <p>I am writing an application that sends keystrokes to another application using the SendMessage function defined in <em>user32.dll</em>. I have figured out how to send a single keystroke but I am stumped trying to send the keystroke along with the ALT key.</p>
  
  <p>For the purposes of my question I will focus on sending F1, and ALT + F1.</p>
  
  <p>As stated above, I am able to send the F1 key no problem. Here is a snippet of my code that sends the F1 key:</p>
  
  <pre><code>// DLL Imports
  
  //Set the active window
  [DllImport("user32.dll")]
  public static extern IntPtr SetActiveWindow(IntPtr hWnd);
  
  //sends a windows message to the specified window
  [DllImport("user32.dll")]
  public static extern int SendMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);
  
  // ...
  
  // Some constants
  #define WM_SYSKEYDOWN 260
  #define WM_SYSKEYUP 261
  #define WM_CHAR 258
  #define WM_KEYDOWN 256
  #define WM_KEYUP 257
  
  // ...
  
  // activate the window and send F1
  SetActiveWindow(hWnd);
  ushort action = (ushort)WM_SYSKEYDOWN;
  ushort key = (ushort)System.Windows.Forms.Keys.F1;
  SendMessage(hWnd, action, key, 0);
  </code></pre>
  
  <p>One interesting side note is that even though the above code works in sending the F1 key to the target application it is not the same as what I see using Spy++. Here is the output of the Spy++ log whenever I hit the F1 key while monitoring the target application:</p>
  
  <pre><code>&lt;00001&gt; 00050412 P WM_KEYDOWN nVirtKey:VK_F1 cRepeat:1 ScanCode:3B fExtended:0 fAltDown:0 fRepeat:0 fUp:0
  &lt;00002&gt; 00050412 P WM_KEYUP nVirtKey:VK_F1 cRepeat:1 ScanCode:3B fExtended:0 fAltDown:0 fRepeat:1 fUp:1
  </code></pre>
  
  <p>Note that there are two messages sent, WM_KEYDOWN and WM_KEYUP. </p>
  
  <p><em>My first question would be, why am I succesful sending F1 using WM_SYSKEYDOWN when Spy++ tells me that WM_KEYDOWN + WM_KEYUP is the proper message sequence?</em></p>
  
  <p>Moving on to my next challenge of trying to send ALT + F1. </p>
  
  <p>I have used Spy++ to monitor the messages passed when pressing ALT + F1 on my keyboard and this is what I see:</p>
  
  <pre><code>&lt;00001&gt; 00050412 P WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:1 fAltDown:1 fRepeat:0 fUp:0
  &lt;00002&gt; 00050412 P WM_SYSKEYDOWN nVirtKey:VK_F1 cRepeat:1 ScanCode:3B fExtended:0 fAltDown:1 fRepeat:0 fUp:0
  &lt;00003&gt; 00050412 P WM_SYSKEYUP nVirtKey:VK_F1 cRepeat:1 ScanCode:3B fExtended:0 fAltDown:1 fRepeat:1 fUp:1
  &lt;00004&gt; 00050412 P WM_KEYUP nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:1 fAltDown:0 fRepeat:1 fUp:1
  </code></pre>
  
  <p>Given the above Spy++ message capture I tried to send the exact message sequence using the following code (simplified):</p>
  
  <pre><code>SetActiveWindow(hWnd);    
  SendMessage(hWnd, (ushort)WM_SYSKEYDOWN, (ushort)System.Windows.Forms.Keys.Menu, 0);
  SendMessage(hWnd, (ushort)WM_SYSKEYDOWN, (ushort)System.Windows.Forms.Keys.F1, 0);
  SendMessage(hWnd, (ushort)WM_SYSKEYUP, (ushort)System.Windows.Forms.Keys.F1, 0);
  SendMessage(hWnd, (ushort)WM_KEYUP, (ushort)System.Windows.Forms.Keys.Menu, 0);
  </code></pre>
  
  <p>This did not work.</p>
  
  <p>And so this leads to my next question. <em>Is there anything else I can try or is there something that I am doing wrong here?</em></p>
  
  <p>Whenever I captured the output of my program using Spy++ here is what was logged:</p>
  
  <pre><code>&lt;00001&gt; 00050412 S WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
  &lt;00002&gt; 00050412 R WM_SYSKEYDOWN
  &lt;00003&gt; 00050412 S WM_SYSKEYDOWN nVirtKey:VK_F1 cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
  &lt;00004&gt; 00050412 R WM_SYSKEYDOWN
  &lt;00005&gt; 00050412 S WM_SYSKEYUP nVirtKey:VK_F1 cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
  &lt;00006&gt; 00050412 R WM_SYSKEYUP
  &lt;00007&gt; 00050412 S WM_KEYUP nVirtKey:VK_MENU cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
  &lt;00008&gt; 00050412 R WM_KEYUP
  </code></pre>
  
  <p>Notice that there are <em>extra</em> messages being sent at lines 2, 4, 6, and 8. <em>Could this be the reason why things are not working?</em></p>
  
  <p>I have one final question regarding the difference between the messages captured from actual keyboard input and those captured using my application. Notice the <em>cRepeat</em>, <em>ScanCode</em>, <em>fExtended</em>, etc. <em>arguments</em>. They are non-zero in the messages that were
  captured using my keyboard as the input and they are all zero in the messages sent by my application. <em>Could this be the reason why my code is not working?</em> <em>If so, how do I modify these values?</em> (I am assuming they come from the 4th argument to the SendMessage
  function, which I have set to zero in all cases.)</p>
  
  <p>Thank you,</p>
  
  <p>Jan</p>