Hello everyone. I know this subject is dealt with numerious of times on the internet, but the .NET count is scarce. I got the following coding structure:
vb Code:
  1. Public Structure KeyEvent
  2.         Private Declare Function MapVirtualKey Lib "User32.dll" Alias "MapVirtualKeyA" (ByVal uCode As UInt32, ByVal uMapType As MAPVK) As UInt32
  3.         Private Enum MAPVK As UInt32
  4.             ''' <summary>uCode is a virtual-key code and is translated into a scan code.
  5.             ''' If it is a virtual-key code that does not distinguish between left- and
  6.             ''' right-hand keys, the left-hand scan code is returned.
  7.             ''' If there is no translation, the function returns 0.
  8.             ''' </summary>
  9.             ''' <remarks></remarks>
  10.             VK_TO_VSC = 0
  11.             ''' <summary>uCode is a scan code and is translated into a virtual-key code that
  12.             ''' does not distinguish between left- and right-hand keys. If there is no
  13.             ''' translation, the function returns 0.
  14.             ''' </summary>
  15.             ''' <remarks></remarks>
  16.             VSC_TO_VK = 1
  17.             ''' <summary>uCode is a virtual-key code and is translated into an unshifted
  18.             ''' character value in the low-order word of the return value. Dead keys (diacritics)
  19.             ''' are indicated by setting the top bit of the return value. If there is no
  20.             ''' translation, the function returns 0.
  21.             ''' </summary>
  22.             ''' <remarks></remarks>
  23.             VK_TO_CHAR = 2
  24.             ''' <summary>Windows NT/2000/XP: uCode is a scan code and is translated into a
  25.             ''' virtual-key code that distinguishes between left- and right-hand keys. If
  26.             ''' there is no translation, the function returns 0.
  27.             ''' </summary>
  28.             ''' <remarks></remarks>
  29.             VSC_TO_VK_EX = 3
  30.         End Enum
  31.  
  32.         Sub New(ByVal Key As Keys, ByVal EventType As KeyEvent)
  33.             Dim Up As Boolean = EventType = KeyEvent.Up
  34.             Dim Alt As Boolean = (Key And Keys.Alt) = Keys.Alt
  35.             Dim F10 As Boolean = (Key And Keys.F10) = Keys.F10
  36.             If Alt Or F10 Then EventType += 4 'offset to sys_
  37.  
  38.             Me.Key = Key
  39.             Me.EventType = EventType
  40.             Me.Flags = EventFlag.None
  41.             If Up Then Me.Flags = EventFlag.PrevWasDown
  42.             If Up And Not Alt And Not F10 Then Me.Flags += EventFlag.Transition
  43.             If Alt And Not Up Then Me.Flags += EventFlag.AltKeyDown
  44.  
  45.             Dim extended As Boolean = False
  46.             If Key = Keys.LControlKey Or Key = Keys.RControlKey Then extended = True
  47.             If Key = Keys.LShiftKey Or Key = Keys.RShiftKey Then extended = True
  48.             If Key = Keys.LMenu Or Key = Keys.RMenu Then extended = True
  49.             If extended Then Me.Flags += EventFlag.ExtendedKey
  50.             Me.RepeatCount = 0
  51.             Me.scancode = 0
  52.         End Sub
  53.         Sub New(ByVal Character As Char)
  54.             Me.Key = Asc(Character)
  55.             Me.EventType = KeyEvent.CharPress
  56.             Me.Flags = EventFlag.None
  57.         End Sub
  58.         Public EventType As KeyEvent
  59.         Public Key As Keys
  60.         Public RepeatCount As UShort
  61.         Public scancode As Byte
  62.         Public Sub SetScanCode(ByVal key As Keys)
  63.             Me.scancode = MapVirtualKey(key, MAPVK.VK_TO_VSC)
  64.         End Sub
  65.         Public Flags As EventFlag
  66.         Public Enum EventFlag As Byte
  67.             None = 0
  68.             ExtendedKey = 1
  69.             AltKeyDown = 32
  70.             PrevWasDown = 64
  71.             Transition = 128
  72.         End Enum
  73.         Public Enum KeyEvent
  74.             Down = &H100
  75.             Up = &H101
  76.             CharPress = &H102
  77.         End Enum
  78.         Public Function Post(ByVal hwnd As IntPtr) As Boolean
  79.             Dim r() As Byte = BitConverter.GetBytes(Me.RepeatCount)
  80.             Dim value(3) As Byte
  81.             value(0) = r(0)
  82.             value(1) = r(1)
  83.             value(2) = scancode
  84.             value(3) = Flags
  85.             Return API.PostMessage(hwnd, EventType, Key, BitConverter.ToInt32(value, 0))
  86.         End Function
  87.     End Structure

It can send keys and chars. It converts all variables used for the "lparam" to a single int32 value. (4 bytes) Call functions:
Code:
    Public Function SendKey(ByVal Key As Keys, ByVal EventType As KeyEvent.KeyEvent, Optional ByVal SendCount As Short = 1)
        Dim k As New KeyEvent(Key, EventType)
        k.RepeatCount = SendCount - 1
        Return SendKey(k)
    End Function
    Public Function SendKey(ByVal k As KeyEvent)
        Return k.Post(Me.hwnd)
    End Function
    Public Function SendKeyChar(ByVal character As Char) As Boolean
        Dim k As New KeyEvent(character)
        Return SendKey(k)
    End Function
Examples:
Code:
        Dim w As Window = New Window("Form1").Children(0)
        w.SendKey(Keys.Control Or Keys.A, Window.KeyEvent.KeyEvent.Down)
        w.SendKey(Keys.Control Or Keys.A, Window.KeyEvent.KeyEvent.Up)
        w.SendKey(Keys.A, Window.KeyEvent.KeyEvent.Down)
        w.SendKey(Keys.A, Window.KeyEvent.KeyEvent.Up)
        w.SendKeyChar("A")
        w.SendKeyChar("a")
The function + sub resides in my Window class at this point. I only have on question: what is the use of the "scancode" value? I have set it to a few keys but no noticable changes...neither is there any explanation for it.