2

e.g. A keypress event will return the below

KeyboardEvent {
    charCode: 32,
    code: "Space",
    key: " ", // space character
    keyCode: 32,
    which: 32
}

Which (if any) of these are deprecated/should not be used? Code makes sense to me because it's human readable. Afaik, which is deprecated.

3stacks
  • 1,880
  • 1
  • 16
  • 21
  • `key` gives `c` and `C` respectively when shift is (not) pressed, while `code` returns `KeyC` for both cases. Furthermore, this ignores the user's keyboard layout. – SOFe Jan 10 '21 at 15:20

3 Answers3

3

I just did some research... According to mdn (https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent) we should use key "If available".

char is deprecated

charCode is deprecated

keyCode is deprecated

which is deprecated

code is not deprecated, so I assume we use this if key is not available.

3stacks
  • 1,880
  • 1
  • 16
  • 21
1

I'm looking in the debugger and tracing out an event object in Angular 2. You would clearly use keyCode. I'm pretty sure that's what the docs say too. Interested where this conversation goes. However note I use keyCode and get exactly what I need from all keys (enter shift etc).

It makes sense I guess; you want the key, use the keycode. You want the character, use charCode. You might intuit them as the same but it would seem they're not (or at least, not always).

Just to add all the detail, I'm bound to the keyup event. It's probably not angular 2 specific but just sharing environment etc.

Tim Consolazio
  • 4,802
  • 2
  • 19
  • 28
  • 1
    `keyCode` is deprecated and should not be used (wherever possible). – Bryce Mar 02 '17 at 23:50
  • Can you show some documentation to that effect (that is not easily refuted)? Looking at the info in the debugger, charCode wouldn't be useful. I see a reference to MDN saying deprecated, but I also see W3C that event.key is not universally supported. The angular docs frequently mention keyCode. Another stackoverflow answer says there's key, keyCode and keyIdentifier. But again, when passing in the event, the only consistently useful thing seems to be keyCode. See http://stackoverflow.com/questions/35394937/javascript-keycode-deprecated-what-does-this-mean-in-practice – Tim Consolazio Mar 03 '17 at 11:33
  • Anyway, with all that said, I looked around and couldn't establish the deprecation as a practical and consistent fact. (Note the point here is to find out the useful practice for day-to-day dev, not to say you're wrong). – Tim Consolazio Mar 03 '17 at 11:37
0

keyCode determine which key is pressed.except keyCode includes shiftKey,altKey,ctrlKey,metaKey to detect special keys.

holi-java
  • 29,655
  • 7
  • 72
  • 83
  • Interestingly, keyCode is what I use in Angular 2. I don't see charcode on the incoming event. – Tim Consolazio Mar 02 '17 at 23:20
  • they are the same.so you must use `var key = e.charCode | e.keyCode ` – holi-java Mar 02 '17 at 23:21
  • Actually I rectract that. charCode would be the entered character. Keycode is the key pressed. They are not the same (looking in debugger right now). For example, I see charCode 0, keyCode 62. I would say keyCode is correct for knowing the key (for some reason I think I knew this already). I mean, it does kind of make sense if you think about it. – Tim Consolazio Mar 02 '17 at 23:22
  • yes,diff property for diff lowest version browsers.but now they are both could used in many browsers. – holi-java Mar 02 '17 at 23:25
  • Yeah I wasn't seeing charcode because I was doing a naive falsy check. The value is 0, so I "didn't see it". – Tim Consolazio Mar 02 '17 at 23:30