Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Game Development
  2. Games

Game Input Simplified


Imagine a game character named "Bob the Butcher" standing on his own in a darkened room while hordes of mutant sausage zombies start pouring in through doors and broken windows. At this point, it would be a good idea for Bob to start blasting the sausage zombies into tiny chunks of meat, but how will Bob do that in a cross-platform game? Will the game player have to press one or more keys on a keyboard, click the mouse, tap the screen, or hit a button on a gamepad?

When programming a cross-platform game, this is the type of thing you are likely to spend a lot of your time battling with if you're unprepared for it. If you're not careful, you could end up with massive, spaghetti-like if statements or switch statements to deal with all of the different input devices.

In this tutorial, we are going to make things much simpler by creating a single class that will unify multiple input devices. Each instance of the class will represent a specific game action or behaviour (such as "shoot", "run", or "jump") and can be told to listen to various keys, buttons, and pointers on multiple input devices.

Note: The programming language used in this tutorial is JavaScript, but the technique that is used to unify multiple input devices can easily be transferred to any other cross-platform programming language that provides APIs for input devices.

Shooting the Sausages

Before we start writing the code for the class that we will be creating in this tutorial, let's take a quick look at how the class could actually be used.

GameInput is the class that we will be creating, and you can see how much simpler it will make things. The shoot.value property is a number and will be a positive value if the space bar on a keyboard is pressed or the right trigger on a gamepad is pressed. If neither the space bar nor the right trigger is pressed, the value will be zero.

Getting Started

The first thing we need to do is create a function closure for the GameInput class. Most of the code will we be writing is not actually part of the class, but it needs to be accessible from within the class while remaining hidden from everything else. A function closure allows us to do that in JavaScript.

(In a programming language like ActionScript or C#, you could simply use private class members, but that isn't a luxury we have in JavaScript, unfortunately.)

The rest of the code in this tutorial will replace the "code goes here" comment.

The Variables

The code only needs a handful of variables to be defined outside of functions, and those variables are as follows.

The constant-like KEYBOARD, POINTER, GAMEPAD, DEVICE and CODE values are used to define input device channels, such as GameInput.KEYBOARD_SPACE, and their use will become clear later on in the tutorial.

The __pointer object contains properties that are relevant to mouse and touch-screen input devices, and the __keyboard object is used to keep track of keyboard key states. The __inputs and __channels arrays are used to store GameInput instances and any input device channels added to those instances. Finally, the __mouseDetected and __touchDetected indicate if a mouse or touch screen has been detected.

Note: The variables don't need to be prefixed with two underscores; that is simply the coding convention I have chosen to use for the code in this tutorial. It helps to separate them from variables defined in functions.

The Functions

Here comes the bulk of the code, so you may want to grab a coffee or something before you begin reading this part!

These functions are defined after the variables in the previous section of this tutorial, and they are defined in order of appearance.

The main() function is called at the end of the code—that is, at the end of the function closure we created earlier. It does what it says on the tin and gets everything running so the GameInput class can be used.

One thing I should bring to your attention is the use of the requestAnimationFrame() function, which is part of the W3C Animation Timing specification. Modern games and applications use this function to run their update or rendering loops because it has been highly optimized for that purpose in most web browsers.

The update() function rolls through the list of active GameInput instances and updates the ones that are enabled. The following updateInput() function is quite long, so I won't add the code here; you can see the code in full by downloading the source files.

The updateInput() function looks at the input device channels that have been added to a GameInput instance and works out what the value property of the GameInput instance should be set to. As seen in the Shooting the Sausages example code, the value property indicates if an input device channel is being triggered, and that allows a game to react accordingly, perhaps by telling Bob to shoot the mutant sausage zombies.

The updateValue() function determines if the value property of an GameInput instance should be updated. The threshold is primarily used to prevent analog device input channels, such as gamepad buttons and sticks, that don't reset themselves properly from constantly triggering a GameInput instance. This happens quite often with faulty or grubby gamepads.

Like the updateInput() function, the following updatePointer() function is quite long so I won't add the code here. You can see the code in full by downloading the source files.

The updatePointer() function updates the properties in the __pointer object. In a nutshell, the function clamps the pointer's position to make sure it doesn't leave the web browser's window viewport, and it calculates the distance the pointer has moved since the last update.

The mouseDetected() and touchDetected() functions tell the code to ignore one input device or the other. If a mouse is detected before a touch screen, the touch screen will be ignored. If a touch screen is detected before a mouse, the mouse will be ignored.

The pointerPressed(), pointerReleased() and pointerMoved() functions handle input from a mouse or a touch screen. All three functions simply update properties in the __pointer object.

After those three functions, we have a handful of standard JavaScript event handling functions. The functions are self-explanatory so I won't add the code here; you can see the code in full by downloading the source files.

The inputAdd(), inputRemove() and inputReset() functions are called from a GameInput instance (see below). The functions modify the __inputs and __channels arrays depending on what needs to be done.

A GameInput instance is considered active, and added to the __inputs array, when an input device channel has been added to the GameInput instance. If an active GameInput instance has all of its input device channels removed, the GameInput instance considered inactive and removed from the __inputs array.

Now we arrive at the GameInput class.

Yep, that's all there is—it's a super lightweight class that essentially acts as an interface to the main code. The value property is a number that ranges from 0 (zero) through to 1 (one). If the value is 0, it means that the GameInput instance is not receiving anything from any input device channels that have been added to it.

The GameInput class does have a few static properties, so we will add those now.

Keyboard device channels:

Pointer device channels:

Gamepad device channels:

To finalize the code, we simply need to call the main() function.

And that's all of the code. Again, it's all available in the source files.

Run Away!

Before we wrap the tutorial up with a conclusion, let's take a look at one more example of how the GameInput class can be used. This time, we will give Bob the ability to move and jump because the hordes of mutant sausage zombies might become too much for him to handle alone.

Nice and easy. Bear in mind that the value property of GameInput instances ranges from 0 through to 1, so we could do something like changing Bob's movement speed using that value if one of the active input device channels is analog.

Have fun!


Cross-platform games all have one thing in common: they all need to deal with a multitude of game input devices (controllers), and dealing with those input devices can become a daunting task. This tutorial has demonstrated one way to handle multiple input devices with the use of a simple, unified API.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.