Get keyboard input from user in C++

Hello. I would like to do the following logic in my Pawn:

if (user presses key A) {
//do this action
}
else if (user presses key B) {
//do this action
}
else if (user presses key C) {
//do this action
}

I prefer to do this purely in C++, as much as possible. My biggest issue is that I can’t seem to find a way how to take in the user input. I’ve tried using PlayerController and looking at other answers from Check if key pressed - #3 by Bojann and How to get Key in C++? but nothing seems to do the trick :frowning:

And to be fair I’m doing this because I don’t like the blueprint looking like this. It’s too messy.

PS. I would also like to stay away from action mappings because that will be depreciated soon(?)

1 Like

But what stops you from doing it as I explained in that thread?

void YourPlayerController::KeyPressed(FKey key)
{
    // do the switch on FKey here?
}

Just make sure to check if Key.GetFName() is A (or something like that), and there you go. If it confuses you, just print it out, so that you know how to compare it. Altought I am qutie sure Unreal Engine has those stored as constants somewhere :slight_smile:

Hello! Thank you for taking your time to reply. To be honest I forgot what kept me from successfully implementing your solution. Maybe it was because I didn’t want to use Action Mappings (I guess it’s really inevitable!)

Upon your reply I attempted to do it again:

I set Action Mappings to any key:

image

Thank you very much!

4 Likes