My last blog post got me thinking… if we wanted to keep track of input over some arbitrary time range *T*, what is a way to approach the problem?

To start with, there are three types of inputs that I care about:

- Keyboards
- Mice
- Gamepads

For each of these inputs, my primary question to ask breaks down into:

- What was the starting state of the input component
- What was the ending state of the input component

Further, for each of the types, I may ask some additional questions depending on the type:

- digital – how many transitions were made over
*t*? - analog – what was the actual path over
*t*? the averaged result over*t*?

As we can see, the basic need is to perform some operation on individual components of the input over a set of those inputs that represent a time range.

## Keyboard

There are lots of different types of keyboards, but both in the US and internationally, the standard keyboards do not have any more than 105 keys.

Great! At any given moment in time, any combination of keys can be down.

This means that a 128 bit wide bitfield will be more than adequate to store this data.

## Mouse

For the mouse, well, some mice have LOT of buttons. Of course, many of those buttons come across as actual keystrokes. Regardless, we know we need the following data:

- The (x, y) coords of the mouse
- The button state of some number of supported buttons
- The rotation of the mouse wheel (x, y)

For now, we can choose the following data layout:

- 32 bits for the x-coordinate
- 32 bits for the y-coordinate
- 32 bits for each of the mouse wheel coords (64 bits)
- A bitfield for each of the supported buttons, let’s say 7 buttons.

So, for a mouse, we can store the data in a 136 bits without any trouble at all.

## Gamepad

If we take a look at the basics of an Xbox or PlayStation controller, you can see the following controls:

- Two thumb-sticks (analog)
- Two triggers (analog)
- Two bumpers
- 8-way directional d-pad
- 4 primary buttons
- Three auxiliary buttons (“select”, “start”, “power”)
- Two buttons on the thumb-sticks

The digital buttons account for 19 bits. The analog inputs account for 6-axises of input data. If we use 16 bits for each axis, then the analog components need 96 bits.

The gamepad can be represented in 120 bits.

## Keeping the History

Now that we have a strategy to handle storing the input in a somewhat compact way, it is good to look at storing the history of the inputs over time. Recall that a *behavior* is about a continuous value for the input.

So how much space is that?

Originally, I was thinking that this would be a bit impractical‚Ä¶ however, after thinking about it a bit more, especially in the context of this compact storage, this really doesn’t seem so bad.

Here’s the math so we can talk about it more deeply:

document.addEventListener(“DOMContentLoaded”, function() {

var tangle = new Tangle(document.body, {

initialize: function () {

this.KeyboardInputSizeInBits = 128;

this.MouseInputSizeInBits = 136;

this.GamePadInputSizeInBits = 120;

this.KeyboardInputs = 1;

this.MiceInputs = 1;

this.GamePadInputs = 1;

this.KeyboardSampleRate = 60;

this.MouseSampleRate = 60;

this.GamePadSampleRate = 60;

this.CaptureTimeInMinutes = 120;

},

update: function () {

function SizeInMB(BitsPerSample, SamplesPerSecond, TimeInMinutes) {

var BitsPerSecond = BitsPerSample * SamplesPerSecond;

var DurationInSeconds = TimeInMinutes * 60;

var BitsPerMB = 1024 * 1024 * 8;

return BitsPerSecond * DurationInSeconds / BitsPerMB;

}

var KeyboardSizeInMB = SizeInMB(this.KeyboardInputSizeInBits, this.KeyboardSampleRate, this.CaptureTimeInMinutes);

var MouseSizeInMB = SizeInMB(this.MouseInputSizeInBits, this.MouseSampleRate, this.CaptureTimeInMinutes);

var GamePadSizeInMB = SizeInMB(this.GamePadInputSizeInBits, this.GamePadSampleRate, this.CaptureTimeInMinutes);

this.MemoryUsageInMB = KeyboardSizeInMB * this.KeyboardInputs + MouseSizeInMB * this.MiceInputs + GamePadSizeInMB * this.GamePadInputs;

}

});

}, false);

**Input Sizes:** Keyboard: bits Mouse: bits Gamepad: bits

**Number of Inputs:** Keyboards: Mice: Gamepads:

**Sample Rates:** Keyboard: Hz Mouse: Hz Gamepad: Hz

**Time to Capture:** minutes

**Memory Usage:** MB

Note: The data above is completely interactive; play with it to your heart’s content.

So, with the default values above we can store two hours of continuous input^{1} data that it will only cost us about 20 MB of memory. However, this is basically the worst case scenario. Normal players are simply not going to be able to give you 60 different keyboard inputs over a second.

Instead, if we normalize the values to: keyboard = 1 Hz, mouse = 10 Hz, and gamepad = 5 Hz, now we are only talking about 1.79 MB over a two hour gameplay session.

This is starting to look a **lot** more feasible that I first suspected.

Next time, I’ll take a look at what it will take to actually process this data and some API structures we may want to use.

- Continuous in this context means 60 samples per second. ↩