Simulate left click with Abutton of gamepad

Is there a way to simulate the left click of the mouse with a game pad?

Iā€™d like to know this as well. Havenā€™t been able to find a solution.

What are you trying to achieve exactly? You can have an action mapping with multiple inputs i.e.

40500-actionmapping.png

Not exactly. Iā€™m trying to use the gamepad to activate ā€œon clickedā€ events.

Gotcha. This isnā€™t currently possible. Are you controlling the mouse cursor with your game pad(with Ramaā€™s plugin maybe; ~ Set Mouse Position - SET the mouse position to any values of your choosing!)? if so, you could do this: Send a line trace from the mouse position towards the Y axis (assuming Y axis is your depth) and trigger a hit if it finds anything.

Sorry that this isnā€™t really what you were looking for, but itā€™s the best I can do :stuck_out_tongue:
Cheers!

Thanks for the reply. I wish there was a way to do click events with the gamepad but this should work.

yep exactly :slight_smile: at least there usually always is a work around (Iā€™ve always found a work around for what Iā€™ve tried doing). but this topic seems to have been tossed around a bit so maybe we will see this feature added in the future.

Hello, sorry to drag this up. But did this work for you?

I am trying to setup multiple cursors for my game, so I have each player moving around a widget that I can get a screen position from, is it even possible to get raycast hits on widgets casting from screen to world?

Any advice you have would be great. Thanks!

This sounds like another similar thread, which I ran into while trying to find workarounds myself. I posted what I ended up doing here: Gamepad Menu Navigation Issues - Programming & Scripting - Unreal Engine Forums

(Not sure on etiquette of copy/pasting the same post, so Iā€™ll just link it.)

TLDR is - if you put the input button you want to use in the player controller or pawn, and have it set a variable to true when pressed, and then re-setting that variable to false when released, you can cast to the player with your widget each frame, and run that through a branch tied to that variable.

Then, you can have that go through a sequence, each one going to a new branch, which is tied to ā€œIs Hoveredā€ which is then tied to each button you want to use.

These branches then only output on ā€œtrueā€ ā€œIs Hoveredā€ responses from the buttons, and feed into whatever you want that button to do.


Hopefully this is kinda what youā€™re looking for? The other thread was a bit more specific about how they were hoping to use this spoofing of mouse clicks, Iā€™m assuming itā€™s for a menu widget or something similar.

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.

2 Likes

nice! good job ~



Itā€™s not the greatest solution, and is quite tedious depending on how much you have in your HUD, but this works for me at least. I found I needed to overlay my non-btn UMG elements that had click events with a transparent btn, that way you can use the OnHovered and Unhovered events to set a boolean. Then you can simply collapse all your click logic into a function, and call that from your pawn/characterā€™s BP.

Itā€™s not the best solution, but itā€™s simple to understand at least.