Getting Keyboard Input

I was trying to do keyboard input in 0.19. I discovered 2 things:

(1) packages.elm told me to look at Browser.Events ( https://package.elm-lang.org/packages/elm/browser/latest/Browser-Events ). Browser.Events told me to look here: https://github.com/elm/browser/blob/1.0.0/examples/wasd.md , which doesn’t exist. So I instead looked at https://github.com/elm/browser/blob/master/examples/wasd.elm , which does exist, but doesn’t work with 0.19. After editing it a bit, it works, but poorly. Keep reading to find out why.
(2) Browser.Events tells me to read this: https://github.com/elm/browser/blob/1.0.0/notes/keyboard.md , which tells me that I should only ever use ‘key’ for the decoder function, and never ‘keyCode’, ‘code’, ‘charCode’, or ‘which’. This is a mistake. ‘key’ gives only the character produced by the key downed/pressed. If I want the key itself, no matter what character is produced according to the layout, I must use ‘code’. This distinction is very important to people that don’t use a qwerty keyboard (such as myself). (For what it’s worth, ‘keyCode’, ‘charCode’, and ‘which’ don’t seem to work at all.)

I’m not asking for advice about anything, I just thought I should share this useful information with the people using Elm, to counter the poor advice given by the Elm documentation, and the people creating Elm, so they can correct it.

Btw, this reminds me of something Firefox did: Starting in Firefox 3 (way back in 2008), Firefox destroyed the ability to use Firefox with a non-qwerty keyboard, because they tied shortcuts to the character produced by a key, instead of the key itself. ( https://bugzilla.mozilla.org/show_bug.cgi?format=default&id=439815 ) This bug (Although some people insisted it was a feature!) festered for the greater part of the past decade (it was not fixed in 2009 despite what bugzilla says) until they fixed it only recently (within the past year or two, iirc). Please don’t do as Firefox did.

1 Like

That document never says “don’t use code”. It does advise against using ‘keyCode’, ‘charCode’, or ‘which’, just like the MDN docs. But code isn’t mentioned anywhere but that heading, as far as I can tell.

It says:

As of this writing, it seems that the KeyboardEvent API recommends using key . It can tell you which symbol was pressed, taking keyboard layout into account. So it will tell you if it was a x , , ø , β , etc.

According to the docs, everything else is deprecated. So charCode , keyCode , and which are only useful if you need to support browsers besides these.

I assumed that “everything else” included code, because the first paragraph mentions only key, the example at the bottom “Decoding for Games” uses key, and the wasd.elm example (which I was pointed to by the same page that pointed me to read that document) also uses key, when it definitely should be using code.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.