I found a solution for the left click event that triggers UI ‘on clicked’ events.
I’ve looked into this because I want a handtracker to move the mouse and also click things.
You’ll have to go into c++ to make it work, but its not too complicated and if you struggle with it, let me know.
So add a new c++ class which inherits from player controller (this is nog mandatory but its nice to have it in a player controller) Then in your .h file add 2 functions like so:
public:
UFUNCTION(BlueprintCallable)
void TriggerMouseLMBDown();
UFUNCTION(BlueprintCallable)
void TriggerMouseLMBUp();
The blueprint callable makes it so that you can trigger the function from blueprints.
The first function simulates the click down and the second function the release. You need both.
Then in the .cpp file add the code below.
The first part triggers any LMB click events and the part below that handles the UI clicks.
void AHandTrack_PlayerController::TriggerMouseLMBDown()
{
//trigger the mouse click event. This will fire any lmb click events within blueprints.
FViewportClient* Client = GEngine->GameViewport->Viewport->GetClient();
FKey MouseLMB = EKeys::LeftMouseButton;
Client->InputKey(GEngine->GameViewport->Viewport, 0, MouseLMB, EInputEvent::IE_Pressed);
//Trigger mouse clicks in UI
//Get our slate application
FSlateApplication& SlateApp = FSlateApplication::Get();
//create a pointer event
FPointerEvent MouseDownEvent(
0,
SlateApp.CursorPointerIndex,
SlateApp.GetCursorPos(),
SlateApp.GetLastCursorPos(),
SlateApp.GetPressedMouseButtons(),
EKeys::LeftMouseButton,
0,
SlateApp.GetPlatformApplication()->GetModifierKeys()
);
//send the mouse event to the slate handler
TSharedPtr<FGenericWindow> GenWindow;
SlateApp.ProcessMouseButtonDownEvent(GenWindow, MouseDownEvent);
}
void AHandTrack_PlayerController::TriggerMouseLMBUp()
{
//trigger the mouse click release event
FViewportClient* Client = GEngine->GameViewport->Viewport->GetClient();
FKey MouseLMB = EKeys::LeftMouseButton;
Client->InputKey(GEngine->GameViewport->Viewport, 0, MouseLMB, EInputEvent::IE_Released);
//trigger the UI mouse click
FSlateApplication& SlateApp = FSlateApplication::Get();
FPointerEvent MouseUpEvent(
0,
SlateApp.CursorPointerIndex,
SlateApp.GetCursorPos(),
SlateApp.GetLastCursorPos(),
SlateApp.GetPressedMouseButtons(),
EKeys::LeftMouseButton,
0,
SlateApp.GetPlatformApplication()->GetModifierKeys()
);
TSharedPtr<FGenericWindow> GenWindow;
SlateApp.ProcessMouseButtonUpEvent(MouseUpEvent);
}
For my hand tracking solution in the blueprint I trigger the mouse click down on a hand pinch.
Don’t forget to also trigger the mouse click up events with a small delay or on a button release.
In your instance you can detect a specific button press and then trigger the mouse click down function and trigger the mouse click up on button release.