CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    May 2001
    Location
    London. UK
    Posts
    67

    Simulating ENTER key

    Dear All,

    Does anyone know how to send an ENTER key to an OK button on a window not owned by the vb application I'm running? I originally tried using SendInput for it but it does not seem to work. Is there an alternative method to using SendInput?

    Many TIA

    Rita

  2. #2
    Join Date
    May 2001
    Location
    London. UK
    Posts
    67
    Forgot to add that I've also tried SendKeys which also doesn't work.

    RitaO

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    If activating that app...

    ..the button has the focus, than you can
    Elseif that button has not focus, you may try with a dirty (and
    dangerous!) trick you can find in attachment
    Attached Files Attached Files
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090
    Code:
    Private Const BM_SETSTATE = &HF3
    Private Const WM_LBUTTONDOWN = &H201
    Private Const WM_LBUTTONUP = &H202
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
      (ByVal hwnd As Long, _
       ByVal wMsg As Long, _
       ByVal wParam As Long, _
       lParam As Any) 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 Command1_Click()
      Dim hwnd As Long, hchildwnd As Long, t As Long, ret As Long
      Dim FileName As String
      Dim PlayTime As Long
     'Change Try me bellow to the window title with the Ok button. 
      hwnd = FindWindow(vbNullString, "Try me")
      hchildwnd = FindWindowEx(hwnd, ByVal 0&, vbNullString, "Ok")
      If hchildwnd > 0 Then
         'restore the last button state to normal
         Call SendMessage(hchildwnd, BM_SETSTATE, 0, ByVal 0&)
         Call SendMessage(hchildwnd, WM_LBUTTONDOWN, ByVal 1&, ByVal 11&)
         Call SendMessage(hchildwnd, WM_LBUTTONDOWN, ByVal 1&, ByVal 0&)
         Call SendMessage(hchildwnd, WM_LBUTTONUP, ByVal 0&, ByVal 0&)
         Call SendMessage(hchildwnd, BM_SETSTATE, ByVal 1&, ByVal 0&)
         DoEvents
         Call SendMessage(hchildwnd, BM_SETSTATE, 0, ByVal 0&)
      End If
    End Sub

  5. #5
    Join Date
    May 2001
    Location
    London. UK
    Posts
    67
    MKSa,

    You lifesaver!!! Thanks for the excellent code. I suspected it could be solved using SendMessage but didn't know about the BM_ constants. Again Many Thanks and five stars.


    Cesare,

    Thanks for the code but I had already tried something similar. I don't know why it did not work but MKSa's code snippet has solved the problem. Many Thanks.

    RitaO

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Thumbs up To MKSa:

    Man, your code (and knowledge) is really great.

    Thanks for sharing

    Cesare Imperiali
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817
    Well since messageboxes get focus when they are created, you can also use keybd_event VK_RETURN, 0, 0, 0

  8. #8
    Join Date
    Nov 2003
    Posts
    2
    '**************************************
    'Windows API/Global Declarations for :A
    ' Send Key - Send Input
    '**************************************
    Public Const WM_CHAR = &H102
    Public Const VK_RETURN = &HD
    Public Const WM_KEYDOWN = &H100


    Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long


    ReturnValue = PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0)

  9. #9
    Join Date
    Mar 2004
    Posts
    30
    i have a similar ques:

    if i use the command shell "explorer c:\project1.vbp" the 'download file' window opens (that is, the dialog that has open, save, cancel, and more info. like when you d/l a file off the internet and u can either open or save it). i want it to go directly to the file. so my question is, can I:
    a) invoke a keypress event for O or o (therefore 'clicking' the open button).
    or,
    b) is there a better way to open the file, or invoke the open button 'click' event?

    i don't understand anything about the API so please help me if you can without using API.

    thanks alot,

    Colt

  10. #10
    Join Date
    Mar 2004
    Posts
    30
    i am still stumped as far as this one goes. any ideas????

  11. #11
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    shell "explorer c:\project1.vbp"
    with above, you're asking explorer to open the vbp file.
    Instead, you should do a kind of:
    shell "vb6.exe c:\project1.vbp" if you want to open it with vb6
    or
    shell "explorer c:\" if you want explorer to open on folder where
    your vbp is....
    Last edited by Cimperiali; May 13th, 2004 at 02:32 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  12. #12
    Join Date
    Mar 2004
    Posts
    30
    hey thanks for this code. i have some problems with this because of the directory, but i think i can sortthis out for myself.

    anyhow, thanks for the help guys,

    Colt.

  13. #13
    Join Date
    Mar 2004
    Posts
    30
    Originally posted by Cimperiali
    with above, you're asking explorer to open the vbp file.
    Instead, you should do a kind of:
    shell "vb6.exe c:\project1.vbp" if you want to open it with vb6
    or
    shell "explorer c:\" if you want explorer to open on folder where
    your vbp is....
    i've tried this and it doesn't work. have you got any ideas? here is my code:

    progpath = "vb6.exe " & "C:\Program Files\Microsoft Visual Studio\VB98\downloader program files\Project1.vbp"
    Shell progpath, vbMaximizedFocus


    any ideas?

    thnx,

    Colt

    EDIT: when i use this code it says "error 53- file not found" but i know its there.

    EDIT: i've also tried using "vb6.exe " & app.path & "project1.vbp",as well as (vb directory)\vb6.exe with the other parts. no luck.
    Last edited by Colt_777; May 31st, 2004 at 06:37 AM.

  14. #14
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    Try to put quotes around the file path..

    progpath = "vb6.exe ""C:\Program Files\Microsoft Visual Studio\VB98\downloader program files\Project1.vbp"""
    Shell progpath, vbMaximizedFocus
    Busy

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

Featured