In a VB6 program, I want to open an application (maybe using shell) and send a keystroke (enter) to the app whether it is in focus or minimized or whatever. I found the following code here on EE and it works great for sending text to an app, but I just want to send a single key (the enter key). I tried to substitute SendMessageSTR with SendMessage, but I must be doing something wrong. Can someone help me to modify the code or suggest a better way.
--------------------------
----------
----------
----
' PUT THE FOLLOWING IN A MODULE:
'-------------------------
----------
----------
-----
Option Explicit
Private Const WM_SETTEXT = &HC
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessageSTR Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Public Function SendString(ByVal hWnd As Long, ByVal TheString As String) As Boolean
' Check for a valid handle
If hWnd = 0 Then
Exit Function
End If
' Send the string via the SendMessage API (altered for sending strings)
If SendMessageSTR(hWnd, WM_SETTEXT, 0, TheString) = 0 Then
MsgBox "Error sending the string"
Exit Function
End If
SendString = True
End Function
Public Sub Main()
Dim HandleWin As Long
Dim HandleEdit As Long
' Find the handle to the NOTEPAD window
HandleWin = FindWindow("Notepad", "Untitled - Notepad")
If HandleWin = 0 Then
MsgBox "Couldn't find the window handle"
Exit Sub
End If
' Find the handle to the text area of the NOTEPAD window found
HandleEdit = FindWindowEx(HandleWin, 0, "Edit", vbNullString)
If HandleEdit = 0 Then
MsgBox "Couldn't find the text area of the window"
Exit Sub
End If
SendString HandleEdit, "Check it out... it works!!"
End Sub
Start Free Trial