title: .NET - Detecting mouse movement through stubborn control
body: <p>I am writing a C# Project that is relatively simple. Think "Public Web Terminal". Essentially there is a maximized form with a fill-docked web browser on it. The web browser control I am using is the WebKit Control found here:</p>
  
  <p><a href="http://webkitdotnet.sourceforge.net/downloads.php" rel="nofollow noreferrer">WebKit Download Page</a></p>
  
  <p>I am trying to detect system idle time by keeping a DateTime which represents the last time a mouse movement or key press was made.</p>
  
  <p>I have instituted event handlers (see code below), and have come across a stumbling block. The mouse (and key) events seem to not fire when my mouse moves over the web document. It DOES work fine when my mouse touches the vertical scroll bar portion of the web browser control, so I know the code is OK - it seems to be some sort of "oversight" (for lack of a better word) on the control's part.</p>
  
  <p>I guess my question is - to all of you coders out there, how would you go about handling this?</p>
  
  <pre><code>this.webKitBrowser1.KeyPress += new KeyPressEventHandler(handleKeyPress);
  this.webKitBrowser1.MouseMove += new MouseEventHandler(handleAction);
  this.webKitBrowser1.MouseClick += new MouseEventHandler(handleAction);
  this.webKitBrowser1.MouseDown += new MouseEventHandler(handleAction);
  this.webKitBrowser1.MouseUp += new MouseEventHandler(handleAction);
  this.webKitBrowser1.MouseDoubleClick += new MouseEventHandler(handleAction);
  
  void handleKeyPress(object sender, KeyPressEventArgs e)
  {
      this.handleAction(sender, null);
  }
  
  void handleAction(object sender, MouseEventArgs e)
  {
      this.lastAction = DateTime.Now;
      this.label4.Text = this.lastAction.ToLongTimeString();
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Using Joe's accepted solution, I put together the following class. Thanks to all who participated.</p>
  
  <pre><code>class classIdleTime
  {
      [DllImport("user32.dll")]
      static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
  
      internal struct LASTINPUTINFO
      {
          public Int32 cbSize;
          public Int32 dwTime;
      }
  
      public int getIdleTime()
      {
          int systemUptime = Environment.TickCount;
          int LastInputTicks = 0;
          int IdleTicks = 0;
  
          LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();
          LastInputInfo.cbSize = (Int32)Marshal.SizeOf(LastInputInfo);
          LastInputInfo.dwTime = 0;
  
          if (GetLastInputInfo(ref LastInputInfo))
          {
              LastInputTicks = (int)LastInputInfo.dwTime;
              IdleTicks = systemUptime - LastInputTicks;
          }
          Int32 seconds = IdleTicks / 1000;
          return seconds;
      }
  </code></pre>
  
  <p><strong>USAGE</strong></p>
  
  <pre><code>idleTimeObject = new classIdleTime();
  Int32 seconds = idleTimeObject.getIdleTime();
  this.isIdle = (seconds &gt; secondsBeforeIdle);
  </code></pre>