title: How to trigger keydown and keyup events from external applications in VB.NET? avoid sendkeys method
body: <p>I am trying to develop a <strong>keystroke macro program</strong> using vb.net which will record keystrokes like <strong>keydown</strong> and <strong>keyup</strong> <strong>events</strong> and then play it anywhere while the main program is running in the background. As of now I have successfully captured the keystrokes and stored those strokes. But the problem which I m facing, is at the time of playing those stored keystrokes. I <strong>can't fire the KeyDown and KeyUp events</strong> <strong>from</strong> any <strong>external program</strong>. I have tried <strong>SendKeys</strong> method as well but it cannot differentiate between KeyDown and KeyUp separately. Help in this scenario will highly be appreciated.</p>
  <p><strong>The KeyDown event which is accessible in parent program only</strong>.</p>
  <pre><code>Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
          'MessageBox.Show(e.KeyCode)
          'bla bla bla
  End Sub
  </code></pre>
  <p><strong>'Use of SendKeys but it cannot distinguish between KeyDown and KeyUp</strong></p>
  <pre><code>Private Function AutoSendKey(ByVal keystroke As String, ByVal delay As Integer)     
          System.Threading.Thread.Sleep(delay)
          My.Computer.Keyboard.SendKeys(keystroke, True)
      End Function
  </code></pre>
  <p>I need an approach to trigger KeyDown and KeyUp events from external applications. Thanks in advance</p>