Results 1 to 17 of 17

Thread: vb 2005 SendKey Doesn't work!!!!

  1. #1

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    vb 2005 SendKey Doesn't work!!!!

    Hi again,

    I'm really trying to get VB 2005. I want to use sendkeys. The example given by Microsoft is:

    VB Code:
    1. Dim ProcID As Integer
    2.         ' Start the Calculator application, and store the process id.
    3.         ProcID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
    4.         ' Activate the Calculator application.
    5.         AppActivate(ProcID)
    6.         ' Send the keystrokes to the Calculator application.
    7.         My.Computer.Keyboard.SendKeys("22", True)
    8.         My.Computer.Keyboard.SendKeys("*", True)
    9.         My.Computer.Keyboard.SendKeys("44", True)
    10.         My.Computer.Keyboard.SendKeys("=", True)
    11.         ' The result is 22 * 44 = 968.

    The code works. If I modify this example to:
    VB Code:
    1. Dim ProcID As Integer
    2.         ' Start the Calculator application, and store the process id.
    3.         ProcID = Shell("c:\windows\notepad.EXE", AppWinStyle.NormalFocus)
    4.         ' Activate the Calculator application.
    5.         AppActivate(ProcID)
    6.         ' Send the keystrokes to the Calculator application.
    7.         My.Computer.Keyboard.SendKeys("22", True)
    8.         My.Computer.Keyboard.SendKeys("*", True)
    9.         My.Computer.Keyboard.SendKeys("44", True)
    10.         My.Computer.Keyboard.SendKeys("=", True)
    11.         ' The result is 22 * 44 = 968.

    (opening notepad instead of calc) it still works. however, If I try to do something that would actually have some real world use:
    VB Code:
    1. Dim ProcID As Integer
    2.         ' Start the Calculator application, and store the process id.
    3.         ProcID = Shell("C:\Program Files\Mozilla Firefox\firefox.exe", AppWinStyle.NormalFocus)
    4.         ' Activate the Calculator application.
    5.         AppActivate(ProcID)
    6.         ' Send the keystrokes to the Calculator application.
    7.         My.Computer.Keyboard.SendKeys("22", True)
    8.         My.Computer.Keyboard.SendKeys("*", True)
    9.         My.Computer.Keyboard.SendKeys("44", True)
    10.         My.Computer.Keyboard.SendKeys("=", True)
    11.         ' The result is 22 * 44 = 968.

    The AppAtivate line halts the Program with:
    System.ArgumentException was unhandled
    Message="Process '3380' was not found."

    Of course 3380 is the value returned from the shell call. I don't get it. Someone PLEASE exlain why this code doesn't work.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: vb 2005 SendKey Doesn't work!!!!

    First of all, that issue has nothing whatsoever to do with Sendkeys, so to say that SendKeys doesn't work is completely wrong. SendKeys does exactly what it is supposed to do, but it's not something you should rely on in most acses anyway.

    I think you'll find that your problem is that the Firefox window doesn't run in the actual process that you start. Try running just the Shell line and then look in the Task Manager and see what the process ID of Firefox is.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: vb 2005 SendKey Doesn't work!!!!

    Even though 2005 still has this as an example, it is a poor one.
    Dont use Shell and Appactivate, try something like this instead.
    VB Code:
    1. Dim myProcess As New Process
    2.                 myProcess.StartInfo.FileName = "CALC.EXE"
    3.                 myProcess.Start()

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: vb 2005 SendKey Doesn't work!!!!

    Actually, given that the alternative to AppActivate is to use pinvoke, I'd go with it. Having said that, it won't restore a minimised window so if that's an issue then API is the only option. I'd not use Shell though. In my opinion Runtime functions should be avoided UNLESS they add value. Avoiding the need for unmanaged code is adding value in my opinion.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: vb 2005 SendKey Doesn't work!!!!

    You're right. The issue isn't with SendKeys at all. My apologies.

    TTn's example start's the application and has the added benefit of leaving focus on the new app. Unfortunately, I’m trying to open a web page with it, and that chocks it at .start, as it can’t find the file specified. I know I didn't include that info with my first post, but I figured one thing at a time.

    Also, it is AFTER that where I run into problems.

    Being new and frustrated with VB2005, can you at least throw me a bone and tell me what you're talking about with pinvoke and the API? My impression is that .NET is "doing away" with dealing with the Windows API favoring its own flavor (called something else).

    I suppose part of the problem is that I'm jumping into the deep end of the pool with 2005 and I don’t know the intricacies of the language enough to be productive, other that raising my frustration level.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: vb 2005 SendKey Doesn't work!!!!

    "pinvoke" is "Platform Invoke", which basically means using the Windows API. The .NET Framework includes a lot of types and a lot of members but it still can't do everything. There are still certain situations where you need to resort to unmanaged code, which means using COM components via COM Interop or exported methods via Platform Invoke. Remember, you can Google any term you like and almost certainly get a detailed explanation.

    Now, may I suggest that you tell us exactly what you're doing and exactly what happens? That way we know exactly what problem we're trying to solve.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: vb 2005 SendKey Doesn't work!!!!

    TTn's example start's the application and has the added benefit of leaving focus on the new app
    For a moment anyway.
    An important side note with this, is that you cant specify a start, without that application stealing focus for at least a moment.
    Sometimes Shelling will still steal focus, even if you specify not to.
    Keeping the focus there is another thing all together.


    I thought that a WebBrowser component might help you, but...

  8. #8

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: vb 2005 SendKey Doesn't work!!!!

    The problem I’m trying to resolve:

    I want to open a web browser (either IE 6 or 7 and Firefox 1 or 2 depending on the user's preference) to a specific page. I then want to refresh it periodically (say every 5 seconds) and/or interact with controls on the page, including buttons.

    That’s it!

    I’ve been trying to Google at every step of the way. That’s how I got one of the lame shell/appactivate examples. I also found a lot of unanswered threads asking similar questions. The ones that are answered don't address the problem, or are just plain wrong.

  9. #9
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: vb 2005 SendKey Doesn't work!!!!

    Okay so you're new to 2005.
    You should be able to open the internet explorer, with the .Start method I posted. I dont know if Firefox is the same.
    VB Code:
    1. Dim startInfo As New ProcessStartInfo("IExplore.exe")
    2.         startInfo.Arguments = "www.YourLink.com"
    3.         Process.Start(startInfo)
    I believe a WebBrowser component is the way for you to go in 2005.

    However, if you want to interact with the browser directly by simulating keyboard messages, then maybe you should check out my SendKeysToWindow class below in my signature.

  10. #10

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: vb 2005 SendKey Doesn't work!!!!

    Firefox works just as well. Now about sending keystrokes ... ?

  11. #11
    Lively Member
    Join Date
    Nov 2006
    Posts
    69

    Re: vb 2005 SendKey Doesn't work!!!!

    If you are determined to use sendkeys to interact with the browser, which I don't think is the correct way to do it, then you may prefer to use sendmessage instead of send keys as it will be slightly more reliable in that you won't have to set the focus to the window and so can send messages to minimised windows. You may also want to run this in another thread as well if you are running it from a form based app, to allow user interaction, but that is another matter.

    Attached is an example of a function you could use. Copy code into a new class (for example named sendClass) and call it,

    dim s as new sendClass
    s.Send("F5")
    Attached Files Attached Files

  12. #12

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: vb 2005 SendKey Doesn't work!!!!

    Thanks. I'll look at it. You said this probably isn't the best way to do this. Why is that?

    Suggestions as to how better approach programming tasks are always welcome, encouraged and make for a good historical reference for other readers.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: vb 2005 SendKey Doesn't work!!!!

    SendKeys is unreliable because you've just got to hope that the correct control has focus to receive your simulated key strokes. It is much better to use the Windows API to interact with external applications. That way you know for sure that you're interacting with exactly the controls you intend. If you use SendKeys and the wrong control has focus you may do something really bad.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Re: vb 2005 SendKey Doesn't work!!!!

    when use sendkey, you have to let the window get focus and bring it as top window, then send keys, then return the top window back to its position. this may make the window flash on the screen. send message is better.

    bear

  15. #15
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Re: vb 2005 SendKey Doesn't work!!!!

    Quote Originally Posted by svb70
    Attached is an example of a function you could use. Copy code into a new class (for example named sendClass) and call it,

    dim s as new sendClass
    s.Send("F5")

    BTW, in the example, what's the variable: eng, do?

    it seems you declared it, but never used it. also, I guess
    Dim appHandle As IntPtr = FindWindow(share.KeyClientClassName, share.KeyClientCaption)

    share.KeyClientClassName, share.KeyClientCaption are for classname and caption?


    thanks

    bear

  16. #16
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Re: vb 2005 SendKey Doesn't work!!!!

    Quote Originally Posted by svb70
    dim s as new sendClass
    s.Send("F5")

    BTW, how can I use this to send message to a web browser control which is embedded in a vb .net program?


    thanks

    bear

  17. #17

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: vb 2005 SendKey Doesn't work!!!!

    Using an API call would be the ideal way of doing this, I agree.

    In fact, in light of all the trouble I've had trying to do it this way; I'd much rather do that.

    How?

    (BTW, during the course of this thread, I've written 80% of the app I need in VB6. I think I'd be able to use an API call in VB6 as well as .NET, so, by all means, if anyone knows how to send messages to other applications, let me know).

    One of the (other) drawbacks of using SendKeys is the necessity of knowing the title (form caption) in the other app. With a web browser, that's nearly impossible because the URL occupies it!

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