|
-
Jul 4th, 2010, 06:55 PM
#1
Thread Starter
New Member
SendKey to other process
Hello,
How can i Send a key to an other process?
As example: notepad.exe
Lets say i open notepad.exe
1 open the visual basic program
And i want to send the keys: test
Then i need to go to the opened notepad (That i opened myself) to look at it.
Is this possible?
Thanks.
Pekeltje
-
Jul 4th, 2010, 07:06 PM
#2
Thread Starter
New Member
Re: SendKey to other process
PS: Hope you understand what i mean.
If not feel free to ask.
-
Jul 4th, 2010, 07:13 PM
#3
Re: SendKey to other process
Code:
AppActivate "Untitled - Notepad"
SendKeys "Hello how are you?"
You may also pass the handle to window instead of the application title to AppActivate function.
Last edited by Pradeep1210; Jul 4th, 2010 at 07:19 PM.
-
Jul 4th, 2010, 07:42 PM
#4
Thread Starter
New Member
Re: SendKey to other process
Thanks.
Now i created an exe file with your code.
Then it focus my opened notepad and writes it.
But is it possible that it writes my notepad without focus on the window?
Because im working with a times to repeat it.
But the when its active i want to do other things while the exe keeps typing the code from sendkeys.
Pekeltje.
-
Jul 4th, 2010, 07:49 PM
#5
Re: SendKey to other process
SendKeys sends keystrokes to the active window. So it won't work when that application doesn't have the focus.
What are you trying to do? Write something into notepad?
If it is so, just simply write a temporary text file and open it in notepad using the Shell or ShellExecute.
-
Jul 4th, 2010, 07:56 PM
#6
Thread Starter
New Member
Re: SendKey to other process
Im trying to make a application that makes an mmorpg easier.
If needs to send 0 till 9 or enter to the client.
But without focus (If possible) so the user can do other things himself too.
What i got:
1 textbox
1 timer (with 30000 for every 30 seconds)
And i used:
That works but i have to focus it or your appactivate like your said.
But then im not allowed to do other things.
Because if i use as example MSN it sends the sendkey to MSN because MSN is focus.
Hope this is possible.
-
Jul 4th, 2010, 08:02 PM
#7
Re: SendKey to other process
The other ways are way too hard and would need you to have a good knowledge of windows APIs.
You would need to get the handle of window where you want to send keystrokes and set the text appropriately.
-
Jul 4th, 2010, 08:03 PM
#8
Re: SendKey to other process
Heres one I posted before, but it simulates typing so there is a delay before sending each character, just modify it. 
Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const EM_REPLACESEL = &HC2
Private CharPos As Long
Private SendString As String
Dim EditHwnd As Long
Private Sub Command1_Click()
Dim nPadHwnd As Long
' set text string
SendString = "VB Forums is the place to be!" & vbCrLf & "See you there!" & vbCrLf
' reset starting position for text.
CharPos = 1
nPadHwnd = FindWindow("notepad", "Untitled - Notepad")
If nPadHwnd > 0 Then
EditHwnd = FindWindowEx(nPadHwnd, 0&, "Edit", vbNullString)
If EditHwnd > 0 Then
Timer1.Enabled = True
End If
Else
MsgBox "Untitled - Notepad was not found!", vbInformation
End If
End Sub
Private Sub Form_Load()
Timer1.Enabled = False ' disable timer
Timer1.Interval = 32 ' set speed
End Sub
Private Sub Timer1_Timer()
Dim ret As Long
If CharPos > Len(SendString) Then
Timer1.Enabled = False
Exit Sub
End If
ret = SendMessage(EditHwnd, EM_REPLACESEL, 0, Mid$(SendString, CharPos, 1))
' Stop if user closes the window while we are sending text
If ret <> 1 Then
Timer1.Enabled = False
MsgBox "Sendmessage Failed!", vbInformation
Exit Sub
End If
CharPos = CharPos + 1
End Sub
-
Jul 4th, 2010, 08:04 PM
#9
Thread Starter
New Member
Re: SendKey to other process
Arnt there opensource API`s for this?
Or is it hard to make for someone with enough knowledge (Like you?)?
-
Jul 4th, 2010, 08:05 PM
#10
Thread Starter
New Member
Re: SendKey to other process
Thanks edge i will try that
-
Jul 4th, 2010, 08:09 PM
#11
Re: SendKey to other process
 Originally Posted by pekeltje
Arnt there opensource API`s for this?
Or is it hard to make for someone with enough knowledge (Like you?)?
Something like what Edgemeal posted is what I was referring to.
-
Jul 4th, 2010, 08:13 PM
#12
Thread Starter
New Member
Re: SendKey to other process
Thanks all.
I have to modify it now but this is exactly what i mean 
Thanks for the fast support and help!
-
Jul 4th, 2010, 08:16 PM
#13
Re: SendKey to other process
 Originally Posted by pekeltje
