Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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++: alt text

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!

Product Version: UE 4.21
capture.png (127.6 kB)
more ▼

asked Feb 27 '19 at 07:48 PM in C++ Programming

avatar image

96 2 3 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

3 answers: sort voted first

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.

more ▼

answered Feb 28 '19 at 03:52 PM

avatar image

96 2 3 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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

alt text

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

any-key.png (6.9 kB)
more ▼

answered Feb 27 '19 at 08:10 PM

avatar image

39 3 5

avatar image TimB Feb 28 '19 at 01:03 AM

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.

avatar image BenjaFriend Feb 28 '19 at 02:37 PM

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

Call IsGamepadKey() on the items in the array


avatar image TimB Feb 28 '19 at 02:54 PM

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

avatar image BenjaFriend Feb 28 '19 at 03:18 PM

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.

avatar image TimB Feb 28 '19 at 03:54 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Feb 28 '19 at 02:51 PM

avatar image

16 1 4

avatar image TimB Feb 28 '19 at 02:56 PM

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...

avatar image BeeGeeDee Feb 28 '19 at 03:01 PM

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.

avatar image TimB Feb 28 '19 at 03:55 PM

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

avatar image BeeGeeDee Feb 28 '19 at 03:41 PM

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


(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question