This article is a good resource explaining the differences between keyup
, keydown
and keypress
.
The short answer is there is no easy way to handle them other than to account for the different browsers.
The way I personally handle it in a Bootstrap plugin I wrote is by creating a custom method to check which event is supported. Coincidentally a very similar method showed up in the official Bootstrap version a little while later :P
//------------------------------------------------------------------
//
// Check if an event is supported by the browser eg. 'keypress'
// * This was included to handle the "exhaustive deprecation" of jQuery.browser in jQuery 1.8
//
eventSupported: function(eventName) {
var isSupported = (eventName in this.$element);
if (!isSupported) {
this.$element.setAttribute(eventName, 'return;');
isSupported = typeof this.$element[eventName] === 'function';
}
return isSupported;
}
Later on I use it in my code to attach event handlers:
if (this.eventSupported('keydown')) {
this.$element.on('keydown', $.proxy(this.keypress, this));
}