Results 1 to 24 of 24

Thread: SendKey to other process

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    Question 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

  2. #2

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    Re: SendKey to other process

    PS: Hope you understand what i mean.

    If not feel free to ask.

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    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.

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    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:
    vb Code:
    1. sendkeys textbox1.Text

    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.

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    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?)?

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    Re: SendKey to other process

    Thanks edge i will try that

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: SendKey to other process

    Quote Originally Posted by pekeltje View Post
    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    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!

  13. #13
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: SendKey to other process

    Quote Originally Posted by pekeltje View Post
    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

  14. #14

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    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:
    1. nPadHwnd = FindWindow("notepad", "Untitled - Notepad")

    Class: notepad
    Windows name: Untitled - Notepad

    But how i get class of an program?

  15. #15
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: SendKey to other process

    Quote Originally Posted by pekeltje View Post
    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.

  16. #16

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    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:
    1. 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

  17. #17

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    Re: SendKey to other process

    Have to go now,
    Its 4:50 now here so gonna sleep for how long it takes

  18. #18
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: SendKey to other process

    Quote Originally Posted by pekeltje View Post
    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:
    1. 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.

  19. #19

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    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:
    1. Dim notepad&
    2. Dim edit&
    3. notepad& = FindWindow("notepad", vbNullString)
    4. edit& = FindWindowEx(notepad&, 0&, "edit", vbNullString)

    But when i use it on the client i get:
    vb Code:
    1. Dim client&
    2. client& = FindWindow("client", vbNullString)

    Its not giving me an child handle.
    Is there another way how to get it?

  20. #20
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: SendKey to other process

    Quote Originally Posted by pekeltje View Post
    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.

  21. #21

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    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

  22. #22

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    13

    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

  23. #23
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: SendKey to other process

    Quote Originally Posted by pekeltje View Post

    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 ?

  24. #24
    Member
    Join Date
    Jun 2010
    Posts
    39

    Re: SendKey to other process

    Quote Originally Posted by Edgemeal View Post
    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
  •  



Click Here to Expand Forum to Full Width