Hi, guys.
I’ve got a problem about hide mouse cursor. In my game, I use GameViewportClient->SetHideCursorDuringCapture() to enbale hide cursor when mouse button is pressed, but this function will hide cursor no matter which mouse button is pressed. I don’t want to hide mouse cursor when left mouse button pressed, and hide mouse cursor when right mouse button pressed. How can I make this happen. Please Help.
You need to write a custom function to enable this.
First you need to select a cursor image for your game and replace it with the default cursor.
Then you need to add the states in the cursor image (UMG class) as when you right click and when you left click.
With this scenario you can create a functionality to hide your mouse cursor
Thanks, bro. I have already changed cursor image, and it is be done in c++ code, is there any method to hide cursor in code?
If you have changed the cursor image, how hard it would be to add alpha to the image when you make a right click event?
I have solved this problem by modify engine code. In SceneViewport, method AcquireFocusAndCapture, before bCursorHiddenDueToCapture set true. I did some checks and make it only be true when rightmousebuttondown. And it works well now.
You can do this without modifying the engine
Override bool APlayerController::InputKey(FKey Key, EInputEvent EventType, float AmountDepressed, bool bGamepad)
there, you set a custom player controller variable to bool true if is presed or double clicked (using eventtype) and set it to false when right mouse is released.
i.e., bool rightmousedown = true or false.
after that, create a c++ object with UGameViewportClient as ancestor
in project settings, set the default game viewpoert class to this class you created
then you override virtual bool HideCursorDuringCapture() override;
cpp file:
bool UMyGViewportClient::HideCursorDuringCapture()
{
(get a pointer here to player controller, several ways to do so)
APlayerController *PC;
if (PC->rightmousedown)
return true
else
return Super::HideCursorDuringCapture();// or simply return false.
}
note: you can´t use the APlayerController::iskeydown… inside this function, it dows not work. perhaps it has not been set yet
that´s why you need to set a bool in the player controller class.
I am in a bit of a rush here I´m sorry for the quick explanation
Paulo
I’m doing this in blueprint:
I bind right mouse button to an input action “MouseLook” and the left mouse button to an input action “MouseRun.”
Then in the player controller, I update the mouse capture based on the input event:
Then in the input controller, I override the controller/axis input with a +1 axis value if the “Is Mouse Running” bool is set, meaning the right and left mouse buttons are down.
You can do this same thing in C++ if you don’t want to use blueprints.