Determine keyboard or gamepad in C++

I want to detect based off any key or gamepad event if the input is a keyboard or gamepad.

I want to recreate this in c++:

I can’t find that “Any Key” event anywhere in code, are those some sort of dynamic nodes?

I’ve looked through the classes PlayerController, PlayerInput, InputComponent, InputCoreTypes, KismetInputLibrary (which is where I thought the Any Key event would be). I don’t see a way to get any key that was pressed and determine what input device it is.

I’ve search through a lot of posts, but don’t see a real way to do this through code.

Any thoughts on how to detect gamepad/keyboard/mouse through c++?

Thanks for any help!

You can just make an input action mapping for any key, like this:

269825-any-key.png

You can also use [EKeys::GetAllKeys][2] and check what inputs have happened, and use that to check if it was a keyboard/gamepad

Unless I’m doing this wrong, there is no way to get the actual Key from the Any Key mapping, so you can’t determine from that.

It looks like EKeys::GetAllKeys just lists AllKeys, not what has actually be pressed that frame.

EKeys::GetAllKeys gives you a TArray of FKey items.

Call IsGamepadKey() on the items in the array

http://api.unrealengine.com/INT/API/Runtime/InputCore/FKey/index.html

Set up in Input, an action mapping like you have up there.
Then in the C++, Add

InputComponent->BindAction( TEXT( "Any Key" ), IE_Pressed, this, &ClassName::EventName );

Define an event like the following:

void ClassName::EventName()

This should fire based on your inputs.

You will have to have your C++ class inheriting from the right class. Assuming the inputs are set up correctly, this should fire the event and you should be able to handle it in the event.

I think you need a reference to an APlayerController or derived class. If you have it working in BP, why don’t you expose a C++ method to blueprint and call that from within the BP instead.

I am pretty sure you really need a different handler for each key grouping however. The keypress themselves are not passed in. It’s probably easier to set up a series of BindActions instead.

Right, but that doesn’t show if the key was actually used. GetAllKeys looks like it just returns all possible keys.

I created the BindAction on AnyKey, but that doesn’t actually show what key was pressed. So there is no way to determine if it was a keyboard, mouse, gamepad, etc…

The easiest way in that case would be a bit more involved. Set up an action in the inputs for each device, add a bindaction for each. Configure an enum or even a series of constants and add a common method that takes the constant/enum, i.e. mouse, keyboard gamepad etc.

Then from each bindAction, call with the correct constant.

Hmm… I see what you are saying now. Sorry for my confusion. I know that you can bind EKeys in your input instead of action mappings, like this:

InputComponent->BindKey(EKeys::A, IE_Released, this, &APlayerController::AFunction);

So maybe you could bind up controller/keyboard keys differently like that.

Another way would be to define your own Action mapping for “Keyboard_AnyKey” or “Controller_AnyKey” and check it that way.

Here’s a novel way around the issue. Use Axis Mapping and define the scale differently for each input:

This seems kind of hacky, but I could not find any other way to grab if any key was pressed and determine what input device that key was. So I created a BindAction for each device and and that action has every possible input type for that device. It was pretty easy to grab them from InputCoreTypes.h and paste the actions into DefaultInput.ini

InputComponent->BindAction("KeyboardAction", IE_Pressed, this, &ACroPlayerController::KeyboardAction);
InputComponent->BindAction("MouseAction", IE_Pressed, this, &ACroPlayerController::MouseAction);
InputComponent->BindAction("GamepadAction", IE_Pressed, this, &ACroPlayerController::GamepadAction);

Then I just update with an enum value what device is active.

Thanks, this is what I basically ended up doing last night. I think it will do the job.

This is basically the solution I came up with last night. Thanks!