|
-
Jun 19th, 2001, 07:46 PM
#1
Thread Starter
Hyperactive Member
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]
-
Jun 19th, 2001, 07:55 PM
#2
PowerPoster
I'm on the wrong workstation so I can't check, but I think the SendMessage API could help you there.
-
Jun 19th, 2001, 08:03 PM
#3
Thread Starter
Hyperactive Member
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]
-
Jun 19th, 2001, 09:40 PM
#4
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
-
Jun 19th, 2001, 10:51 PM
#5
Thread Starter
Hyperactive Member
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]
-
Jun 20th, 2001, 08:40 AM
#6
Thread Starter
Hyperactive Member
?
Nobody knows how to do this? Megtron?
[vbcode]
' comment
Rem remark
[/vbcode]
-
Jun 20th, 2001, 01:20 PM
#7
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
-
Jun 20th, 2001, 02:07 PM
#8
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.
-
Jun 20th, 2001, 03:36 PM
#9
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|