keybd_event (user32)
Last changed: -185.22.142.183

.
Summary
This function is useful to simulate Key presses

C# Signature:

[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
   UIntPtr dwExtraInfo);

VB Signature:

<DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, _
           CharSet:=CharSet.Unicode, EntryPoint:="keybd_event", _
           ExactSpelling:=True, SetLastError:=True)> _
Public Shared Sub keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, _
                              ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
End Sub

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

This function is useful to simulate Key presses (for input use the virtual keycodes from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp or windows CE universal core virtual key code compact chart http://msdn2.microsoft.com/en-us/library/ms927178.aspx ).

Use FindWindow and SetFocus to direct input to the desired window.

Note
Sleep may be required to assure Window is ready for input:

  RUN('NOTEPAD.EXE')
  Sleep(2000,0)
  SetFocus(FindWindow('Untitled - Notepad'))

(see also VkKeyScan):

    void PressKey( byte keyCode )
    {
        const int KEYEVENTF_EXTENDEDKEY = 0x1;
        const int KEYEVENTF_KEYUP       = 0x2;
        keybd_event( keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0 );
        keybd_event( keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
    }

Sample Code:

VB Sample:

This sample will generate a 'Scroll Lock' key press event when the user clicks 'Button1'.

Imports System.Runtime.InteropServices

Public Class Form1

    <DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, _
           CharSet:=CharSet.Unicode, EntryPoint:="keybd_event", _
           ExactSpelling:=True, SetLastError:=True)> _
    Public Shared Sub keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, _
                                  ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Const VK_SCROLL As Byte = &H91
        Const KEYEVENTF_KEYUP As Byte = &H2

        keybd_event(VK_SCROLL, 0, 0, 0)               ' Generates a KEY_DOWN
        keybd_event(VK_SCROLL, 0, KEYEVENTF_KEYUP, 0) ' Generates a KEY_UP

    End Sub

End Class

Please add some more!

Alternative Managed API: System.Windows.Forms.SendKeys

Documentation
keybd_event on MSDN

PROBLEM: How do i use combination of shift and tab keys at the same time ?

Answer:posted by dokks http://www.ravensmyst.com

define the shift key as a const

    public const byte VK_LSHIFT= 0xA0; // left shift key
    public const byte VK_TAB = 0x09;
    public const int KEYEVENTF_EXTENDEDKEY = 0x01;
    public const int KEYEVENTF_KEYUP = 0x02;

    //press the shift key
    keybd_event(VK_LSHIFT, 0x45, 0, 0);

    //press the tab key
    keybd_event(VK_TAB, 0x45, 0, 0);

    //release the tab key
    keybd_event(VK_TAB, 0x45, KEYEVENTF_KEYUP, 0);

    //release the alt key
    keybd_event(VK_LSHIFT, 0x45, KEYEVENTF_KEYUP, 0);