Detect if any Key on Gamepad or Keyboard is pressed = Switching Widget

Greetings,
is there a Simple solution to detect, if any key on Keyboard or Gamepad is pressed, so i can switch my Widget depending on what was pressed ?

I have 2 Widgets (Image inside), one with keyboard controls and one for the gamepad.

What i want to archieve is, if any key on the keyboard is pressed, the widget with the controls of the keyboard should be visible and if any button on the gamepad is pressed, the other one should appear.

I know there is a “any key” for the keyboard in the inputs aviable, but how would this work for the gamepad ?

I have no PlayerController, if this is important to have/know.

1 Like

This is very simple.

Безымянный.png

3 Likes

Thank you alot sir :smiley: !!

This works well, except for the when Input Mode is set to Game & UI and you use the arrow keys/directional gamepad. Anyone know why?

I’m using 4.24.

2 Likes

did you ever figure this out? im stuck in the same spot.

Unfortunately, I just ended up never switching Input Modes (and having to roll my own UMG menu navigation code because of that).

Any ideas how it can be done in c++?

ok so in case somebody needs it

void ACCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent, character);
PlayerInputComponent->BindAction(“DetectKey”, IE_Pressed, this, &ACCharacter::KeyPressed);
}

void ACCharacter::KeyPressed(FKey key)
{
if (key.IsMouseButton() || !key.IsGamepadKey())
{
// this is keyboard or mouse
} else if (key.IsGamepadKey())
{
// this is gamepad
}
}

DetectKey action here is a list of any possible buttons which are supported by UE4.
And it was a real pain to add all these keys with hands (excepting keyboard. u can use Any Key value) in the editor.

In case anybody else knows how to make it better please reply to me.

3 Likes

I think you could try this:
FSlateApplication::LastUserInteractionTimeUpdateEvent or FSlateApplication::GetLastUserInteractionTime()

1 Like