Catching Backspace and Esc on Chrome

Started by
-1 comments, last by Sik_the_hedgehog 9 years, 6 months ago

OK so, I'm writing something like a DOS simulator in javascript out of boredom. This is what I have so far:

http://sik.titandemo.de/.bludos/

The problem I have is with keyboard input. It works just fine on Firefox, but Chrome eats up backspace and esc. I guess that's because they're shortcuts (back and stop loading) but huh, you can see how this can be an issue here (and I'm not even filtering the default actions). Any idea how to get them working?

In case you wonder, keyboard input is handled by having a huge inputbox that covers the entire screen and then has opacity 0 (making it transparent), then the script just hooks on its keypress event to retrieve the keyboard input. I haven't tried with keydown yet but as you can imagine ideally I'd want to keep the repeat behavior (especially with backspace). Any suggestions?

PS: it may not work on Explorer since I didn't hook up old-style events yet. I suppose modern Explorer does support standard events, but I'm not sure so warning goes here...

PS2: yes I know you may argue that catching keys that happen to be shortcuts could be a security hazard, but for that matter being able to detect any keyboard input is a security hazard in the first place =P And I'm not even blocking the default browser actions.

EDIT: welp, turns out I had the concept of keydown and keypress wrong along all this time. Keydown is for any key, keypress is for characters only, and both have repeat behavior (I thought only keypress did). Whoops. Well, that fixes it, now character input is handled through keypress and Enter, Backspace and Esc are handled through keydown. That solves the problem.

Also if somebody else is having problems with keyboard events, I found this site which is pretty useful (in particular the form at the beginning which lets you test what events trigger):

http://javascript.info/tutorial/keyboard-events

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

This topic is closed to new replies.

Advertisement