x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

FViewport::LockMouseToViewport isn't working?

Hi!

When I call FViewport::LockMouseToViewport nothings seams to happen. Is there a misunderstanding of how this function should be used or is it just not working?

I get the FViewport from casting the Player member in the PlayerController to a ULocalPlayer. Then I call localPlayer->ViewportClient->Viewport->LockMouseToViewport(true). I have tried to call it in all different places but can't get any result.

API pages link1, link2.

Product Version: Not Selected
Tags:
more ▼

asked Jun 15 '14 at 04:13 PM in Bug Reports

avatar image

undercover
838 123 111 138

avatar image DrHobo Jun 23 '14 at 03:25 PM

Hi. I just wanted to know what you wanted to achieve with it ? Make a mouse cursor appear during, say, a TPP game and limit the mouse cursor into it ?

avatar image undercover Jun 23 '14 at 08:56 PM

Hi!

Yes, I want to lock the mouse to the viewport. So in other words limit the mouse cursor's movement to the bounds of the viewport.

avatar image Tim C ♦♦ STAFF Jul 08 '14 at 10:18 PM

Hi undercover,

Are you still experiencing this issue?

avatar image DrHobo Jul 08 '14 at 10:38 PM

I am. What I suggested in this thread isn't a complete solution, and when I toggle from / to my program, now I'm really losing the boundaries for my mouse and it goes everywhere. I'd love to know how this function works.

avatar image Tim C ♦♦ STAFF Jul 09 '14 at 01:48 PM

Just to make sure I completely understand what you are looking for, you are trying to find a way to keep the mouse cursor from moving outside of the game window?

avatar image DrHobo Jul 09 '14 at 02:09 PM

Yes, that's it.

avatar image undercover Jul 10 '14 at 01:11 PM

Yes, in a way such that it will work after some engine updates. I have seen some hacks being used but I can't really use that for final product code.

avatar image Tim C ♦♦ STAFF Jul 10 '14 at 04:22 PM

I am still doing some research regarding uses for LockMouseToViewport. One thing that was pointed out to me was that the Strategy Game example project keeps the mouse locked within the game window. However, it seems to accomplish this without using LockMouseToViewport. I believe this is where that logic is being implemented, but I am not 100% sure.

 // SStrategySlateHUDWidget.cpp
 FCursorReply SStrategySlateHUDWidget::OnCursorQuery( const FGeometry& MyGeometry, const FPointerEvent& CursorEvent ) const
 {
     //make sure if our application is active, we do have mouse captured in window bounds
     if (FSlateApplication::Get().GetActiveTopLevelWindow().IsValid() && FSlateApplication::Get().GetMouseCaptor() != SharedThis(this))
     {
         FSlateApplication::Get().SetFocusToGameViewport();
     }
     return FCursorReply::Cursor(EMouseCursor::Default);
 }
avatar image DrHobo Jul 11 '14 at 06:49 AM

I'm not familiar with Slate, I'm using AHUD and Canvas for my 2D drawings. So can I implement a HUD in both Slate and AHUD / Canvas together ? I've tried Strategy Game and the mouse is limited to the window, before and after toggling back into the program. The program also mutes the sound when I toggle to another app (dunno if that's UE4 related or Windows related) but it doesn't pause the game - unless I pause the game explicitly with the menu button.

In a related issue, I also notice that AHUD::OnLostFocusPause() never pauses a game. And even overridden in my own child class, it never goes through it when I loose my game's focus. That's because it is called exclusively by UEngine::OnLostFocusPause(), line 5123, but the latter is never called by anything else. Unless I must call that function myself (doubt it) , I would need another method to receive the lost focus signal.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

4 answers: sort voted first

In 4.6 and forward there are functions that help you setup input modes. One thing they allow is a way to control whether or not the mouse is locked, they also do quite a bit more.

In blueprints use the SetInputMode_UIOnly, SetInputMode_GameAndUI, SetInputMode_GameOnly nodes to setup your desired behavior.

alt text

Game Only always locks.

In C++ you can call the:

 APlayerController::SetInputMode()

function takes FInputModeUIOnly, FInputModeGameAndUI, FInputModeGameOnly, or your own custom class.

setinputmode.png (46.7 kB)
more ▼

answered Jan 17 '15 at 12:26 AM

avatar image

Chris Gagnon STAFF
86 5 3 2

avatar image undercover Jan 20 '15 at 08:04 PM

