Beginner Tutorials: How to build a game in Elm — Part 2

Part 2 of 12 — Add Keyboard Support

Lucamug
3 min readJun 27, 2019

Previous: Part 1 — The Game Loop

Next: Part 3 — Add the Pause

This is what we are going to make in this part 2 of the tutorial

Add Keyboard Support

Now that we have our game loop… looping, let’s add the support for the keyboard.

We could reach for the core library Browser with its Browser.Events.onKeyPress, onKeyUp, onKeyDown but why not to leverage the nice library ohanhi/keyboard so that we don’t need to reinvent the wheel?

There are things that this library does to make our life easier:

  • Keep track of keyUp and KeyDown so that we can always access a list of keys that are pressed at each moment
  • Process the key code so that, for example, the character a is always lower case also when the key Shift is pressed

When I planned the game I started very ambitious and I wanted to have four player playing on the same keyboard. Shortly after implementing the Keyboard Support I realized that this is technically possible. The keyboard hardware has limitations about how many keys can be…

--

--

Responses (1)