Skip to content
This repository has been archived by the owner on Apr 5, 2021. It is now read-only.

Input binding

Arnis Lielturks edited this page Apr 9, 2019 · 5 revisions

Input binding

This sample includes a way how to map controls to specific game actions, all code related to this is here: https://github.com/ArnisLielturks/Urho3D-Empty-Project/tree/master/Source/Input

To enable this functionality, BaseApplication class contains

context_->RegisterFactory<ControllerInput>();
context_->RegisterSubsystem<ControllerInput>();

ControllerInput is created as a subsystem which will deal with all the user controls.

For instance, this sample uses these actions:

static const unsigned CTRL_FORWARD = 1;
static const unsigned CTRL_BACK = 2;
static const unsigned CTRL_LEFT = 4;
static const unsigned CTRL_RIGHT = 8;
static const unsigned CTRL_JUMP = 16;
static const unsigned CTRL_ACTION = 32;
static const unsigned CTRL_SPRINT = 64;
static const unsigned CTRL_UP = 128;

Mapping new keys to actions

each of these actions can be mapped against any keyboard/mouse/joystick key. To start mapping action to specific control, you must call this event

C++ and AngelScript

VariantMap data;
data["ControlAction"] = 1; //1 - CTRL_FORWARD, int or string can be passed
SendEvent("StartInputMapping", data);

When this event is called, ControllerInput will record the first key that was pressed after the event. One key can be used only once, if you use the same key for different action, previous action key will be cleared.

When mapping stops, InputMappingFinished event will be sent out with these parameters

C++ and AngelScript

void HandleInputMappingFinished(StringHash eventType, VariantMap& eventData)
{
    eventData["Controller"].GetString(); 
    eventData["ControlAction"].GetInt(); 
    eventData["ActionName"].GetString(); 
    eventData["Key"].GetInt(); 
    eventData["KeyName"].GetString();
}

ActionName values can be defined in the ControllerInput constructor

_controlMapNames[CTRL_FORWARD] = "Move forward";
_controlMapNames[CTRL_BACK] = "Move backward";
_controlMapNames[CTRL_LEFT] = "Strafe left";
_controlMapNames[CTRL_RIGHT] = "Strafe right";
_controlMapNames[CTRL_JUMP] = "Jump";
_controlMapNames[CTRL_ACTION] = "Primary action";
_controlMapNames[CTRL_SPRINT] = "Sprint";
_controlMapNames[CTRL_UP] = "Move up";

Reading the actual controls

To view the actual sample, see the Level class: https://github.com/ArnisLielturks/Urho3D-Empty-Project/blob/master/Source/Levels/Level.cpp C++

Controls controls = GetSubsystem<ControllerInput>()->GetControls();
if (controls.IsDown(CTRL_FORWARD)) {
    // DO something
}

Control saving

All the controls are saved in the Data/Config/config.cfg file.

Sample config.cfg content:

[keyboard]
Move_forward=119
Move_backward=115
Strafe_left=97
Strafe_right=100
Jump=32
Primary_action=-1
Sprint=1073742049
Move_up=-1

[mouse]
Move_forward=-1
Move_backward=-1
Strafe_left=-1
Strafe_right=-1
Jump=-1
Primary_action=1
Sprint=-1
Move_up=4

[joystick]
Move_forward=-1
Move_backward=-1
Strafe_left=-1
Strafe_right=-1
Jump=-1
Primary_action=-1
Sprint=-1
Move_up=-1

This file will be updated automatically when you do the input mapping.

Clone this wiki locally