Is there a way to send keys to an app without the app having to have focus? It's urgent, thanks.
Printable View
Is there a way to send keys to an app without the app having to have focus? It's urgent, thanks.
I'm on the wrong workstation so I can't check, but I think the SendMessage API could help you there.
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.
VB Code:
Private 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 Private Const WM_CHAR = &H102 Private Sub Command1_Click() PostMessage Text1.hWnd, WM_CHAR, vbKeyA, 0 End Sub
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:
Form: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
It doesn't send "WASSUP!!" to Form1, even though a TextBox on Form1 has focus.Code:Private Sub Command1_Click()
ChatSend "Form1", "WASSUP!!"
End Sub
Any other ideas? Thanks.
Nobody knows how to do this? Megtron?
Not sure if this'll work, but try it as well.
VB Code:
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 Const WM_SETTEXT = &HC Private Sub Command1_Click() SendMessage Text1.hWnd, WM_SETTEXT, 0, ByVal "MyString" End Sub
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.
Yeah thanks, I figured it out about 2 hours after I posted. The code ended up looking like this:
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.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