Results 1 to 6 of 6

Thread: Sendkeys

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2003
    Posts
    16

    Sendkeys

    I need to know how I can have my VB application wait until something is open after a shell line before the SendKeys line activates.

    thanks.

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    Different applications load differently. There is no 'finished loading' flag, meaning the best way to do what you want is to simply wait a specified amount of time before the SendKeys statement.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You can use the WaitForInputIdle function to get information when a process is ready to get input from the mouse or keyboard. However this function requires a hProcess handle which you don't get from the VB Shell function. You must eighter use CreateProcess or the ShellExecuteEx function.

    I've written a wrapper sub that uses ShellExecuteEx to launch a file and then waits for the process to be ready to get input from the keyboard before it returns.
    VB Code:
    1. Private Declare Function ShellExecuteEx Lib "shell32.dll" (ByRef lpExecInfo As SHELLEXECUTEINFO) As Long
    2. Private Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
    3. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    4.  
    5. Private Type SHELLEXECUTEINFO
    6.     cbSize As Long
    7.     fMask As Long
    8.     hwnd As Long
    9.     lpVerb As String
    10.     lpFile As String
    11.     lpParameters As String
    12.     lpDirectory As String
    13.     nShow As Long
    14.     hInstApp As Long
    15.     ' fields
    16.     lpIDList As Long
    17.     lpClass As Long
    18.     hkeyClass As Long
    19.     dwHotKey As Long
    20.     hIcon As Long
    21.     hProcess As Long
    22. End Type
    23.  
    24. Private Const SEE_MASK_NOCLOSEPROCESS = &H40
    25. Private Const SEE_MASK_FLAG_NO_UI = &H400
    26.  
    27. Public Sub ShellAndWaitForReady(ByVal sFile As String)
    28.     Dim hProcess As Long
    29.     Dim shi As SHELLEXECUTEINFO
    30.     Dim nRet As Long
    31.    
    32.     shi.lpDirectory = vbNullString
    33.     shi.lpVerb = "Open"
    34.     shi.lpParameters = vbNullString
    35.     shi.lpFile = sFile
    36.     shi.nShow = vbNormalFocus
    37.     shi.fMask = SEE_MASK_FLAG_NO_UI Or SEE_MASK_NOCLOSEPROCESS
    38.     shi.cbSize = Len(shi)
    39.    
    40.     If ShellExecuteEx(shi) > 32 Then
    41.         hProcess = shi.hProcess
    42.         Do
    43.             nRet = WaitForInputIdle(hProcess, 500)
    44.             DoEvents
    45.         Loop While nRet <> 0
    46.         CloseHandle hProcess
    47.     End If
    48. End Sub
    Just call ShellAndWaitForReady and pass it the file to launch (this could be either a program or a document that has an associated program).

    Best regards

  4. #4
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    That's a really cool API, although it is very application specific. Better than my solution, though
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by jemidiah
    That's a really cool API, although it is very application specific. Better than my solution, though
    Application specific? Yes, it only works on Windows application and of course all application might not react to keyboard input even though it's ready to do so... but to that kind of application you can't use SendKeys anyway.

  6. #6
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    I tried it on Acrobat Reader 6, and it returned during the splash screen, which is why I said app. specific (mostly I was trying to insinuate they should, of course, test it on each app. they intended to use it on)
    Last edited by jemidiah; Oct 13th, 2003 at 04:55 PM.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

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