Advertisement
Codball

Simulate any mouse button click Unreal Engine 5.2 c++

Feb 23rd, 2024 (edited)
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | Software | 0 0
  1. ./MyFunctionLibrary.cpp
  2.  
  3. #include "MyFunctionLibrary.h"
  4. #include "Framework/Application/SlateApplication.h"
  5.  
  6. void UMyFunctionLibrary::SimulateMouseButton(bool bLeft, bool bButtonDown, bool bWidgetsOnly, bool bWorldOnly)
  7. {
  8.     // Determine Mouse Button and Input Type
  9.     FKey MouseButton = bLeft ? EKeys::LeftMouseButton : EKeys::RightMouseButton;
  10.     EInputEvent InputEvent = bButtonDown ? EInputEvent::IE_Pressed : EInputEvent::IE_Released;
  11.  
  12.     // Get our slate application
  13.     FSlateApplication& SlateApp = FSlateApplication::Get();
  14.  
  15.     // Create a pointer event
  16.     FPointerEvent MouseEvent(
  17.         0,
  18.         SlateApp.CursorPointerIndex,
  19.         SlateApp.GetCursorPos(),
  20.         SlateApp.GetLastCursorPos(),
  21.         SlateApp.GetPressedMouseButtons(),
  22.         MouseButton,
  23.         0,
  24.         SlateApp.GetPlatformApplication()->GetModifierKeys()
  25.     );
  26.  
  27.     // Mouse Button Down
  28.     if (bButtonDown)
  29.     {
  30.         if (!bWorldOnly)
  31.         {
  32.             // Get the widget window under the mouse
  33.             TArray<TSharedRef<SWindow>> WindowsArray;
  34.             FWidgetPath WidgetPath = SlateApp.LocateWindowUnderMouse(SlateApp.GetCursorPos(), WindowsArray);
  35.  
  36.             for (int32 i = WidgetPath.Widgets.Num() - 1; i >= 0; --i)
  37.             {
  38.                 const FArrangedWidget& ArrangedWidget = WidgetPath.Widgets[i];
  39.                 const TSharedRef<SWidget>& Widget = ArrangedWidget.Widget;
  40.                 TSharedPtr<SWindow> Window = SlateApp.FindWidgetWindow(Widget);
  41.                 if (Window.IsValid())
  42.                 {
  43.                     // Found the window under the mouse
  44.                     SlateApp.ProcessMouseButtonDownEvent(Window->GetNativeWindow(), MouseEvent);
  45.                     return;
  46.                 }
  47.             }
  48.         }
  49.  
  50.         if (!bWidgetsOnly)
  51.         {
  52.             // World click
  53.             TSharedPtr<FGenericWindow, ESPMode::ThreadSafe> NullWindow;
  54.             SlateApp.ProcessMouseButtonDownEvent(NullWindow, MouseEvent);
  55.             return;
  56.         }
  57.         return;
  58.     }
  59.  
  60.     // Mouse Button up
  61.     SlateApp.ProcessMouseButtonUpEvent(MouseEvent);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement