Catching "onKeyPress" events

R

Robert Inder

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.
 
T

Tom Cole

Robert said:
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.
 
R

Robert Inder

Thanks of responding (and you to, "Dr. Clue" -- see below).
Subject: Re: Catching "onKeyPress" events
Date: 16 Sep 2006 18:10:34 -0700
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top