mousetrap
mousetrap copied to clipboard
KeyboardEvent.which being deprecated
KeyboardEvent.which is now deprecated. It's currently used in _characterFromEvent
.
Would it be possible to update the lib to use KeyboardEvent.key instead?
Using which
causes a number of problems with testing.
I cannot create an event in the recommended way:
new KeyboardEvent('keydown', { key:'.'})
and I'm not able to test my code since mousetrap does not call the callback (due to the usage of which
)
Can we expect this to be addressed any sooner?
I was able to set the which
property like this:
const event = new KeyboardEvent('keydown', { key:'.'});
Object.defineProperty(event, "which", {value: 190});
But yet the use of which
is adding one more buggy side effect.
function _characterFromEvent(e) {
// for keypress events we should return the character as is
if (e.type == 'keypress') {
var character = String.fromCharCode(e.which);
...
if (!e.shiftKey) {
character = character.toLowerCase();
}
return character;
}
// for non keypress events the special maps are needed
if (_MAP[e.which]) {
return _MAP[e.which];
}
if (_KEYCODE_MAP[e.which]) {
return _KEYCODE_MAP[e.which];
}
In the case of which=190
(that is a .
) _characterFromEvent will return:
-
¾
for 'keypress' event -
.
otherwise
But yet the use of which is adding one more buggy side effect.
I was not right. Since when the event is:
-
keypress
then thewhich
property contains the Unicode value of a character key pressed. -
keydown
then thewhich
property contains a system and implementation dependent numerical code.