PDA

View Full Version : close window with anykey?


tester
04-28-2005, 10:04 PM
On a piece of test equipment, I have the option to launch a file when the program starts. At the beginning of the test, I want to display an image of test cables and connections as an aid to our factory workers.
I think html would be a great filetype to launch, because I know how to create simple pages with pictures and text. And I know all of our factory test PC's are running Windows 98 or newer, and they all have internet explorer. The intent is for the PC's to autoload my test program, and all input will be with a bar code scanner or keyboard entry.

So I've launched a page with an image and some instructions. To simplify things for production workers, I want to make the page close by pressing any key on the keyboard, rather than using a mouse or using combinations of keystrokes to return to the tester software's main menu.

I searched here and found many snippets for closing the window with a button, but is it possible to close a window by hitting one key on the keyboard? It would be great if they could hit enter or smack the spacebar to close IE.

thanks!

MikeFoster
04-28-2005, 11:48 PM
Hi tester, welcome to the forums :)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<style type='text/css'>
</style>
<script type='text/javascript'>
window.onload = function()
{
window.opener = window; // this is a hack - not sure if it is correct but it works for me
document.onkeypress = doc_onkeypress;
}
function doc_onkeypress()
{
window.close();
}
</script>
</head>
<body>
<h1>Press Any Key To Close Window</h1>
</body>
</html>

tester
04-29-2005, 03:34 PM
Thanks! It works just fine.

You made a comment about being a hack... Does that imply that these files may not work with future microsoft updates?

brothercake
04-29-2005, 04:39 PM
The ability to close a window which was not script-generated is generally considered a bug - the trick that Mike showed you of setting "window.opener = window" is what makes it work, but future versions of IE might "fix" that so it doesn't work anymore

For what you're doing it sounds like HTML Applications (HTA) would be suitable - HTAs are essentially webpages which run in their own application window and have special (higher) security privileges. Window manipulation is far more of an application-level thing than it is a document-level thing, so HTAs probably have syntax built in that's specifically designed for what you want to do.

Have a look here for an intro: http://msdn.microsoft.com/workshop/author/hta/overview/htaoverview.asp