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

Recommended Answers

All 2 Replies

Not sure if this what you need. I've used this to capture keydown. It's not real thorough on identifying browsers, but it seems to work. Microsoft and Mozilla do it differently, as usual.

if (navigator.appName == 'Netscape') {
    window.captureEvents(Event.KEYPRESS);
    document.onkeypress = netscapeKeyPress;
}
function netscapeKeyPress(e) {
	keyCode=e.which;
	handleEvent(keyCode);
}
function microsoftKeyPress() {
	keyCode = window.event.keyCode;
	handleEvent(keyCode);
}
function handleEvent(keyCode) {
	var moveleft = false;
	var moveright= false;
	if (keyCode=="106"){moveleft=true;} //J
	if (keyCode=="107"){moveright=true;} //K
	if (keyCode=="74"){moveleft=true;} //j
	if (keyCode=="75"){moveright=true;} //k
}

I have an update for this problem --

No keypresses are capture, UNTIL I RUN Venkman's Javascript Debugger, and switch back to the window my program is running in. Then all keypresses work perfectly.

So what is Venkman initializing in my program that my program isn't?

Very strange.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.