Results 1 to 8 of 8

Thread: SendKeys to a specific Windows Handle

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    39

    SendKeys to a specific Windows Handle

    Hey,

    Is it possible to send keys to a specific windows handle without having to bring that window to focus?

    Right now I have the following code almost doing the trick:

    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
        Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Integer
    
        Sub SendKeysToApplication(ByVal AppTitle As String, ByVal Keys As String)
    
            Dim hWnd As Long = FindWindow(Nothing, AppTitle)
    
            If hWnd <= 0 Then
                '*** TO DO *** Window doesn't exist exception 
            End If
    
            SetForegroundWindow(hWnd)
            SendKeys.SendWait(Keys)
    
        End Sub

    Upon calling it as

    Code:
    SendKeysToApplication("Calculator", "{1}")
    It will bring the windows calculator window to the top and set the value "1" on it.


    My question is: Is it possible to send the keystroke without poping the window?


    Thanks!

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: SendKeys to a specific Windows Handle

    SendKeys requires the window to have focus. You might be able to use the SendMessage or PostMessage Win32 APIs to achieve this.

  3. #3
    Hyperactive Member BadgerBadger's Avatar
    Join Date
    Aug 2009
    Location
    Wales
    Posts
    382

    Re: SendKeys to a specific Windows Handle

    I'm pretty sure you can use the SendMessage/PostMessage API to do so.

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    39

    Re: SendKeys to a specific Windows Handle

    Something like...?

    Sub SendMessageToApplication(ByVal AppTitle As String, ByVal Msg As String)

    Dim hWnd As Long = FindWindow(Nothing, AppTitle)

    SendMessage(hWnd, Msg, Nothing, Nothing)


    End Sub
    This is resulting in an "Arithmetic operation resulted in an overflow." upon calling the SendMessage command though.

  5. #5
    Hyperactive Member BadgerBadger's Avatar
    Join Date
    Aug 2009
    Location
    Wales
    Posts
    382

    Re: SendKeys to a specific Windows Handle

    Check out this article for some information on the parameter types and how you use them:
    http://www.developerfusion.com/artic...message-api/3/

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    39

    Re: SendKeys to a specific Windows Handle

    Thanks Badger!

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    39

    Re: SendKeys to a specific Windows Handle

    I'm back... \o/

    I've read through that documentation but I'm still stuck.

    For instance, if I want to tell another process to do Alt -> E -> H -> F (which will trigger a menu option on this other process) without bringing any of it's forms to top, what's the way to go?

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    39

    Re: SendKeys to a specific Windows Handle

    Alright so,

    I'm getting my way around SendMessage although finding object handles is proofing to be a major pain in the butt, specially for random toolbar buttons with no associated label whatsoever.

    My question is: Is it possible to provide input to a window's current focus with SendMessage? As in, I know that at a given time, a textbox will be focused on that specific form; Can I directly input on it without having to go dig around it's handle?

    Thanks!

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