A good keyboard input class is important for browser based games. It must support temporal queries, for example, was the key just pressed or is it being held? Read on to learn how the Javelin library processes keyboard events providing a simple, efficient, and powerful keyboard API designed for games.
Recently there was a question on stackoverflow asking how to properly use keyboard events for games . A good answer was provided but with a just bit more code your keyboard class can allow for temporal queries, making it possible to detect a variety of keyboard events including holds, releases, presses.
I’m going to start at the lowest level, the internal state of our keyboard class.
NOTE: I’ll be highlighting code snippets, the complete code can be found here
1 2 3 | |
We keep two maps from keyboard code to boolean (true corresponding to key down). One encodes the current frame’s state and the other encodes the previous frame’s state.
At the start of each frame the following code executes:
1 2 3 4 5 6 7 8 | |
The above code swaps the current frame’s keyboard state for the previous frame’s. At this point the two keyboard states match. Immediately afterwards, the current frame’s keyboard input is processed:
1 2 3 | |
We now have both the previous and current frame’s keyboard state. We need one internal query:
1 2 3 4 5 6 7 8 | |
This returns true if and only if keyCode is down for the
given keyboardState.
We can build on _isDown and add two methods:
1 2 3 4 5 6 7 8 9 | |
Conversely, isUp and wasUp are introduced. Given the above
methods we can now make more complex, temporal, keyboard queries:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
We’ve gone from just isPressed to many possible queries. Next time I write about
keyboard input, I will discuss detecting chords (simultaneous key presses) and sequences.