Results 1 to 9 of 9

Thread: SendKeys w/o having the app have focus?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421

    Unhappy SendKeys w/o having the app have focus?

    Is there a way to send keys to an app without the app having to have focus? It's urgent, thanks.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    I'm on the wrong workstation so I can't check, but I think the SendMessage API could help you there.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    Yeah that's what I was thinking. If anyone could post up some code that sends key strokes to an app that doesn't have focus, it would be greatly appreciated. Thanks.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  4. #4
    Matthew Gates
    Guest
    VB Code:
    1. Private Declare Function PostMessage Lib "user32" _
    2. Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    3. ByVal wParam As Long, ByVal lParam As Long) As Long
    4.  
    5. Private Const WM_CHAR = &H102
    6.  
    7.  
    8. Private Sub Command1_Click()
    9.     PostMessage Text1.hWnd, WM_CHAR, vbKeyA, 0
    10. End Sub

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    Thanks Matthew, but it doesn't work when I try to pass it to another Form. It only seems to work if I pass it to a control that's already on the Form. This code does not work:

    Module:
    Code:
    Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Private Const WM_CHAR = &H102
    
    Public Sub ChatSend(lpWindowName As String, strString As String)
     Dim c$, i&, w&
     w& = FindWindow(vbNullString, lpWindowName$)
     
     If w& = 0 Then MsgBox "Window not found!", vbCritical, "Error!"
     For i& = 1 To Len(strString$)
      c$ = Mid$(strString$, i, 1)
      PostMessage w&, WM_CHAR, Asc(c$), 0&
     Next i
    End Sub
    Form:
    Code:
    Private Sub Command1_Click()
     ChatSend "Form1", "WASSUP!!"
    End Sub
    It doesn't send "WASSUP!!" to Form1, even though a TextBox on Form1 has focus.

    Any other ideas? Thanks.
    Last edited by Oafo; Jun 20th, 2001 at 08:38 AM.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421

    Question ?

    Nobody knows how to do this? Megtron?
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  7. #7
    Matthew Gates
    Guest
    Not sure if this'll work, but try it as well.


    VB Code:
    1. Private Declare Function SendMessage Lib "user32" _
    2. Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _
    3. Long, ByVal wParam As Long, lParam As Any) As Long
    4.  
    5. Private Const WM_SETTEXT = &HC
    6.  
    7. Private Sub Command1_Click()
    8.     SendMessage Text1.hWnd, WM_SETTEXT, 0, ByVal "MyString"
    9. End Sub

  8. #8
    Tygur
    Guest
    Oafo:
    The reason why that code you posted doesn't work is because you're trying to send the message to the form instead of the textbox. You have to send the message directly to the TextBox.


    And a quick note to anyone who wants to use Matthew's code as a replacement for SendKeys: That code replaces the text in the textbox with the new text instead of adding it.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    Yeah thanks, I figured it out about 2 hours after I posted. The code ended up looking like this:

    Code:
    Public Sub ChatSend(strString As String)
     Dim Chat&, Dlg&, Dlg2&, Edit&, Send&, Clk&, Dg&, Dg2&, Btn&
     Chat& = FindWindow("War2Class", vbNullString)
     Dlg& = FindWindowEx(Chat, 0&, "SDlgDialog", vbNullString)
     Dlg2& = FindWindowEx(Dlg&, 0&, "SDlgDialog", vbNullString)
     Edit& = FindWindowEx(Dlg2&, 0&, "Edit", vbNullString)
     
     Clk& = FindWindow("War2Class", vbNullString)
     Dg& = FindWindowEx(Clk, 0&, "SDlgDialog", vbNullString)
     Dg2& = FindWindowEx(Dg&, 0&, "SDlgDialog", vbNullString)
     Send& = FindWindowEx(Dg2&, 0&, "Button", vbNullString)
     
     SendMessageByString Edit&, WM_SETTEXT, 0&, strString$
     SendMessage Edit&, WM_CHAR, vbKeyReturn, 0&
    End Sub
    With all of the Declares and Consts at the top of course. Thanks for your help even though I didn't need it. Still, the last line before End Sub doesn't send the Return key. I'll figure it out.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

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