Has anyone got LockMouseToViewport() to work ?

I’ve been struggling to keep my mouse cursor inside my game, and with a dual monitor my mouse just goes as far as my desktop can reach. I guess for a single screen desktop running a fullscreen application, this is not a critical issue, but it’s still not “locked” in the window. So I tried this method


inside my MyHUD::PostInitializeComponents(), inside my characters components, wherever.

So, is this the correct way to use this function (I got this from an AnswerHub answer from ShadowRiver).
Is there a specific location where I should use it ?

I don’t know if it should be working for 4.2, but it’s certainly not working for 4.1.

That is because it has no code in it - this is the declaration:

virtual void LockMouseToViewport( bool bLock )	{ }

I knew that. :slight_smile: But that’s the default viewport definition. I use a Third-Person template and the viewport is derived from FSceneViewport - at least that’s where I landed when I traced my code into the Engine - and that has code in it.

I got it to work on a very specific case but it’s not easy to reproduce and not very coherent. Something about the first click touching a target I defined with an OnClick() in Blueprint. Then the view port locks the mouse in the Editor’s viewport.

But in retrospect, that’s not what I’m looking for. FViewport/FSceneViewport seem to be reserved for the Editor viewport only. I’m looking for a way to limit my mouse into windowed games.

Thanks for the answer. But if you have anything else on my problem feel free to ring me. :slight_smile:


Well I got it to work into a windowed game after all. I think the basic problem is how my game handles mouse “capture” into the game. You know, when the Editor says “Click for Mouse Control”, then “Press Shift-F1 for Mouse Cursor”, well in my case it toggles back to it immediately to the first case. And it’s only when the game really keeps that mouse in control that it locks the viewport. The culprit is probably in my code :frowning: I’ll need to investigate that.


So how have you achieved to lock the mouse? I’m interesed in doing that to so I’ll be happy for a code sample. :smiley:

Thanks in advance.

I’m working the issue. So far, I believe that LockMouseToViewport() has nothing to do with it. I removed it and I have the same erratic behaviour. So there goes one theory. And any code I wrote seems to break things, not fix them. :slight_smile:

Keep watching this thread.

I have no idea why my first experiment with it failed and that is why I thought LockMouseToViewport() would help in some way: it doesn’t. But if this helps anybody else, my post will not have been made in vain. :slight_smile:

This is a simplified version of Rama’s HUD tutorial to a bare minimum where the only thing happening here is a toggle between character control and mouse cursor display. I used it on a Third-Person template. I haven’t tried it on other templates. Beyond that, YMMV.

Original tutorial:,_Canvas,_Code_Sample_of_800%2B_Lines,Create_Buttons%26_Draw_Materials

The mouse should be locked into the window when “F” is pressed. Just like the tutorial says, don’t forget to create your Blueprint HUD and include HUD creation code into your Game Mode constructor.

/ht Rama. I love your code, but sometimes we need to break it down a little just to keep things under control. :slight_smile:


#pragma once

#include "GameFramework/HUD.h"
#include "MyHUD.generated.h"

class AMyHUD : public AHUD


	void PostInitializeComponents();
	void PlayInputChecks();
	virtual void DrawHUD() OVERRIDE;


	APlayerController* ThePC;


#include "MuGame.h"
#include "MyHUD.h"

AMyHUD::AMyHUD(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)


void AMyHUD::PostInitializeComponents()
	//Establish the PC
	ThePC = GetOwningPlayerController();

void AMyHUD::DrawHUD()
	//Have PC for Input Checks and Mouse Cursor?
		//Attempt to Reacquire PC
		ThePC = GetOwningPlayerController();
		//Could Not Obtain PC
		if(!ThePC) return;
	//Multiplayer Safety Check
	if(!ThePC->PlayerInput) return; //not valid for first seconds of a multiplayer client


void AMyHUD::PlayInputChecks()
		bool  lookinput = !ThePC->IsLookInputIgnored();

		ThePC->bShowMouseCursor = lookinput;