# How to inject/simulate mouse clicks?

**URL:** <https://forums.unrealengine.com/t/how-to-inject-simulate-mouse-clicks/25602>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [March 27, 2015, 11:36am UTC](https://forums.unrealengine.com/t/how-to-inject-simulate-mouse-clicks/25602 "2015-03-27T11:36:16Z")\
**Posts on this page:** 1\
**Showing post:** 15

<div class="post-metadata">

**Author:** ![Jotaid](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jotaid](https://forums.unrealengine.com/u/Jotaid)\
**Post date:** [August 10, 2024, 2:38pm UTC](https://forums.unrealengine.com/t/how-to-inject-simulate-mouse-clicks/25602/15 "2024-08-10T14:38:07Z")

</div>

Thanks! this is my first cpp so I used Chatgpt and here is the complete code, I just added some input and output extras:

.cpp

> #include “MyFunctionLibrary.h”  
> #include “Framework/Application/SlateApplication.h”
> 
> void UMyFunctionLibrary::SimulateLeftMouseButtonClick(  
> bool bButtonDown,  
> FVector2D& LastCursorPos,  
> bool& bIsValid,  
> bool bUseCustomCursorPos,  
> FVector2D CustomCursorPos  
> )  
> {  
> // Get the Slate application instance  
> FSlateApplication& SlateApp = FSlateApplication::Get();
> 
> // Determine whether to use the custom cursor position or the actual cursor position  
> if (bUseCustomCursorPos)  
> {  
> LastCursorPos = CustomCursorPos;  
> }  
> else  
> {  
> LastCursorPos = SlateApp.GetLastCursorPos();  
> }
> 
> // Check if the cursor position is valid (not zero)  
> bIsValid = !LastCursorPos.IsZero();
> 
> // Create a pointer event for the left mouse button  
> FPointerEvent MouseEvent(  
> 0,  
> SlateApp.CursorPointerIndex,  
> SlateApp.GetCursorPos(),  
> LastCursorPos,  
> SlateApp.GetPressedMouseButtons(),  
> EKeys::LeftMouseButton,  
> 0,  
> SlateApp.GetPlatformApplication()-\>GetModifierKeys()  
> );
> 
> // Simulate the mouse button down or up event  
> if (bButtonDown)  
> {  
> SlateApp.ProcessMouseButtonDownEvent(nullptr, MouseEvent);  
> }  
> else  
> {  
> SlateApp.ProcessMouseButtonUpEvent(MouseEvent);  
> }  
> }

.h

> #pragma once
> 
> #include “Kismet/BlueprintFunctionLibrary.h”  
> #include “MyFunctionLibrary.generated.h”
> 
> UCLASS()  
> class TOPDOWN\_API UMyFunctionLibrary : public UBlueprintFunctionLibrary  
> {  
> GENERATED\_BODY()
> 
> public:  
> UFUNCTION(BlueprintCallable, Category = “Utility”)  
> static void SimulateLeftMouseButtonClick(  
> bool bButtonDown,  
> FVector2D& LastCursorPos,  
> bool& bIsValid,  
> bool bUseCustomCursorPos,  
> FVector2D CustomCursorPos  
> );  
> };

---

_[View the full topic](https://forums.unrealengine.com/t/how-to-inject-simulate-mouse-clicks/25602)._
