[RESOLVED] Using SendMessage to send "enter" to an app
I want to have a VB6 program open an app, then send a keystroke (enter) to the app. The app may or may not be in focus. I thought if I use SendMessage as shown below, this would work whether or not the app window is in focus...
SendMessage (Hwnd,WM_CHAR,13,ByVal 0)
I have two problems... first, I don't know what declarations need to be made for SendMessage. Last of all, I don't know how to get the Hwnd when I launch the app. I thought maybe it would be like this...
Hwnd = Shell("c:\Programs\MyApp.exe", vbNormalFocus)
I know this isn't correct, but I don't know how to do it. Any help would be appreciated!:)
Re: Using SendMessage to send "enter" to an app
Check out the definition and code example of SendMessage.
http://allapi.mentalis.org/apilist/SendMessage.shtml
Note: You will need FindWindow and FindWindowEx to obtain the proper window handle of the textbox or button that is to recceive the keypress message.
Re: Using SendMessage to send "enter" to an app
Before I started this thread, I tried this code...
http://vbnet.mvps.org/index.html?cod.../shellhwnd.htm
It opens notepad, but then nothing more happens. I also tried the link you gave, but I can't even get that to work. Granted it's probably due to my limited knowledge of VB or poor programming skills, but it seems like there should be an easier way that only requires a few lines of code to do something this simple.
Is there a better method than SendMessage to open an application (like notepad) and send a keystroke to it?
Re: Using SendMessage to send "enter" to an app
Its by no means simple or just a few lines. Automating another app is mid level complexity.
The example should place text into notepad although shell may not be returning the PId. You could use FindWindow to be sure.
Re: Using SendMessage to send "enter" to an app
Ok, I have the following code which almost seems to work. I have the classname & title for both the window and the close button I'm trying to push. I tried passing ascii 13 (enter) and ascii 20 (space bar) to try to push the close button. I don't get an error. I know I have the hwnd's correct because if I change either name, I get the message that it couldn't find the window or the button. Any idea what I'm doing wrong?
vb Code:
Option Explicit
Private Const WM_CHAR = &H102
Declare Function SendMessageBynum Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
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 Sub Main()
Dim HandleWin As Long
Dim hwnd As Long
' Find the handle to the Reatogo window
HandleWin = FindWindow("#32770", "Reatogo PE Builder v3.1.10a - Build")
If HandleWin = 0 Then
MsgBox "Couldn't find the window handle"
Exit Sub
End If
' Find the handle to close button on the window found
hwnd = FindWindowEx(HandleWin, 0, "Button", "&>>") 'vbNullString)
If hwnd = 0 Then
MsgBox "Couldn't find the button"
Exit Sub
End If
Call SendMessageBynum(hwnd, WM_CHAR, 13, ByVal 0)
MsgBox "key sent"
MsgBox hwnd
End Sub
Re: Using SendMessage to send "enter" to an app
Quote:
Originally Posted by BrianPaul
Any idea what I'm doing wrong?
Have you tried using "SendMessage" or "PostMessage" with the BM_CLICK param?
Const BM_CLICK As Long = &HF5&
PostMessage hwnd, BM_CLICK, 0, 0
Re: Using SendMessage to send "enter" to an app
BM_CLICK worked! Thanks Edgemeal!
Re: Using SendMessage to send "enter" to an app
Quote:
Originally Posted by
BrianPaul
BM_CLICK worked! Thanks Edgemeal!
I am stuck in same kind of problem...please help me...
I am trying to click the "OK" button when it is prompted after clicking the "Update" button.
The challenge is that when I am running the full code its not clicking the OK button which comes.
But when I am clicking the "Update" button manually in Debug mode its working....!
Please help...
Set UpdateBtn = ie.document.getElementsByName("update")
UpdateBtn(0).Click 'Click Update button
Application.Wait (Now + TimeValue("00:00:02"))
For i = 1 To 2
WIN_Dialog = FindWindow(vbNullString, Dialog_Caption) 'Get the dialogbox hwnd
If WIN_Dialog = 0 Then
Exit For
End If
Dlg_ChildWIN = FindWindowEx(WIN_Dialog, 0, Button_ClassName, ButtonTxt)
SendMessage Dlg_ChildWIN, BM_CLICK, 0, 0 'click OK button
Next