title: Windows API Fails to Create "Input Messages"/events for Certain Applications (i.e. Mozilla Firefox)
body: <p>I'm attempting to emulate user clicking and mouse-moving, specifically in Mozilla Firefox, in a Windows 7 environment.  The solution I frankensteined together from various tutorials, forum posts, and MSDN documents works for 99% of Windows Applications out there, but it won't work for Firefox 8.0.</p>
  
  <p>From my preliminary research, the most accurate (low-level) way of emulating keyboard and mouse input in Windows is to use the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx" rel="nofollow">SendInput function</a> from the User32.dll Windows Library.  To test this, I wrote a short C# program to loop through and make a call to SendInput that creates a programmatic mouse-click every 5 seconds, no matter where the mouse cursor is on screen.  </p>
  
  <p>Once running, the program perfectly emulates a mouseclick for almost every application window I switch focus to, even including the Windows interface itself (Start Button, Taskbar, Windows Explorer, etc.), but no programmatic mouse-clicks occur when I bring the cursor into a Mozilla Firefox Window.</p>
  
  <p>In order to get a better handle on what was happening under the hood, I booted up Microsoft Spy++ and began inspecting what messages were actually getting passed to the Firefox window's message queue.  Sure enough, the Firefox window would receive no messages even though my cursor would be situated directly above it while it had focus.  When I manually clicked my mouse, the Firefox Spy++ listener would then go nuts and display the full "nHittest:HTCLIENT wMouseMsg:WM_LBUTTONDOWN" that I saw when observing other applications' correct responses to my emulation program.</p>
  
  <p>Can anyone provide an explanation for how/why Mozilla Firefox is one of the only applications that does not even receive any messages at all from the SendInput function and perhaps a suggestion for how to overcome this?</p>
  
  <p><strong>Source Code</strong>
  (Imports/external references removed for clarity):</p>
  
  <pre><code>static void Main(string[] args)
  {
      for (; ; )
      {
          System.Threading.Thread.Sleep(5000);
          INPUT[] inp = new INPUT[2];
          inp[0].type = INPUT_MOUSE;
          inp[0].mi = createMouseInput(0, 0, 0, 0, MOUSEEVENTF_LEFTDOWN);
          inp[1].type = INPUT_MOUSE;
          inp[1].mi = createMouseInput(0, 0, 0, 0, MOUSEEVENTF_LEFTUP);
          SendInput((uint)inp.Length, inp, Marshal.SizeOf(inp[0].GetType()));
      }
  }
  
  private static MOUSEINPUT createMouseInput(int x, int y, uint data, uint t, uint flag)
  {
      MOUSEINPUT mi = new MOUSEINPUT();
      mi.dx = x;
      mi.dy = y;
      mi.mouseData = data;
      mi.time = t;
      mi.dwFlags = flag;
      return mi;
  }
  
  [StructLayout(LayoutKind.Sequential)]
  struct MOUSEINPUT
  {
      public int dx;
      public int dy;
      public uint mouseData;
      public uint dwFlags;
      public uint time;
      public IntPtr dwExtraInfo;
  }
  
  [StructLayout(LayoutKind.Explicit)]
  private struct INPUT
  {
      [FieldOffset(0)]
      public int type;
      [FieldOffset(sizeof(int))] //[FieldOffset(8)] for x64
      public MOUSEINPUT mi;
      [FieldOffset(sizeof(int))] //[FieldOffset(8)] for x64
      public KEYBDINPUT ki;
      [FieldOffset(sizeof(int))] //[FieldOffset(8)] for x64
      public HARDWAREINPUT hi;
  }
  </code></pre>