# Getting Keyboard Input

**URL:** <https://discourse.elm-lang.org/t/getting-keyboard-input/1988>\
**Category:** Learn\
**Created:** [September 17, 2018, 1:42pm UTC](https://discourse.elm-lang.org/t/getting-keyboard-input/1988 "2018-09-17T13:42:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnBugner](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@JohnBugner](https://discourse.elm-lang.org/u/JohnBugner)\
**Post date:** [September 17, 2018, 1:42pm UTC](https://discourse.elm-lang.org/t/getting-keyboard-input/1988/1 "2018-09-17T13:42:54Z")

</div>

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](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](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](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](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](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.

---

<div class="post-metadata">

**Author:** ![Sidney](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/sidney/32/3290_2.png) [@Sidney](https://discourse.elm-lang.org/u/Sidney)\
**Post date:** [September 17, 2018, 4:22pm UTC](https://discourse.elm-lang.org/t/getting-keyboard-input/1988/2 "2018-09-17T16:22:59Z")

</div>

> [@JohnBugner](#):
>
> Browser.Events tells me to read this: [https://github.com/elm/browser/blob/1.0.0/notes/keyboard.md](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’. […] If I want the _key itself_ , no matter what character is produced according to the layout, I must use ‘code’.

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.

---

<div class="post-metadata">

**Author:** ![JohnBugner](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@JohnBugner](https://discourse.elm-lang.org/u/JohnBugner)\
**Post date:** [September 18, 2018, 8:51am UTC](https://discourse.elm-lang.org/t/getting-keyboard-input/1988/3 "2018-09-18T08:51:41Z")

</div>

It says:

> As of this writing, it seems that the `KeyboardEvent` API recommends using [`key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/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](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent), everything else is deprecated. So `charCode` , `keyCode` , and `which` are only useful if you need to support browsers besides [these](http://caniuse.com/#feat=keyboardevent-key).

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`.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [September 28, 2018, 9:04am UTC](https://discourse.elm-lang.org/t/getting-keyboard-input/1988/4 "2018-09-28T09:04:55Z")

</div>

Closed on September 28, 2018, 9:04am UTC

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