How to get Key in C++?

Greetings, fellow engine users. As above stated, does anyone know how to get the ‘key’ as in the FKey (I think) in C++, as shown below? I have scoured the web and this answerhub for solutions but everything is always in blueprint (does anyone know why that is anyhow? Aren’t we supposed to use CPP eventually? Anyhow, an answer would be ineffably helpful. I’ve tried all kinds of ways but the all fail miserably (and some of the source documentation for more esoteric tasks are the vaguest things in the world and almost completely unhelpful). Thanks in advance!

I’m not aware of any way to do this in C++, but what you could do is have a Blueprint inherit from a C++ class that has a function to handle the “Any Key” logic (and takes in a const FKey reference as a parameter). Then in the Blueprint’s event graph you can listen for the “Any Key” event and call that function.

There is a function in PlayerController called IsInputKeyDown which takes a FKey argument. There is also WasInputKeyJustPressed and WasInputKeyJustReleased. It relies on a member called PlayerInput which seems to be public so you can access it as well and all it’s functions which store the current state of the controller’s input.

1 Like

OK here’s my idea:

First grab a list of all possible keys and store it using EKeys::GetAllKeys which take a reference to a TArray. Now you have all possible keys.

Now, each time you want to check for your Any Key, you loop through the array and call IsInputKeyDown and when you get a true you can respond to it knowing exactly which FKey was pressed. FKey has some helper functions to help determine what type of key it is. FKey is declared in InputCoreTypes.h.

1 Like

I actually need the FKey as a return. I cannot manage to convert a key press to an FKey. There is no method in InputComponent, although maybe there is one in player controller–I shall see. Thank you anyway, though.

I think it might work, except would it not be a waste of resources, especially when there is already key event called “AnyKey” that I can bind to. The only issue is, how do you convert from a bound inputactionevent to a FKey? How does one even set an FKey? There is no documentation at all…

All the possible FKeys are already defined for you in the EKeys struct. Looping over them almost certainly will not be a big resource impact. You only need to obtain the TArray once and the loop itself it unlikely to be a big performance impact.

I will try it. Thank you.

For future reference, you can bind AnyKey event, which can have a parameter FKey passed in

330388-bindanykey.png

330387-anykeyfunction.png

You set up the “AnyKey” input in project settings

330389-anykeybind.png

5 Likes