Skip to content

Expose KeyboardEvent.code on jQuery event #3978

@waffledonkey

Description

@waffledonkey

Background

Keyboard events introduce new properties, .key and .code. The latter being especially useful because it normalizes keyboard layouts (eg. Dvorak) and different locales.

https://www.w3.org/TR/uievents-key/
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
https://developers.google.com/web/updates/2016/04/keyboardevent-keys-codes

Description

If you register a keydown event listener using W3 addEventListener and then console.log the event, the code property is present. If, however, you do the same thing using jQuery the code property is absent (presumably removed) -- but the key property is curiously present.

document.addEventListener('keydown', function (e) {
    console.log('W3:', e.type, e.code, e.key, e.keyCode);
});

$(document).on('keydown', function (e) {
    console.log('jQuery:', e.type, e.code, e.key, e.keyCode);
});

Results

jQuery: keydown undefined ArrowRight 39
W3: keydown ArrowRight ArrowRight 39

Link to test case

see: https://jsfiddle.net/waffledonkey/vwgu2u12/

While you're in there, IE implemented an older spec so they use 'Left', 'Right', 'Up' and 'Down' versus 'ArrowLeft', 'ArrowRight', 'ArrowUp' and 'ArrowDown' -- there may be other differences... If you still consider yourself an abstraction library I would LOVE if you could help pull IE up the hill.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions