Send "Enter Key" to the currently active window

In my code I'm going to have a Print dialog box appear, caused by a web page appearing
that has "<body onload="window.print();">" in it.
I want "Enter" to click the "ok" to print.
Can I do this?    thanks
LVL 16
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeff CertainCommented:
Your current approach will not work as you envision -- the window.print command will be executed as soon as the HTML body tag is loaded.

I believe what you're trying to acheive would be better implemented by the following:
1. Create an HTML button that will fire the window.print() code.
2. In the body onload event, set the focus to the HTML button.
3. When "enter" is pressed, the HTML button (with the focus) should fire.

Sorry I can't provide exact code -- not a javascript expert, and I'm at home (no references handy other than the web). However, this should be adequate to get you  through.

Let me know if this isn't quite what you intended.

MikeMCSDAuthor Commented:
thanks Jeff,
the Print dialog does appear . .
I tried putting:     SendKeys.Send("{ENTER}")   after it   . . but it clicks the
buttons in my program instead!
Jeff CertainCommented:

Again, sorry for the lack of actual code. There's a javascript wait function that sometimes help solve these issues -- even if you set it to wait for 1 ms. You may want to see if putting that right before your SendKeys statement helps.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.