Mapping Input in C++

Mapping input for a project is easy enough to do through the Unreal Editor [Project Settings menu][projectsettings], but sometimes it can be cumbersome to have to load up the Editor every time you want to change a setting when you are working in C++. Or you may want to map inputs quickly when working with Editor-free builds. Being able to remap inputs natively can speed up your iteration time considerably.

Input handling in UE4 is split into two parts: mapping and binding. Mapping connects raw hardware inputs into game delegates. Binding hooks up those game delegates to in-game functions. A jump ability is a good example. First, we map the Spacebar key to an Action mapping called "Jump". Then we bind the "Jump" mapping to a Jump() function in our Player Controller. Mapping inputs by directly editing config files has a few special use-cases, and it is error-prone compared to using the Project Settings menu. Use this method sparingly.

Creating an Input Config File

Your input mappings are contained in a config file called DefaultInput.ini. This file is located in the Config folder of your project. However, this file is only automatically created when you create a project from a template that contains input - the First Person Shooter Template and the Top Down Template are good examples. By contrast, "blank" projects will not have an input config file created automatically.

If you have already added input

Check that you have a file called DefaultInput.ini in the folder ~YourProjectFolder~/Config/.

If you don't find the config file

Just add a placeholder input from the Editor. The Editor will generate the config file for you.To do so, follow these steps:

  1. Start your project in the Unreal Editor
  2. Open the Project Settings menu in the Edit toolbar at the top left of the screen
  3. Select the Input submenu from the options to the left, under the Engine heading
  4. Add an ActionMapping with any name you like, with any key binding you want
  5. Close the Project Settings menu
  6. The file DefaultInput.ini should now be created in the folder structure described aboveYou should now have a DefaultInput.ini file. The remainder of our lesson will take place in this file.

Though it is important that you understand where the input config file is located, you do not always need to navigate this folder structure to reach your config file. In fact, your project's Visual Studio solution (.sln) makes the Config folder available from the solution, which can often be the most convenient access point. You can also opt to edit the config file using an external editor. We recommend Visual Studio Code.

Adding Mappings

Ini files are separated into sections. Mappings belong under the section [/Script/Engine.InputSettings]

Action Mappings

Input Action mappings are discrete events that fire on a few natural occasions, such as when a button is pressed or released. A classic example is a "Jump" binding to spacebar. We want to initiate a jump once when the spacebar is pressed and nothing more.

+ActionMappings=ActionName="Jump",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=SpaceBar) ;jump
+ActionMappings=ActionName="SuperJump",bShift=False,bCtrl=True,bAlt=False,bCmd=False,Key=SpaceBar) ;jump higher
  • ActionName is the name of the mapping. You will have to match it exactly when you bind to it.
  • Key is the key that will trigger this action.
  • bShift, bCtrl, bAlt, and bCmd are your basic modifiers.

Axis Mappings

Input Axis mappings are continuous events that occur repeatedly so long as an input occurs, such as a when a button is held or a mouse is moved. A classic example is a "MoveForward" binding to W. We want to keep moving forward so long as W is pressed. Another good example might be a helicopter - we keep applying thrust so long as the input is pressed.

+AxisMappings=(AxisName="MoveForward",Scale=1.000000,Key=W) ; move forward+AxisMappings=(AxisName="MoveForward",Scale=-1.000000,Key=S) ; move backward
  • Axis Name is the name of the mapping. You will have to match it exactly when you bind to it.
  • Key is the key that will trigger this axis.
  • Scale is the strength or direction of the axis.

You may notice that there are no modifiers (Shift/Ctrl/Cmd) for Axis mappings. Axis mappings are not concerned with modifiers. They are replaced by the Scale attribute, which allows you to have one mapping that can go a number of different ways.

The "MoveForward" mapping is a good example, as it comes with Engine templates. The "W" key gives a 1.0 scale and the "S" key gives a -1.0 scale. You can bind to the axis mapping once and simply pass different scale values to go forward or backward.

You can now add input mappings easily to your UE4 Project without ever leaving the (dis)comfort of Visual Studio. The next step is to bind your in-game responses to the mappings you set up.*[ProjectSettings]: