How can I determine what key was pressed when picking up player input?

I’ve been able to assign Q,W,E and F keys to an action called CombatAction within the editors Inputs like so:

I’ve then be able to pick this up within my Character class like so:

7897-bindings.png

InputComponent->BindAction(“CombatAction”, IE_Pressed, this, AThe_Battle_of_MaldonCharacter::CombatAction);

Which runs this:

void AThe_Battle_of_MaldonCharacter::CombatAction()
{

}

All I need is to pick up whether they pressed Q,W,E or F within the method i.e.

void AThe_Battle_of_MaldonCharacter::CombatAction(char buttonPressed)
{

}

Something like that.

Any help would be great :smiley:

What you could do is this… instead of binding multiple keys to an Action Mapping, bind those keys to an Axis Mapping. The Axis Mapping allows you to assign a scale value that is returned when you press the button. Just assign a different value to each button and then do an if statement or a switch statement to handle this.

Something like:

In Editor:

Axis Mapping -

Q to scale 1.0

W to scale 2.0

E to scale 3.0

F to scale 4.0

InputComponent->BindAxis("CombatAction", this, &AThe_Battle_of_MaldonCharacter::CombatAction);


void AThe_Battle_of_MaldonCharacter::CombatAction(float Value) {
    if(Value > 3.5){
        // F Pressed
    }else if (Value > 2.5){
        // E Pressed
    }else if (Value > 1.5){
        // W Pressed
    }else if (Value > 0.5){
        // Q Pressed
    }else{
        // do default or nothing or return
    } 
}

This is untested but the docs don’t say the Axis Mapping is restricted to [-1,1] so I don’t foresee any issues.

Hopefully this works for you!

Edit: Typo =)

An interesting idea, I’ll use it for now but surely they should still have something like this:

function bool InputKey(int ControllerId, name Key, EInputEvent Event, optional float AmountDepressed = 1.f, optional bool bGamepad = false)
{

return false;

}

Only want a string or char but I’m sure there will be some documentation on it somewhere :smiley:

I’m almost certain they don’t have that. The input documentation specifically states that keys are mapped to actions or values. A Staff member can correct me if I’m wrong but I’m pretty sure a Key press passing in a char value as a parameter is not possible.

Edit:

Just finished digging around in the Runtime InputCore (link) class with no luck. I’ll keep browsing around though.

I’m a bit surprised the input event adds the values, that’s pretty interesting. Good info Marc… even thought it’s not my question I appreciate the detailed explanation.

That implementation makes sense. Thanks man.

The entire purpose of the Action and Axis mapping system is to abstract away the keys from the in game behaviors to make changing the default keys not require a big rework of your code and make user remapping of controls much easier. Exposing the specific key pressed goes against that intention.

For the moment, the expectation is that if you had 4 different combat actions you would map each key individually to CombatAction1, 2, etc. We do have some thoughts about being able to bind a parameter to the delegate so you could then bind CombatAction1 to CombatAction(1) or something like that, but we don’t have specific plans for when such a change might be made.

I have to give AnXgotta credit for a sneaky idea, but I wouldn’t recommend it. The reason for this is that if you had both Q and W pressed (for example) the axis value for the given frame would be 3 and the code would treat it as an E.

Edit: As for your question about InputKey, yes, you could circumvent the input binding system in general and in your PlayerController override InputKey yourself. I wouldn’t generally recommend it, but it is an option available to you in 4.2 (they weren’t virtual before that).

If you think about the typical mapping purpose of the keys to axis with scale is to create something like MoveForward or MoveRight axes with WASD where pressing W is forward, and S is backwards. So you make W scale +1 and S scale -1 and then if both are held they add together and you end up with 0.

It can be tricky though, because if you’re not careful and you have both W and up arrow key mapped to MoveForward with scale +1 then you actually end up with double forward movement unless you’re capping it in your code.