I have actually tested those but wasn't completely satisfied with how they worked. E.g. when I used FInputModeGameAndUI, if the window lost focus the mouse wasn't recaptured when selecting the window again. I want the mouse to be visible all the time but never allow it to leave the window as long as the window is in the foreground.

I guess I could use the code to implementing my own input states. The problem with that would be when the engine gets updated there is a risk my code would break over time. Unreal engine has had a tendency of breaking old code when updated.

avatar image Doug E ♦♦ STAFF Jan 22 '15 at 04:28 PM

Hey undercover-

Looking at the Tower Defense demo in the Learn tab of the launcher (which I believe has the functionality you're looking for), it appears that you'd have to make a call to SetUserFocus() to check when the game window regains focus.

The idea of using code is also not a bad idea and in fact if you are able to setup a system that works for you, you could then submit a pull request to have your code added to the engine for future use to make the process simpler.

avatar image undercover Jan 22 '15 at 08:33 PM

I can't find any call to SetUserFocus? Can you please specify exactly on which line that call is made? I have search through the entire solution without finding it :/

avatar image undercover Jan 22 '15 at 09:24 PM

The call that makes the trick is FSlateApplication::Get().SetKeyboardFocus(SharedThis(this));. The implementation of that method calls SetUserFocus but the example itself does never call SetUserFocus directly.

Anyhow I don't like the busy wait implementation since it must be checked every tick if we need to call it again. Would be much cleaner to rely on events when gaining and/or losing application focus. However that doesn't seems to work either. Have another post up regarding that issue.

avatar image undercover Jan 22 '15 at 09:28 PM

Okay, APlayerController::SetInputMode is the way to go. Just need those application focus events to work properly now.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Here's a little tutorial I wrote based on Rama's code when I was asking the same question to myself.

Cheers.

https://forums.unrealengine.com/showthread.php?10789-Has-anyone-got-LockMouseToViewport%28%29-to-work

more ▼

answered Jun 23 '14 at 10:38 PM

avatar image

DrHobo
271 32 35 53

avatar image undercover Jun 26 '14 at 08:45 AM

The code seems to toggle the mouse visibility and whether look input should be ignored or not. I want to lock the mouse cursor to the window. Is it some kind of a hack? I think LockMouseToViewport would be a nicer way of achieving this behavior and probably more reliable when the engine is being updated.

avatar image DrHobo Jun 26 '14 at 02:10 PM

I agree. Rama's code is useful but seems limited in that matter and yeah, probably a hack.

(Edited gibberish from my part).

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Hello,

Thank you for your report. We were not able to investigate this on the engine version you reported, but there have been many version changes to UE4 since this question was first posted. With a new version of the Engine comes new fixes and it is possible that this issue has changed or may no longer occur. Due to timetable of when this issue was first posted, we are marking this post as resolved for tracking purposes.

If you are still experiencing the issue you reported in the current engine version, then please respond to this message with additional information and we will investigate as soon as possible. If you are experiencing a similar, but different issue at this time, please submit a new report for it.

Thank you

more ▼

answered Oct 03 '14 at 02:38 PM

avatar image undercover Oct 07 '14 at 10:05 AM

It works to some extent. However there are still some problems. It doesn't work at all in a standalone window. In "normal" window it works but the mouse isn't locked to the viewport but the the window. Has it been tested at all?

I guess I will have to use slate in order to lock the mouse to the viewport, a bit awkward but what should you do.

avatar image undercover Jan 11 '15 at 07:27 PM

This still doesn't work in 4.6.1. Calling this in a "standalone window" or build without editor, have no effect.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Hey undercover-

Looking at the declaration of the virtual void function "LockMouseToViewport(bool bLock)" in the UnrealClient.h file, there is no code to implement the function. As a virtual function it would be possible to override the function elsewhere with your own code functionality.

Cheers

Doug Wilson

more ▼

answered Jan 15 '15 at 09:34 PM

avatar image undercover Jan 16 '15 at 02:20 PM

There is indeed code implementing the virtual method if called as I wrote above.

 void FSceneViewport::LockMouseToViewport( bool bLock )
 {
     if( bLock )
     {
         CurrentReplyState.LockMouseToWidget( ViewportWidget.Pin().ToSharedRef() );
     }
     else
     {
         CurrentReplyState.ReleaseMouseLock();
     }
 }

However this doesn't seem to work very good. Especially not when working with standalone window or running without the editor. Anyhow if there is any other way to simply lock the mouse to the viewport without having to lock it to a specific slate widget that would work too.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question