keywords: [UE4]Controller and Input Related

slogan_image

How to get Touch Position on screen
  1. New C++ class inherit from GameViewportClient

  2. Project Settings -> Engine -> General Settings -> Default Classes -> set Game Viewport Client Class as your customized class.

  3. Project Settings -> Engine -> General Settings -> Default Classes -> set Game Viewport Client Class as your customized class.

  4. Override function InputTouch

    virtual bool InputTouch(FViewport* Viewport, int32 ControllerId, 
        uint32 Handle, ETouchType::Type Type, const FVector2D& TouchLocation, 
        float Force, FDateTime DeviceTimestamp, uint32 TouchpadIndex) override;
    

    Argument TouchLocation is the Touch Position.

Mobile Input - Swipe and Pinch

A plugin for Unreal Engine 4 that exposes touches and swipes on mobile devices as events in blueprints
https://github.com/getsetgames/Swipe

Ultimate Touch Components
https://www.unrealengine.com/marketplace/custom-touch-controls

Controller Rotation

By default, Controller Rotation is equal to PlayerStart Actor Rotation. So if you change the Rotation of PlayerStart Actor, Controller Rotation would be affected.

How to set the rotation of Controller:

void AController::SetControlRotation(const FRotator& NewRotation)
How to create and get Spectator

1st way

Create Spectator:

AMyGameModeBase::AMyGameModeBase()
{
    DefaultPawnClass = ASpectatorPawn::StaticClass();
}

Get Spectator:

ASpectatorPawn* APlayerController::GetSpectatorPawn() const;

2nd way

GetWorld()->SpawnActor<ASpectatorPawn>(ASpectatorPawn::StaticClass());
How to prevent cursor from moving out of window when using dual monitors

Project Settings -> Engine -> Input -> Viewport Properties -> change Default Viewport Mouse Lock Mode to Lock Always.
Then when mouse clicked in viewport, cursor would be locked in viewport.

游戏暂停时是否允许输入执行(默认是禁止输入)

How to disable input when the game is paused
FInputActionBinding& ToggleInGameMenuBinding = InputComponent->BindAction("InGameMenu", IE_Pressed, 
    this, &AStrategyPlayerController::OnToggleInGameMenu);
ToggleInGameMenuBinding.bExecuteWhenPaused = true;
Bind key mapping for keyboard at runtime

Add key mapping

void AMyPlayerController::ResetPlayerInput()
{
    FInputAxisKeyMapping Forward("MoveForward", EKeys::W, 1.f);
    FInputAxisKeyMapping Backward("MoveForward", EKeys::S, -1.f);
    FInputAxisKeyMapping Right("MoveRight", EKeys::A, -1.f);
    FInputAxisKeyMapping Left("MoveRight", EKeys::D, 1.f);
    PlayerInput->AddAxisMapping(Forward);
    PlayerInput->AddAxisMapping(Backward);
    PlayerInput->AddAxisMapping(Right);
    PlayerInput->AddAxisMapping(Left);

    FInputActionKeyMapping Fire("Fire", EKeys::LeftMouseButton);
    PlayerInput->AddActionMapping(Fire);
}

Remove key mapping

FInputAxisKeyMapping KeyMapping_Fwd_W("MoveForward", EKeys::W);
FInputAxisKeyMapping KeyMapping_Fwd_S("MoveForward", EKeys::S);

Controller->PlayerInput->RemoveAxisMapping(KeyMapping_Fwd_W);
Controller->PlayerInput->RemoveAxisMapping(KeyMapping_Fwd_S);
Check if key pressed

MyPlayerController.h

/** Handles a key press */
virtual bool InputKey(FKey Key, EInputEvent EventType, float AmountDepressed, bool bGamepad) override;

/** Handles a touch screen action */
virtual bool InputTouch(uint32 Handle, ETouchType::Type Type, const FVector2D& TouchLocation, float Force, FDateTime DeviceTimestamp, uint32 TouchpadIndex) override;

MyPlayerController.cpp

bool AMyPlayerController::InputKey(FKey Key, EInputEvent EventType, float AmountDepressed, bool bGamepad)
{
    if(Key == EKeys::W)
    {
        //do the magic
    }
}

Blueprint Event: AnyKey

Check if key pressed (or mouse button click) on widget

If widget has input event (e.g. UEditableTextBox), then APlayerController::InputKey() will not be triggered even widget has been focused and keyboard pressed.

Solution:
Implemente virtual function of UserWidget.
UserWidget.h

virtual FReply NativeOnKeyDown( const FGeometry& InGeometry, const FKeyEvent& InKeyEvent );
virtual FReply NativeOnMouseButtonDown( const FGeometry& InGeometry, const FPointerEvent& InMouseEvent );

NativeOnKeyDown didn’t fire if execute SetInputMode_UIOnly().

Issues

AddYawInput doesn’t work in Tick of PlayerController

Reason:
It’s strange, maybe a issue of engine?

Solution:
Execute AddControllerYawInput() in ACharacter::Tick() (inherited by AMyCharacter)

Issue: Set Input UI Only - Removes Keyboard Input

Don’t use the UI only node. To disable game input and focus solely on the widget, do the following:
Turn this setting on in your widget:

Put this node in your widget blueprint.

In my case, I have AbilityMenu assigned in my input settings. So when the key assigned to that action name is pressed, it will fire CloseWidget and close this menu.

Origin:
https://forums.unrealengine.com/t/set-input-ui-only-removes-keyboard-input/400777/5?u=dawnarc

UE5 Enhanced Input

Documents

Enhanced Input
https://dev.epicgames.com/documentation/en-us/unreal-engine/enhanced-input-in-unreal-engine


三十功名尘与土,八千里路云和月。----岳飞《满江红》