Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keep script's focus where it's should be?

Status
Not open for further replies.

cold25

Programmer
Feb 11, 2002
41
0
0
US
Hi. My script launches a scanning program and then performs some sendkeys commands and other things, too. My problem is that I need the script to execute its commands only on the program that it launched (the scanning program), and not on other programs or the computer itself, if the user changes focus with the mouse or keyboard, while the script is running. Currently, if the user changes focus after the script has begun running, the script starts performing its commands on whatever the new item of focus is.
So my question is, how might I program the script to execute it's commands only on the scanning program, despite what the user does, while the script it running? Any suggestions would be greatly appreaciated. Thanks.
cold25
 
This is why Sendkeys is considered a last-resort hack.

Normally a script should control an application via an object reference. The problem is that many applications are not written as ActiveX EXE's (like IE and Office applications are). Sometimes even when they are ActiveX EXE's they don't expose fine enough control via their object models.

These are the times people resort to Sendkeys. The problems you are encountering are just part of the deal with the devil you make though. I'm not aware of any solution and I doubt there is one.
 
If the script launches the program via the Exec method you might be able to, but I can't say that I've ever tried.

Another approach you might try is to use the AppActivate method. This method allows you to pull an application back into focus. But then, it would probably annoy the dog out of the user. :)
 
The only sure trick would require a component callable from script that retrieves the target window's handle using FindWindow() and then does SendMessage() or PostMessage() API calls to send the keys to the specific window. If this was truly viable (i.e. actually worked that easily) I'd think we'd have numerous 3rd-party components in use for this already.

I couldn't find any after searching a bit, but it doesn't mean there aren't any.

SendKeys() has several adverse effects. There are workarounds, but I haven't seen any good ones.

Replacement for Sendkeys avoids a few of the side effects.

Are you sure your "scanning program" doesn't have an Automation interface?
 
Thank you both for your suggestion, comments and links for guidance. I shall examine them all. This is my first time trying to automate a program. The person working who was working on this project prior to me showed me sendkeys about two weeks ago, thought it was the way to go, and that was my first introduction into vbs. It's a lot of experimentation and searching at this point, which is why I appreciate the help so much.

I'm not sure about the scanning program having an automation interface or not. If it did, I guess it'd say something about it in the program's menus or help somewhere? I shall keep looking. Thanks.
cold25
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top