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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Firefox keyboard capture - works by itself, but not in my program

Status
Not open for further replies.

AngelusWebDesign

Programmer
Jan 8, 2010
1
US
I can't seem to capture keystrokes (via onkeydown) in Firefox.

Now before you judge too quickly, here is a snippet that DOES work in Firefox -- but only by itself; not when pasted into my program:

------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<script language=javascript>
<!--
function vProcessKeyboard(e)
{
e = e || window.event;
var code = e.keyCode || e.which;

document.getElementById("chardisplay").innerHTML = code;
}
//-->
</script>

<body>

<script language=javascript>
<!--
document.onkeydown = vProcessKeyboard
//-->
</script>

<div id="chardisplay"></div>

</body>
</html>
------------------------------------------------------------------------

Here's my question: What would cause the above code to fail, when used in a larger, more complicated webpage?
If I debug my webpage (using FireBug 1.4.5), I note that "onkeydown" indeed points to my keyboard handler. But when I put a simple debug message in that handler, I get absolutely nothing -- no matter what code I paste in the keyboard handler, it doesn't run. No matter how many keys I press!

I run the same program in Internet Explorer and it runs fine. I run the keyboard code by itself in Firefox and it runs fine.

I've spent about 10 hours Googling this, and all I can find is discussions of how to capture keystrokes in Firefox vs. IE. I found about 20 different variations on the code I pasted above.

The "program" I'm trying to use with this keyboard capture code with is a large browser game -- a glorified webpage, even if it behaves like a Windows game -- with hundreds of objects (divs). There are several layers of graphics to make the game happen.

I'm just trying to figure out what is FUNDAMENTALLY different between my large program and a simple webpage, when it comes to something simple like keyboard capture.

Is it the size, in bytes, of my webpage? Is it the large number of DIVs? The large number of functions? Incidentally, I'm not using Frames.

Any help would be much appreciated.

Thanks,

Matthew
 
Perhaps you have some other code that captures the document's onkeydown event after yours, and so yours isn't run?

Perhaps you have a JS error somewhere on your page that is stopping your handler from working (check out Firefox's JS error console by clicking Tools -> Error Console - it sometimes shows up more errors than Firebug).

I think that if your HTML validates, then post either a URL to your code, or a cut down test page illustrating the problem. Sometimes, creating a test page can help you identify the problem anyway.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top