/    Sign up×
Community /Pin to ProfileBookmark

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

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:


————————————————————————
[CODE]<!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>[/CODE]

————————————————————————

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

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@jnosterJan 08.2010 — Honestly there are a lot of things that could be killing this. Once there is an error in JS it simply quits working in some cases. It could be other JS that is written into the page, it could be conflicting variables, it could be the order that the script is presented (remember that js is, for the most part, a top-down script), meaning that if the script is presented before a referenced <div> (for example) is made available going from top-down, the script will break.

These are some ideas, but unless someone smarter than me has anything to add, I think we would have to see it in a larger context.
Copy linkTweet thisAlerts:
@jnosterJan 08.2010 — Further, some things to watch for in the JS would be...

1.the order of the script vs. the order of the HTML

2.the use of an onLoad somewhere that fires functions elsewhere (there could be conflicting "onLoad" commands.
Copy linkTweet thisAlerts:
@AngelusWebDesigauthorJan 08.2010 — Another thing I should add: I'm not using any libraries.

As far as Javascript errors, I know what you mean. If you have the "right kind" of error, everything will stop running. Usually there are no errors reported by Firefox (I turned on the Status Bar so I could keep an eye on such things, and be able to run Firebug)

But wouldn't Javascript-related errors cause IE to have the same problem(s)? But this problem never happens in IE.
×

Success!

Help @AngelusWebDesig spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...