Let’s face it: keyup is fairly meaningless. With keypress out of way let’s move onto why keyup is best left to the sit on the shelf, collecting dust. But, it is a good indicator that we should all stop using it moving forward. It’s usage is too prevalent in web sites and applications that have been built between the mid-nineties and now. The W3C specifications are sometimes a little ahead and sometimes a little behind browser implementation so somewhere down the road this may mean that browsers will do away with keypress. The keypress event type is defined in this specification for reference and completeness, but this specification deprecates the use of this event type. DeprecatedĪ big reason to avoid keypress, and maybe I should have led with this, is that keypress is deprecated in the current W3C UI Events specification: What?! It’s mind boggling I know, but Firefox will fire a keypress event when Tab or any of the arrow keys are pressed, but no other browser will do this. If you’re still not convinced here’s another reason to avoid keypress: It’s not fired consistently across browsers. Why have two methods of capturing key events when one will do? Just use keydown. This can put you in a situation where you’re needlessly debugging keypress for not firing events or where you’re managing both keypress and keydown events. So modifier keys like Shift, Control, Alt/Meta, function keys, etc won’t fire keypress events. You can cancel and stop bubbling of keypress events just like you can keydown events, but keypressonly fires for a subset of keys – keys that produce character values – whereas keydown fires on all of the keys. inserting a character, moving focusing, submitting a form, etc). Keypress fires after keydown, but still before the browser processes the key (e.g. Reasons for avoiding keypress Lacks key coverage Speaking of keypress, let’s look at why we want to avoid keypress next. This is important because as you’ll see soon this isn’t always the case for its closely related event: keypress. Consistent across browsersĪnother reason for using keydown is that it fires consistently across browsers. If you don’t want the event to propagate out (aka bubble up thru the DOM) then you can stop that by using the event’s stopPropagation method. If you don’t want to allow the letter “f” to be inserted – no problem – just cancel it using the event’s preventDefault method. CancelableĪnother reason for keydown is that it fires before the browser processes the key so you have the opportunity to cancel it and/or stop it from bubbling up thru the DOM. You’ll never miss a keyboard event for a key that is pressed with keydown. For example, it will fire for a character producing key like “f”, a modifier key like “Shift”, function keys, etc. Keydown fires for any key so it’s going to give you the most coverage of keys being pressed. In my experience, keydown is the only keyboard event worth using, keypress can be ignored entirely, and keyup is well suited for getting dusty on the shelf. Keydown is the only event you need to use
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |