How to detect whether keyboard/mouse or controller was interacted with last

If you’re using C++ you can check the most recently used hardware device with the following function:

#include "GameFramework/InputDeviceSubsystem.h"

....

bool YourClass::IsPlayerUsingGamePad()
{
    APlayerController* PlayerController = UGameplayStatics::GetPlayerController(this, 0);
    if (!PlayerController) return false;
    
    if (UInputDeviceSubsystem* InputDeviceSubsystem = GEngine->GetEngineSubsystem<UInputDeviceSubsystem>())
    {
        const FPlatformUserId UserId = PlayerController->GetPlatformUserId();
        FHardwareDeviceIdentifier DeviceIdentifier = InputDeviceSubsystem->GetMostRecentlyUsedHardwareDevice(UserId);
        return (DeviceIdentifier.PrimaryDeviceType == EHardwareDevicePrimaryType::Gamepad);
    }
    return false;
}
1 Like