I have found the following code on the internet, when I saw this solution I wondered if this key code is the same for all browsers.
var CalendarFilter = Backbone.View.extend({
// ...
events: {
'click .filter': 'filter',
'keypress input[type=text]': 'filterOnEnter'
},
filterOnEnter: function(e) {
if (e.keyCode != 13) return;
this.filter(e);
},
filter: function(e) { /* ... */ });
}
});
Just a doubt, thanks.
First of all,
charCodeis notkeyCode!charCodefollows the ascii set, whilekeyCodeis a particular index of the key.. The different values between the two can be seen here: Keyboard Event Character Values for the Lower ASCII Character Set – O’Reilly AnswersOne major difference between
charCodeandkeyCodeis that charCode is deprecated and typically has no value in some browsers [apart from 0] when referencedFunnily enough, onkeypress seems to return the character code instead of the
keyCode, while onkeyup and onkeydown works as expected, so there may be some issues when detectingkeyCodevalues. You can test this out here JavaScript – Detecting keystrokes– Additional reference: keyCode and charCode.
keyCode,charCodeandwhichis not recommended by w3c, however, there is still legacy support for thekeyCodemodel. Solid cross browser/platform support is done with fixed virtual keyCodes that stay independent of keyboard layout – hence being “virtual”.Other Virtual keyCodes – outside of the fixed virtual keyCodes – seems to be consistently implemented across vendors as well: KeyboardEvent – Document Object Model (DOM) | MDN Virtual-Key Codes (Windows)
jQuery uses it’s own
keyCode/charCodeevent object property:.which, which attempts to unifykeyCodeandcharCode. And favorskeyCodevalues – event.which – jQuery APIIn short, your specific
keyCode: “13”, should work with most browsers that support javascript as it is a fixed virtual keycode and is consistent with all browsers and platforms