using chrome 15 on osx 10.6.8
When adding event listeners, I’ve found that putting preventDefault in when a keypress event is used, I get mas issues.
uncomment the line with event.preventDefault(); on it and you’ll see the keypress event stops working. what’s the deal?
preventDefault()does exactly what it’s supposed to. It stops the default action of the key. If you want the default action of the key, then don’t call it.In both Chrome and Firefox, I see repeated keypresses, even when
preventDefault()is uncommented from your fiddle. Perhaps you were losing focus.It would help us know what to suggest in it’s place if you describe more about what you’re trying to accomplish.
In your jsFiddle, you are listening to keys at the document level. By the time the keystrokes propogate up to the document, they have already been processed by the individual objects that they are targetted at. If you want to intercept and prevent keys from being processed, you have to process them in event handlers on the objects they originally are targetted at. You can’t prevent default on keys from the document level. In this jsFiddle where I intercept the keystrokes on the first object they go to, you can see that they are entirely blocked. You may also want to be aware that cross-browser behavior is not perfect in this regard as there are some browser differences you can see here.