Public Structure KeyEvent
Private Declare Function MapVirtualKey Lib "User32.dll" Alias "MapVirtualKeyA" (ByVal uCode As UInt32, ByVal uMapType As MAPVK) As UInt32
Private Enum MAPVK As UInt32
''' <summary>uCode is a virtual-key code and is translated into a scan code.
''' If it is a virtual-key code that does not distinguish between left- and
''' right-hand keys, the left-hand scan code is returned.
''' If there is no translation, the function returns 0.
''' </summary>
''' <remarks></remarks>
VK_TO_VSC = 0
''' <summary>uCode is a scan code and is translated into a virtual-key code that
''' does not distinguish between left- and right-hand keys. If there is no
''' translation, the function returns 0.
''' </summary>
''' <remarks></remarks>
VSC_TO_VK = 1
''' <summary>uCode is a virtual-key code and is translated into an unshifted
''' character value in the low-order word of the return value. Dead keys (diacritics)
''' are indicated by setting the top bit of the return value. If there is no
''' translation, the function returns 0.
''' </summary>
''' <remarks></remarks>
VK_TO_CHAR = 2
''' <summary>Windows NT/2000/XP: uCode is a scan code and is translated into a
''' virtual-key code that distinguishes between left- and right-hand keys. If
''' there is no translation, the function returns 0.
''' </summary>
''' <remarks></remarks>
VSC_TO_VK_EX = 3
End Enum
Sub New(ByVal Key As Keys, ByVal EventType As KeyEvent)
Dim Up As Boolean = EventType = KeyEvent.Up
Dim Alt As Boolean = (Key And Keys.Alt) = Keys.Alt
Dim F10 As Boolean = (Key And Keys.F10) = Keys.F10
If Alt Or F10 Then EventType += 4 'offset to sys_
Me.Key = Key
Me.EventType = EventType
Me.Flags = EventFlag.None
If Up Then Me.Flags = EventFlag.PrevWasDown
If Up And Not Alt And Not F10 Then Me.Flags += EventFlag.Transition
If Alt And Not Up Then Me.Flags += EventFlag.AltKeyDown
Dim extended As Boolean = False
If Key = Keys.LControlKey Or Key = Keys.RControlKey Then extended = True
If Key = Keys.LShiftKey Or Key = Keys.RShiftKey Then extended = True
If Key = Keys.LMenu Or Key = Keys.RMenu Then extended = True
If extended Then Me.Flags += EventFlag.ExtendedKey
Me.RepeatCount = 0
Me.scancode = 0
End Sub
Sub New(ByVal Character As Char)
Me.Key = Asc(Character)
Me.EventType = KeyEvent.CharPress
Me.Flags = EventFlag.None
End Sub
Public EventType As KeyEvent
Public Key As Keys
Public RepeatCount As UShort
Public scancode As Byte
Public Sub SetScanCode(ByVal key As Keys)
Me.scancode = MapVirtualKey(key, MAPVK.VK_TO_VSC)
End Sub
Public Flags As EventFlag
Public Enum EventFlag As Byte
None = 0
ExtendedKey = 1
AltKeyDown = 32
PrevWasDown = 64
Transition = 128
End Enum
Public Enum KeyEvent
Down = &H100
Up = &H101
CharPress = &H102
End Enum
Public Function Post(ByVal hwnd As IntPtr) As Boolean
Dim r() As Byte = BitConverter.GetBytes(Me.RepeatCount)
Dim value(3) As Byte
value(0) = r(0)
value(1) = r(1)
value(2) = scancode
value(3) = Flags
Return API.PostMessage(hwnd, EventType, Key, BitConverter.ToInt32(value, 0))
End Function
End Structure