Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyboardEvent.which being deprecated #474

Open
OLBEA20 opened this issue Feb 19, 2020 · 3 comments
Open

KeyboardEvent.which being deprecated #474

OLBEA20 opened this issue Feb 19, 2020 · 3 comments

Comments

@OLBEA20
Copy link

OLBEA20 commented Feb 19, 2020

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

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

@irahov
Copy link

irahov commented Jun 29, 2020

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
Copy link

irahov commented Sep 29, 2020

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
Copy link

irahov commented Sep 30, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants