How to center mouse cursor

I want to center mouse cursor. Here how I do it.

But here is a problem - when I change active window with alt + tab, cursor still in the center all the time. Can I check somehow that game window is focusable?

SetMousePosition indeed controls the OS cursor, sometimes a bit aggressively.
You can check if UE is running in the foreground and if so SetMousePosition:

UGameViewportClient* GameViewportClient = GEngine->GetWorldFromContextObjectChecked(LocalPlayerContext.GetPlayerController())->GetGameViewport();
if (!GameViewportClient->Viewport || !GameViewportClient->Viewport->IsForegroundWindow()) {
	return;
}
else {
    // Set mouse position
///
}

Not sure if you can do that with blueprint.

Thanks, but I am looking for a blueprint solution. It is strane that so basic feature is now working by default

You could test if the axis value of the mouse movement is > or < than 0 when the window is out of focus, I guess it should not be. Then you could only center the cursor using SetMousePosition if that axis value is < 0 or > 0.

But if I will switch game window with alt + tab, then it will affect other programs.
I tries to change some project settings, but it still don’t work as expected

Are the axis values for mouse X and Mouse Y equal to zero while alt tabbing to another program?

afbeelding

Yes. It change value from -3 to 3 when I move cursor but then became 0

image

this free plugin has a few nodes for setting mouse position in blueprints.

The idea is to measure the axis values like this, then act on it:

Strange, it never goes to the false branch. Maybe problem is somewhere in another place because now it says that X, Y is always 0, 0, even when I press button and nohing is happens.

Maybe problem is somewhere in Break Hit Result function

X, Y should not be exact 0 while you move the mouse, but note that this value is measured per frame so if you don’t actively move the mouse around you get 0 readings. It should read 0 when the window is out of focus, which is how you should determine if you want to SetMousePosition or not if there is no BP alternative of measuring so.

You were right, it is still cursor out of screen problem. I can see in windowed mode that it is somewhere in that red area. And at that moment InputAction event is not working. Now I know where exactly I have this problem, will try to fix it with your advices