Thanks all.
I have to modify it now but this is exactly what i mean
Thanks for the fast support and help!
No timer....
Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const EM_REPLACESEL = &HC2
Private Sub Command1_Click()
Dim nPadHwnd As Long, ret As Long, EditHwnd As Long
Dim SendString As String
' string to send
SendString = "VB Forums is the place to be!" & vbCrLf & "See you there!" & vbCrLf
' find notepad window
nPadHwnd = FindWindow("notepad", "Untitled - Notepad")
If nPadHwnd > 0 Then
' find edit window
EditHwnd = FindWindowEx(nPadHwnd, 0&, "Edit", vbNullString)
If EditHwnd > 0 Then
' send text
ret = SendMessage(EditHwnd, EM_REPLACESEL, 0&, SendString)
End If
Else
MsgBox "Untitled - Notepad was not found!", vbInformation
End If
End Sub
-
Jul 4th, 2010, 08:34 PM
#14
Thread Starter
New Member
Re: SendKey to other process
Really thanks.
I had no idea how to fix this and you both helped me alot.
But there is still 1 think i dont understand.
vb Code:
nPadHwnd = FindWindow("notepad", "Untitled - Notepad")
Class: notepad
Windows name: Untitled - Notepad
But how i get class of an program?
-
Jul 4th, 2010, 08:40 PM
#15
Re: SendKey to other process
 Originally Posted by pekeltje
Class: notepad
Windows name: Untitled - Notepad
But how i get class of an program?
Well you could replace the classname with VBNullstring,
nPadHwnd = FindWindow(vbNullString, "Untitled - Notepad")
, though if you had other windows with the same titlebar caption that could cause a problem.
If you search for Enumerate Windows I think you should find examples on how to get a list of all open windows, their captions, classnames and handles. for classname you'd use the GetClassName API.
-
Jul 4th, 2010, 09:12 PM
#16
Thread Starter
New Member
Re: SendKey to other process
I followed this:
http://www.mvps.org/access/api/api0013.htm
It says,
Class: CLIENT
caption: SRO_Client
But it doesnt work.
This is how i got it:
vb Code:
nPadHwnd = FindWindow("CLIENT", titel.Text)
title.text is userinterface for caption (In this case SRO_Client)
The class is CLIENT wich i also editted.
Works great in notepad but when i send it to the client it doesnt work
-
Jul 4th, 2010, 09:51 PM
#17
Thread Starter
New Member
Re: SendKey to other process
Have to go now,
Its 4:50 now here so gonna sleep for how long it takes
-
Jul 4th, 2010, 09:59 PM
#18
Re: SendKey to other process
 Originally Posted by pekeltje
