Table of Contents

Virtual input is a high-level input system, built on top of existing Input functionality shown earlier. The main difference between the two systems is that virtual input abstracts the concept of buttons and axes into virtual objects, instead of referencing them directly.

This allows the application to use virtual buttons and axes without needing to know actual hardware buttons/axes the user is using. This means same virtual buttons can be used for multiple input devices (e.g. keyboard & gamepad), as well as allow the user to manually re-map keys with no additional gameplay logic.

All virtual input is handled through the VirtualInput class, accessible through gVirtualInput(). You'll notice it shares a very similar interface with Input, with the only difference being how we represent buttons and axes.

Before we explain individual aspects, lets see a quick working example to give you a rough idea:

// Set up input configuration that maps virtual keys to actual hardware keys

Input configuration

Before we can use the virtual input system, we must first create a set of virtual buttons and axes, name them, and map them to actual hardware keys. To do this we require an InputConfiguration object, which can be retrieved from VirtualInput by calling VirtualInput::getConfiguration().

Virtual buttons

Registration

Virtual buttons can be registered by giving them a unique name, and a hardware button code they map to. Any time a particular hardware button is pressed, the virtual button will be reported as pressed as well. New buttons are registered with InputConfiguration::registerButton().

// Register a virtual button named "Forward" that maps to W and Up arrow keys

Virtual axes

Registration

Virtual axes allow you to map hardware axes (e.g. gamepad analog stick or mouse movement) to virtual axes. They are registered similarily to buttons, though InputConfiguration by calling InputConfiguration::registerAxis().

You are required to give it a unique name, and fill out VIRTUAL_AXIS_DESC structure that describes the axis. The structure allows you to choose which hardware axes to reference, as well as set other properties like sensitivity, inversion or dead zones.

Note that unlike with buttons you shouldn't call VirtualInput::registerAxis multiple times for the same virtual axis. Instead provide all hardware axes in the VIRTUAL_AXIS_DESC::type by ORing them together.