How to do it...

  1. To begin, we need to define our FInputAxisKeyMapping or FInputActionKeyMapping objects, depending on whether you are hooking up an Axis key mapping (for buttons that are held down for input) or an Action key mapping (for one-off-event buttons that are pressed once for input).
    1. To use either of the following classes, we will need to include the following .h file:
#include "GameFramework/PlayerInput.h" 
    1. For Axis key mappings, we define an FInputAxisKeyMapping object, as follows:
FInputAxisKeyMapping backKey( "Back", EKeys::S, 1.f ); 
    1. This will include the string name for the action, the key to press (use the EKeys enum), and whether or not Shift, Ctrl, Alt, or cmd (Mac) should be held to trigger the event.
    2. For action key mappings, define FInputActionKeyMapping, as follows:
FInputActionKeyMapping jump("Jump", EKeys::SpaceBar, 0, 0, 0, 0); 
    1. This will include the string name for the action, the key to press, and whether or not Shift, Ctrl, Alt, or cmd (Mac) should be held to trigger the event.
  1. In your player Pawn class's SetupPlayerInputComponent function, register your Axis and Action key mappings to the following:
    1. The PlayerInput object connected to a specific controller:
GetWorld()->GetFirstPlayerController()->PlayerInput
->AddAxisMapping( backKey ); // specific to a controller
    1. Alternatively, you could register to the static member functions of the UPlayerInput object directly:
UPlayerInput::AddEngineDefinedActionMapping(jump );
Ensure that you're using the correct function for Axis versus Action Mappings!
  1. Register your Action and Axis Mappings to C++ functions using C++ code, just like we did in the preceding two recipes, for example:
PlayerInputComponent->BindAxis("Back", this, &AWarrior::Back); 
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AWarrior::Jump 
);