Imports System.Runtime.InteropServices
Namespace WinApi
Module WinApi
Public Function TypeAscii(ByVal code As Integer) As Boolean
Dim kc = 96 + (code \ 100) ' key from numeric keyboard, for 1st digit of ASCII code
Dim kd = 96 + ((code Mod 100) \ 10) ' key from numeric keyboard, for 2nd digit of ASCII code
Dim ku = 96 + (code Mod 10) ' key from numeric keyboard, for 3rd digit of ASCII code
Return SendSingleInput(InputKeyDown(18)) AndAlso
SendSingleInput(InputKeyDown(kc)) AndAlso
SendSingleInput(InputKeyUp(kc)) AndAlso
SendSingleInput(InputKeyDown(kd)) AndAlso
SendSingleInput(InputKeyUp(kd)) AndAlso
SendSingleInput(InputKeyDown(ku)) AndAlso
SendSingleInput(InputKeyUp(ku)) AndAlso
SendSingleInput(InputKeyUp(18))
End Function
Private Function SendSingleInput(ByVal single_input As INPUT) As Boolean
Return CBool(SendInput(1, single_input, Marshal.SizeOf(single_input)))
Application.DoEvents()
End Function
Private Function InputKeyDown(ByVal wVk As Short) As INPUT
Dim i As INPUT
i.type = INPUT_KEYBOARD
i.u.ki.wVk = wVk
i.u.ki.dwFlags = KEYEVENTF_KEYDOWN
Return i
End Function
Private Function InputKeyUp(ByVal wVk As Short) As INPUT
Dim i As INPUT
i.type = INPUT_KEYBOARD
i.u.ki.wVk = wVk
i.u.ki.dwFlags = KEYEVENTF_KEYUP
Return i
End Function
' From here on, it's not "my" code, but code taken from Pinvoke.net and other places
<DllImport("user32.dll", SetLastError:=True)> _
Private Function SendInput(ByVal cInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer
End Function
Structure INPUT
Public type As Integer
Public u As InputUnion
End Structure
Private Const KEYEVENTF_KEYDOWN As Integer = &H0
Private Const KEYEVENTF_EXTENDEDKEY As Integer = &H1
Private Const KEYEVENTF_KEYUP As Integer = &H2
Private Const KEYEVENTF_UNICODE As Integer = &H4
Private Const KEYEVENTF_SCANCODE As Integer = &H8
Private Const INPUT_MOUSE As Integer = 0
Private Const INPUT_KEYBOARD As Integer = 1
Private Const INPUT_HARDWARE As Integer = 2
<StructLayout(LayoutKind.Explicit)> _
Structure InputUnion
<FieldOffset(0)> Public mi As MOUSEINPUT
<FieldOffset(0)> Public ki As KEYBDINPUT
<FieldOffset(0)> Public hi As HARDWAREINPUT
End Structure
Structure MOUSEINPUT
Public dx As Integer
Public dy As Integer
Public mouseData As Integer
Public dwFlags As Integer
Public time As Integer
Public dwExtraInfo As IntPtr
End Structure
Private Const MOUSEEVENTF_MOVE = &H1
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10
Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
Private Const MOUSEEVENTF_MIDDLEUP = &H40
Private Const MOUSEEVENTF_ABSOLUTE = &H8000
Structure KEYBDINPUT
Public wVk As Short
Public wScan As Short
Public dwFlags As Integer
Public time As Integer
Public dwExtraInfo As IntPtr
End Structure
Structure HARDWAREINPUT
Public uMsg As Integer
Public wParamL As Short
Public wParamH As Short
End Structure
End Module
End Namespace