Code:
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Class KeySender
<DllImport("user32.dll", SetLastError := True)> _
Private Shared Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
End Function
<DllImport("User32.Dll", EntryPoint := "PostMessageA")> _
Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function VkKeyScan(ByVal ch As Char) As Byte
End Function
Const WM_KEYDOWN As UInteger = 256
Public Shared Function GetKey(ByVal character As Char) As Keys
Return DirectCast(VkKeyScan(character), Keys)
End Function
Public Shared Sub SendMessageToWindow(ByVal windowHandle As IntPtr, ByVal message As IEnumerable(Of Char))
For Each c As Char In message
SendKeyToWindow(windowHandle, GetKey(c))
Next
End Sub
Public Shared Sub SendMessageToWindow(ByVal process As Process, ByVal message As IEnumerable(Of Char))
SendMessageToWindow(process.MainWindowHandle, message)
End Sub
Public Shared Sub SendMessageToWindow(ByVal windowName As String, ByVal message As IEnumerable(Of Char))
For Each process As Process In Process.GetProcesses()
If process.MainWindowTitle = windowName Then
SendMessageToWindow(process.MainWindowHandle, message)
End If
Next
End Sub
Public Shared Sub SendKeyToWindow(ByVal windowHandle As IntPtr, ByVal key As Keys)
PostMessage(windowHandle, WM_KEYDOWN, CByte(key), 0)
End Sub
Public Shared Sub SendKeyToWindow(ByVal windowName As String, ByVal key As Keys)
For Each process As Process In Process.GetProcesses()
If process.MainWindowTitle = windowName Then
SendKeyToWindow(process.MainWindowHandle, key)
Exit For
End If
Next
End Sub
Public Shared Sub SendKeyToWindow(ByVal process As Process, ByVal key As Keys)
SendKeyToWindow(process.MainWindowHandle, key)
End Sub
End Class
That class will handle pretty much any key sending you need. (That's VB.NET)
And in VB6 (using outdated API calls, since keybd_event is obsolete now, should look into SendInput instead) Taken from vbAccelerator - SendKeys using the API
Code:
Private Declare Sub keybd_event Lib "user32" ( _
ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Private Declare Function GetVersion Lib "kernel32" () As Long
Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" ( _
ByVal cChar As Byte) As Integer
Private Declare Function VkKeyScanW Lib "user32" ( _
ByVal cChar As Integer) As Integer
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Public Sub KeyDown(ByVal vKey As KeyCodeConstants)
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY, 0
End Sub
Public Sub KeyUp(ByVal vKey As KeyCodeConstants)
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
End Sub
Public Function KeyCode(ByVal sChar As String) As KeyCodeConstants
Dim bNt As Boolean
Dim iKeyCode As Integer
Dim b() As Byte
Dim iKey As Integer
Dim vKey As KeyCodeConstants
Dim iShift As ShiftConstants
' Determine if we have Unicode support or not:
bNt = ((GetVersion() And &H80000000) = 0)
' Get the keyboard scan code for the character:
If (bNt) Then
b = sChar
CopyMemory iKey, b(0), 2
iKeyCode = VkKeyScanW(iKey)
Else
b = StrConv(sChar, vbFromUnicode)
iKeyCode = VkKeyScan(b(0))
End If
KeyCode = (iKeyCode And &HFF&)
End Function