Results 1 to 8 of 8

Thread: [RESOLVED] Using SendMessage to send "enter" to an app

  1. #1

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Resolved [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!

  2. #2
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    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?

  4. #4
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    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:
    1. Option Explicit
    2. Private Const WM_CHAR = &H102
    3. 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
    4. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    5. 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
    6. Private Sub Main()
    7.   Dim HandleWin As Long
    8.   Dim hwnd As Long
    9.  
    10.   ' Find the handle to the Reatogo window
    11.   HandleWin = FindWindow("#32770", "Reatogo PE Builder v3.1.10a - Build")
    12.   If HandleWin = 0 Then
    13.     MsgBox "Couldn't find the window handle"
    14.     Exit Sub
    15.   End If
    16.  
    17.   ' Find the handle to close button on the window found
    18.   hwnd = FindWindowEx(HandleWin, 0, "Button", "&>>") 'vbNullString)
    19.   If hwnd = 0 Then
    20.     MsgBox "Couldn't find the button"
    21.     Exit Sub
    22.   End If
    23.  
    24.   Call SendMessageBynum(hwnd, WM_CHAR, 13, ByVal 0)
    25.   MsgBox "key sent"
    26.     MsgBox hwnd
    27. End Sub

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,254

    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

  7. #7

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Using SendMessage to send "enter" to an app

    BM_CLICK worked! Thanks Edgemeal!

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    Unhappy Re: Using SendMessage to send "enter" to an app

    Quote Originally Posted by BrianPaul View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width