Results 1 to 21 of 21

Thread: [vb6] Help in SendKeys

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    19

    [vb6] Help in SendKeys

    hello,

    I need a script to SendKeys to a program secretly the program title "Calcu"

    i searched in the forums and in google but all didnt work
    so somebody can help me plz ??

    Thx

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [vb6] Help in SendKeys

    Make a project using below code. Compile it and call it SendKeysA.exe

    Code:
    Dim ReturnValue As Long
    
    Private Sub Cpmmand1_Click()
     ReturnValue = Shell(App.Path & "\SendKeysB.exe", 1)
    End Sub
    
    Private Sub Command2_Click()
     AppActivate ReturnValue
      
     SendKeys "Hello from program A" & vbCrLf
     Exit Sub
     
     MsgBox "Press OK to do a Ctrl+M - " & Chr(34) & "^M" & Chr(34)
     SendKeys "^M", True      ' Send Ctrl + M
      
     MsgBox "Press OK to do a Ctrl+M - " & Chr(34) & "^(M)" & Chr(34)
     SendKeys "^(M)", True    ' Send Ctrl + M
     
     MsgBox "Press OK to do a Alt+S - " & Chr(34) & "%S" & Chr(34)
     SendKeys "%S", True      ' Send Alt + S
     
     MsgBox "Press OK to do a Alt+S - " & Chr(34) & "%(S)" & Chr(34)
     SendKeys "%(S)", True    ' Send Alt + S
    End Sub
    Now make another project and put a Text1 on it. Compile it and call it SendKeysB.exe

    Run SendKeysA.exe only. Click on Command1. SendKeysB.exe will be launched. Now click on Command2.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    19

    Re: [vb6] Help in SendKeys

    it works but i have a problem
    the "AppActivate" return the application to top

    i want to send the key secretly.
    and thank you for the script

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [vb6] Help in SendKeys

    You might try:

    PathToApp = "C:\Calcu.exe"
    ReturnValue = Shell(PathToApp, vbHide)

    Also, is the other program Calcu a VB project and you have access to it? If yes then just make it invisible.

    However, hiding or making invisible could have adverse effects depending on what is taking place


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    19

    Re: [vb6] Help in SendKeys

    "Calcu" is already running so i shouldn't use Shell command
    i just want to send keys to it without SetFocus on "Calcu"

    Quote Originally Posted by jmsrickland
    ReturnValue = Shell(App.Path & "\SendKeysB.exe", 1)
    AppActivate ReturnValue
    Quote Originally Posted by jmsrickland
    PathToApp = "C:\Calcu.exe"
    ReturnValue = Shell(PathToApp, vbHide)
    Suggestions ??

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [vb6] Help in SendKeys

    Maybe something like this:

    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 SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    
    Private Const WM_SETTEXT = &HC
    Private Const BM_CLICK = &HF5
    Private Const WM_SETFOCUS = &H7
    
    Private Sub Command1_Click()
     Dim PID As Long
     Dim EDITX As Long
         
     PID = FindWindow(vbNullString, "exact words on the titlebar of other application")
       
     EDITX = FindWindowEx(PID, 0&, "t", vbNullString)
         
     If PID Then
       '
       ' If here then text will be sent to the titlebar
       '
       Call SendMessageByString(PID, WM_SETTEXT, 0, "BOO!")
     Else
       If EDITX Then
         '
         ' If here then text will be sent to the textbox
         '
         Call SendMessageByString(EDITX, WM_SETTEXT, 0, "Hello from another application")
       Else
         Call SendMessageByString(EDITX, BM_CLICK, 0, "Send Some")
       End If
     End If
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    19

    Re: [vb6] Help in SendKeys

    Just change the title of the other program from "Calcu" to "BOO!"

    Note: "Calcu" program have a blank textbox but i don't know which tab it lay on
    Note: if you could help me to sendkeys to that program, that will be better

    Any suggestions ??

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [vb6] Help in SendKeys

    Just change the title of the other program from "Calcu" to "BOO!"

    I was just presenting that sample as an example and not necessarily an answer to your problem

    Note: "Calcu" program have a blank textbox but i don't know which tab it lay on
    Note: if you could help me to sendkeys to that program, that will be better


    I do not have an application with a titlebar that says Calcu


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    19

    Re: [vb6] Help in SendKeys

    Can you please explain this code??

    EDITX = FindWindowEx(PID, 0&, "t", vbNullString)

    What 0& mean ?
    What "t" mean ??

    And thank you very much

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [vb6] Help in SendKeys

    Here's something that might help you abit.

    There are two projects in the zip file. One is called App1 and the other App2. App1 is the target application and App2 is the application that sends messages to App1.

    After you unzip, load App1 project into VB and run it. Now load App2 into another VB project and run it. You will see App1 in the small textbox. Click the button below it. This puts all of the objects from App1 in the list. Click on any listing and then click one or more of the other two buttons.
    Attached Files Attached Files


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    19

    Re: [vb6] Help in SendKeys

    looks great,

    but when i try to write my program "Calcu" and then click
    "Get Objects From App1"
    nothing happens and the listbox still clear

    Note: i suggest "Calcu" program coded in C++

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [vb6] Help in SendKeys

    Did it work as is; like I gave it to you?

    Did you change the text in Textbox1 from App1 to Calcu?

    I tested it with Calculator, and a few other misc applications running and it worked in all cases.
    Last edited by jmsrickland; May 26th, 2011 at 09:59 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    19

    Re: [vb6] Help in SendKeys

    Yes it worked with calculator and i tried some programs and it works perfectly

    but with "Calcu" it give nothing

    maybe because "Calcu" built in c++ ? or it doesn't matter ??

  14. #14
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [vb6] Help in SendKeys

    It depends on the application and what you want to do. For example, I tried it on MS Paint and the only thing I could do was to change the titlebar and the status line.

    What is Calcu, anyway? If I had a copy of it I could test. Maybe the App2 program does not have the correct features (API calls, for example) to perform on Calcu. Here's anothe thing the program App2 only gets the object's handles from the top level and there are more but I didn't go down that deep. It gets more complex to do that. Everything on a program has a handle but you need to know a few things about the application so as to know what handles you need.

    One thing you can do is to run Calcu then run SpyXX (I think that is part of VB or Visual Studio). Using SpyXX you can get down to all of the handles. Copy them down. Now, do not close Calcu (if you do the handles are no longer any good) but while it is still running you can hard code these handles into the source of App2 and see what you can do with them.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    19

    Re: [vb6] Help in SendKeys

    Can you send me a copy of "SpyXX" ??

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [vb6] Help in SendKeys

    It is on the VB CD, so you should have it already.

    As it is a copyrighted program, it is illegal for others to pass it on to you.

  17. #17
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [vb6] Help in SendKeys

    Quote Originally Posted by mostafaz4 View Post
    Yes it worked with calculator and i tried some programs and it works perfectly

    but with "Calcu" it give nothing

    maybe because "Calcu" built in c++ ? or it doesn't matter ??
    All applications that run on Windows are assigned a handle number. Objects on a Form like buttons, textboxes, pictureboxes, list boxes, treeviews, listviews, everything (well, maybe not WinSock, for example) has a handle. Even menu have handles and they in turn have sub-menu items that also have handles. The small example I posted for you only deals with the very top-most object handles; like the ones I mentioned above except it doesn't get down to the other handles like menu handles.

    Before you can start manipulating an external application by using it's handles you need to understand what these handles are in reference to and what is it you need to accompolish.

    If you don't have SpyXX (or even if you do) here is an application, which goes deeper than the one I posted before, that you can use just like you would use SpyXX except here you get the source code and you can modify it to your likings until you have accompolished your task. Hope this will help.
    Attached Files Attached Files
    Last edited by jmsrickland; May 28th, 2011 at 08:18 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    19

    Re: [vb6] Help in SendKeys

    I will suicide.

    in "SpyXX"
    it bring some handles to the program but i dont know how to use them

    in "EnumAllWindows"
    every program and process in the listbox1 have its full handles in listbox2 except my prog
    i can just [Minimize,Maximize,Show,Hide,Change program title]
    but the right listbox clear with my program only.

  19. #19
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [vb6] Help in SendKeys

    Maybe you should tell me what it is you want to do with Calcu and tell me how I can get a copy of it.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    19

    Re: [vb6] Help in SendKeys

    i have an idea,

    i need a code just to
    1. bring "Calcu" to top
    2. send "F12" to it
    3. unfocus "Calcu"
    4. setfocus to the next running application

    as if im running media player so "Calcu" get a F12 then media player got focus again

    Example:
    Code:
    AppActivate "Calcu"
    SendKeys ("{F12}")
    Unfocus Calcu
    Sorry for my bad English
    Sorry for annoying
    Thank you jmsrickland =)

  21. #21
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [vb6] Help in SendKeys

    What does F12 make Calcu do?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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