Results 1 to 9 of 9

Thread: Will SendKeys.Send ever be replaced so that it's more reliable?

  1. #1

    Thread Starter
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    685

    Will SendKeys.Send ever be replaced so that it's more reliable?

    Hello guys and gals!

    It's pretty common that we get people in here asking about sendkeys, and usually most gurus and moderators will suggest that it's not really the best idea, because it's really not.

    One of the main problems with the concept, is ofcourse stray keyboard focus.
    So it would be nice to see a sendkeys.Send, that has a managed way to activate and send input to a particular window identified by the developer,
    and also not have it interupted by the user, as sendkeys does. MSDN suggests using FindWindow and Setforegroundwindow, which is okay, but even that does'nt completely ensure focus. It would also be nice to have a method that waits for a new window, that has been invoked by the keyboard strokes, ie {Enter}. This way the automation can then continue on, in a linear manner.

    I myself wrote such an input class in VB.NET, that can also simulate mouse events.
    InputToWin
    I've found it very helpful, and stable in automating tasks, that I can already do as a user.
    Feel free to use it as a sketch, or idea template.

    PS I need a job!

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Will SendKeys.Send ever be replaced so that it's more reliable?

    What do you mean "doesn't completely ensure focus"?

    If you use SetFocus() then it should ensure complete focus.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Will SendKeys.Send ever be replaced so that it's more reliable?

    All it takes to throw your sendkeys code out of sync even with all the functions and methods you can think of is still flakey as the user can be clicking or keying data which can easily change the input focus while your code runs.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Will SendKeys.Send ever be replaced so that it's more reliable?

    But we can't help that, no matter what solution replaces SendKeys the input focus could change at any moment. Or is there a way? I wouldn't have the faintest idea.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Will SendKeys.Send ever be replaced so that it's more reliable?

    The most solid way to transfering data to another external window is to use APIs like FindWindow, FindWindowEx and SendMessage and PostMessage APIs. These directly interface with the windows message stream of the identified window so it doesnt matter whats going on in the windows session.

    @TTn, So basically you are asking if the VB Team will ever produce API-like functionality and stability with the simple SendKeys method?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Will SendKeys.Send ever be replaced so that it's more reliable?

    Yea thats how i use to do it in VB6

    Couldn't the keybd_event() API be a better way?

  7. #7
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Will SendKeys.Send ever be replaced so that it's more reliable?

    Never mind, just read the documentation on keybd_event looks like it can simulate keystrokes but wouldn't be of much help, please enlighten me if i am wrong.

  8. #8

    Thread Starter
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    685

    Re: Will SendKeys.Send ever be replaced so that it's more reliable?

    Setforegroundwindow does'nt work occasionally when the recieving window is flashing in the task bar.
    I actually lost the link to msdn, which mentions something about it, and suggested pressing the menu key first(paraphrasing), to reset focus availability.
    I had already found that by using the Ctrl-Alt combo instead, avoids having the current app in focus,
    from underlining it's menu shortcuts for no reason, as the menu key would alone. It's just cleaner that way.
    Oh and just because you've set the foreground window, doesn't mean it is forced to stay there for long if called elsewhere.

    I actually was able to help someone in these forums in desperate need of solving the problem because of a flashing window, that didn't accept focus even though it should have. Sure i guess using SendKeys.Send("%") first would work, but this should built within the method.

    There are other API that have the word Foreground in them that apply like AllowSetForegroundWindow, and apiLockSetForegroundWindow.

    I'm pretty sure that SetFocus only works on the current thread, so you'd have to try AttatchInputThread to MAYBE get that to work outside your own app. I do that with GetFocus, which can get the handle of any child, regardless of thread input.

  9. #9

    Thread Starter
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    685

    Re: Will SendKeys.Send ever be replaced so that it's more reliable?

    The most solid way to transfering data to another external window is to use APIs like FindWindow, FindWindowEx and SendMessage and PostMessage APIs
    Correct, you should always try this route first, and i included methods within InputToWin that wrap that up.
    However you'll still run into things that cannot be done with sendMessage and PostMessage. Hence the need, for using keyboard event api, which also is more friendly to beginers, that dont know about handles yet.

    TTn, So basically you are asking if the VB Team will ever produce API-like functionality and stability with the simple SendKeys method?
    Yes, basically whatever it takes.

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