mousetrap icon indicating copy to clipboard operation
mousetrap copied to clipboard

KeyboardEvent.which being deprecated

Open OLBEA20 opened this issue 4 years ago • 3 comments

KeyboardEvent.which is now deprecated. It's currently used in _characterFromEvent.

Would it be possible to update the lib to use KeyboardEvent.key instead?

OLBEA20 avatar Feb 19 '20 20:02 OLBEA20

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?

irahov avatar Jun 29 '20 11:06 irahov

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

irahov avatar Sep 29 '20 10:09 irahov

But yet the use of which is adding one more buggy side effect.

I was not right. Since when the event is:

  • keypress then the which property contains the Unicode value of a character key pressed.
  • keydown then the which property contains a system and implementation dependent numerical code.

irahov avatar Sep 30 '20 08:09 irahov