By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Catching "onKeyPress" events

P: n/a

I am struggling to catch kestrokes within an Internet Explorer 6 window.

My window happens to be displaying three frames, though I suspect a
similar problem would arise with a single document.

The "<body.." tag in each frame includes an "onKeyPress" handler to
catch and act on key presses. And if I focus the window by clicking
on the content of one of the documents, keystrokes are sent to the
handler on its "<body..." tag.

BUT...

If I focus the window by clicking somewhere that does not have any
document content, the window gets focus, but key strokes just
disappear --- they are not caught by any of the "onKeyPress" event
handlers.

And in particular, when I first open the browser window (from
Javascript in another window), and focus it with window.focus(),
the window gets focus (is exposed, changes border etc.), but the
keystrokes are not caught by the "onKeyPress" event handler on the
"body" of any of the documents within it.

So what's the trick? How can I catch all the key strokes in a window,
not just within the body of the document?

Is there a standard way to do this?

Robert.

--
|_) _ |_ _ ._ |- | So what? It's easier for me, so I'll do it!
| \(_)|_)(-'| |_ |
deadspam.com is a spamtrap. | What's wrong with top posting?
Use bcs.org.uk instead. | It makes it hard to see comments in context.
Sep 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Robert Inder wrote:
I am struggling to catch kestrokes within an Internet Explorer 6 window.

My window happens to be displaying three frames, though I suspect a
similar problem would arise with a single document.

The "<body.." tag in each frame includes an "onKeyPress" handler to
catch and act on key presses. And if I focus the window by clicking
on the content of one of the documents, keystrokes are sent to the
handler on its "<body..." tag.

BUT...

If I focus the window by clicking somewhere that does not have any
document content, the window gets focus, but key strokes just
disappear --- they are not caught by any of the "onKeyPress" event
handlers.
<snip>

Well, I guess the easiest thing to do would be to put some
content there. I guess these are like blank stub pages
your using to initialize the frames.

if you have <body onclick="foo()"></body>
changing that to <body onclick="foo()"> </body>

might make it work, or else you could add a DIV
something like

<div
style="position:fixed;z-index:0;display:block;left:0px;top:0px;width:100%; height:100%;background-color:transparent;"/>

The idea in both cases is to have this (blank page) used for
initializing frames not really be blank.

Sep 16 '06 #2

P: n/a

Robert Inder wrote:
I am struggling to catch kestrokes within an Internet Explorer 6 window.

My window happens to be displaying three frames, though I suspect a
similar problem would arise with a single document.
You would suspect correctly.
>
The "<body.." tag in each frame includes an "onKeyPress" handler to
catch and act on key presses. And if I focus the window by clicking
on the content of one of the documents, keystrokes are sent to the
handler on its "<body..." tag.

BUT...

If I focus the window by clicking somewhere that does not have any
document content, the window gets focus, but key strokes just
disappear --- they are not caught by any of the "onKeyPress" event
handlers.
Just to clarify, based on my testing of your problem I believe you are
talking about clicking on something like the address bar (actually the
only part of IE I can find that exhibits the behaviour you specify IS
the address bar). It's part of the browser, but not part of the
displayed webpage(s).

At this point what happens is while the OS puts focus on the browser
window (not to be confused with javascript widow, they are only loosely
coupled), the body (which is where you attached the event method) does
not have focus. Therefore it doesn't capture or transmit the keypress
event any longer. Not until you click somewhere within the browser's
display area.
>
And in particular, when I first open the browser window (from
Javascript in another window), and focus it with window.focus(),
the window gets focus (is exposed, changes border etc.), but the
keystrokes are not caught by the "onKeyPress" event handler on the
"body" of any of the documents within it.

So what's the trick? How can I catch all the key strokes in a window,
not just within the body of the document?
AFAIK you can't with javascript. That's not your (as a webpage
developer's) domain, meaning it doesn't belong to you and you can't (or
at least shouldn't) have any control over it.

>
Is there a standard way to do this?

Robert.

--
|_) _ |_ _ ._ |- | So what? It's easier for me, so I'll do it!
| \(_)|_)(-'| |_ |
deadspam.com is a spamtrap. | What's wrong with top posting?
Use bcs.org.uk instead. | It makes it hard to see comments in context.
Sep 17 '06 #3

P: n/a

Thanks of responding (and you to, "Dr. Clue" -- see below).
>>>>Tom Cole writes:
Subject: Re: Catching "onKeyPress" events
Date: 16 Sep 2006 18:10:34 -0700
>If I focus the window by clicking somewhere that does not have any
document content, the window gets focus, but key strokes just
disappear --- they are not caught by any of the "onKeyPress" event
handlers.
Just to clarify, based on my testing of your problem I believe you are
talking about clicking on something like the address bar
No, I'm having problems WITHIN the browser window.

One frame is a header bar, filled with an image, and clicking anywhere
within that frame always gets characters caught by that documents
onKeyPress handler.

But the second frame contains a block of text of varying length. If the text
finishes half way down the space available, clicking in the white
space below the end of the text results in key strokes not being caught.
And the third frame contains a variable amount of information at the
top, and a "footer" placed at the bottom by style sheet. Clicking
on either chunk of information works, but clicking in the gap between
them throws up the problem!

In both cases, the border of the browser window shows that Windows
believes it has focus, but key presses are not caught even though every
frame contains a document with an "onKeyPress" handler on the body.

Clicking on the Windows header bar transfers control to the window,
but does not change focus within it: if keystrokes were previously
getting caught by a document, they are again. But if they were getting
lost before, they continue to disappear.

Now, having said that clicking in the document window but
off the end of the text that makes up the document means that
keystrokes are lost, I have just created a test document where it
doesn't! I'm guessing that the difference/problem is caused by the
real pages having DTDs, or using style sheets, or something....

Tracking this down isn't going to be fun, is it :-(

And I think, even though Dr. Clue underestimated the
sublety/complexity of the problem, I'm going to have a shot at the
kind of thing he is suggesting --- creating a "div" that fills the
entire viewport, so that there is "document" everywhere...

Robert.

--
|_) _ |_ _ ._ |- | So what? It's easier for me, so I'll do it!
| \(_)|_)(-'| |_ |
deadspam.com is a spamtrap. | What's wrong with top posting?
Use bcs.org.uk instead. | It makes it hard to see comments in context.
Sep 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.