I followed this:
http://www.mvps.org/access/api/api0013.htm
It says,
Class: CLIENT
caption: SRO_Client
But it doesnt work.
This is how i got it:
vb Code:
nPadHwnd = FindWindow("CLIENT", titel.Text)
title.text is userinterface for caption (In this case SRO_Client)
The class is CLIENT wich i also editted.
Works great in notepad but when i send it to the client it doesnt work
Yes but are you setting the correct child handle... in notepad the text window is callled "Edit".
You should try using SPY that is on the VB6 CD to find out text window classname in the app you are trying to send text to.
-
Jul 5th, 2010, 04:54 AM
#19
Thread Starter
New Member
Re: SendKey to other process
I used this API:
http://patorjk.com/programming/tutorials/apispy.htm
When i use it on notepad i see the child handle is edit as your already said.
vb Code:
Dim notepad& Dim edit& notepad& = FindWindow("notepad", vbNullString) edit& = FindWindowEx(notepad&, 0&, "edit", vbNullString)
But when i use it on the client i get:
vb Code:
Dim client& client& = FindWindow("client", vbNullString)
Its not giving me an child handle.
Is there another way how to get it?
-
Jul 5th, 2010, 11:32 AM
#20
Re: SendKey to other process
 Originally Posted by pekeltje
Its not giving me an child handle.
Is there another way how to get it?
The arguments for FindWindow are case-senistive so make sure you type them in exactly the same as the spy shows. When you get the classname for the window you want to send text to it may be inside other containers so you may need to call FindWindowEx multiple times to drill down to its parent container.
Another way is to enumerate all the children and their info into a UDT array, then you loop through the array until you find the control you want by a classname, caption, or ID number , etc and get the handle to it, I posted an example here, once you see how it works try modifying it to search the control you are after. If all else fails Search the forum for others examples.
-
Jul 5th, 2010, 12:02 PM
#21
Thread Starter
New Member
Re: SendKey to other process
Hello,
I used this from the forum:
http://www.vbforums.com/showpost.php...80&postcount=1
It says,
Handle: 000900C8
Class: CLIENT
text: SRO_Client
Style: 96C80000
pos: 61, 4) Size: 1030x800
Path:
But still cant get it working.
I tried alot of apo`s and googled for it but nothing seems to work
-
Jul 8th, 2010, 09:45 AM
#22
Thread Starter
New Member
Re: SendKey to other process
Does anyone know a answer?
I found something how i want to like it.
http://i50.tinypic.com/eb8vpj.png
Or even better:
http://i49.tinypic.com/24oob2w.jpg
To choose the window in a list.
But the priority is to get it working without focus on the window.
To send a keypress to SRO_Client without activate it (In the meaning dont focus)
So i can do other things or start it twice.
Thanks.
Pekeltje
Last edited by pekeltje; Jul 8th, 2010 at 09:46 AM.
Reason: More demo links
-
Jul 8th, 2010, 12:36 PM
#23
Re: SendKey to other process
 Originally Posted by pekeltje
But the priority is to get it working without focus on the window.
To send a keypress to SRO_Client without activate it (In the meaning dont focus)
Maybe try WM_CHAR, like in this example ?
-
Oct 8th, 2010, 03:15 AM
#24
Member
Re: SendKey to other process
 Originally Posted by Edgemeal
No timer....
Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const EM_REPLACESEL = &HC2
Private Sub Command1_Click()
Dim nPadHwnd As Long, ret As Long, EditHwnd As Long
Dim SendString As String
' string to send
SendString = "VB Forums is the place to be!" & vbCrLf & "See you there!" & vbCrLf
' find notepad window
nPadHwnd = FindWindow("notepad", "Untitled - Notepad")
If nPadHwnd > 0 Then
' find edit window
EditHwnd = FindWindowEx(nPadHwnd, 0&, "Edit", vbNullString)
If EditHwnd > 0 Then
' send text
ret = SendMessage(EditHwnd, EM_REPLACESEL, 0&, SendString)
End If
Else
MsgBox "Untitled - Notepad was not found!", vbInformation
End If
End Sub
It's working for simple applications like notepad and etc. Help, how to find parent and child window.
Tags for this Thread
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
|