The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
[UWP] [C#] [SharpDX] [GameDev] How do i lock / position the mouse cursor? RRS feed

  • Question

  • How can i either lock the mouse cursor to my application window or programmatically set the mouse cursor position in order to lock it to the application ?

    This is for a game-scenario which uses a first-person mouse controller. If the mouse cursor leaves the window then i stop receiving pointer move events and presumably any pointer click events will focus away from the application which is not a desired result obviously. 

    I need a solution for both full-screen and windowed application, as I want to support both configurations.

    Without fully investigating, I'm guessing that the Win32 equivalents are :

    ClipCursor and SetCursorPos
    • Edited by Gavin..Williams Sunday, September 6, 2015 3:04 AM forgot title
    • Moved by Kristin Xie Monday, September 7, 2015 5:48 AM UWP related
    Sunday, September 6, 2015 3:04 AM

All replies

  • Wich GUI technology are you using?

    WinForms? WPF? XNA? Other?

    Sunday, September 6, 2015 3:26 AM
  • Xaml for the mouse input, I consume events from Windows.UI.Core.CoreIndependentInputSource.

    Edit : Actually i should keep this info in : The CoreIndependentInputSource is attached to a SwapChainPanel and I'm collecting mouse input through that background thread. Which means some of the normal ways of handling the cursor will not work.

    Sunday, September 6, 2015 8:03 AM
  • Hi Gavin,

    Your question is more related to Windows apps. We have moved your cases to Developing Universal Windows apps forum, you will get more help there.

    Best Regards,
    Li Wang

    Monday, September 7, 2015 5:47 AM
  • From what I can gather, xaml SwapChainPanel is not actually usable for any application that uses a mouse-look camera. And even if it is possible, it must be an obscure or difficult technique, because I cannot work out how to do it. Perhaps there are peculiarities in my project that complicate things (eg IndependentInput thread), who knows. It's unfortunate because I've been writing my engine built around the xaml SwapChainPanel, supporting multiple SCP's and working alongside xaml. And having xaml always on call was really nice.

    I will have to rebuild my engine around the corewindow / exclusive fullscreen approach. I lose xaml, which is actually really disappointing, but I'll try to look at it as an opportunity to:

    1) Take a look at DX12

    2) Rebuild the engine to operate in a much simpler fashion, 1 window and 1 swapchain (which is actually 99% of scenarios anyway).

    Tuesday, September 8, 2015 1:54 AM
  • From what I can gather, xaml SwapChainPanel is not actually usable for any application that uses a mouse-look camera. And even if it is possible, it must be an obscure or difficult technique, because I cannot work out how to do it. Perhaps there are peculiarities in my project that complicate things (eg IndependentInput thread), who knows. It's unfortunate because I've been writing my engine built around the xaml SwapChainPanel, supporting multiple SCP's and working alongside xaml. And having xaml always on call was really nice.

    I will have to rebuild my engine around the corewindow / exclusive fullscreen approach. I lose xaml, which is actually really disappointing, but I'll try to look at it as an opportunity to:

    1) Take a look at DX12

    2) Rebuild the engine to operate in a much simpler fashion, 1 window and 1 swapchain (which is actually 99% of scenarios anyway).

    So... this is your next plan?

    Best Regards,
    Please remember to mark the replies as answers if they help

    • Edited by IssueKiller Thursday, September 10, 2015 12:08 PM
    Thursday, September 10, 2015 12:08 PM
  • Yeah, it looks like the xaml SwapChainPanel is suited to non-exclusive style applications. For instance, it's used for paint packages. It would also be ok for some games, where a mouse pointer is used. I've been using it fine up till now (over several months) because I'd not tried putting a first-person camera in. But i wanted to build a small scene to run around in to work on some 3d stuff - and thus ran into the problem with not being able to make the app exclusive. I'm not totally sure that it's impossible, but I don't believe i have the capability to work it out. 

    Anyway, there is an easier approach I think, and that is to use the more typical approach and get rid of the xaml completely and come at the application setup from a different angle, I've translated this Win8 C++  CoreWindow sample to UWP C#, and setting up DirectX 12. So far so good, I have a DX12 swapchain and basic render pipeline setup.

    My only concern is that I can't switch this CoreWindow app and swapchain to exclusive fullscreen either. My reasoning is that I need to use SwapChainDescription1 and it doesn't have the IsWindowed flag like SwapChainDescription does. I don't like that the flag is missing, but I've only just started looking at using CoreWindow, so I won't dismiss it just yet. I hope there is a new WinRT way to enter exclusive fullscreen mode.
    Thursday, September 10, 2015 1:46 PM
  • Hi Gavin.  I am curious -- did you ever find a way to center the mouse for a 3d look camera?  I am hitting the same issue -- I am curious to hear how your final approach turned out.  Regards, Shaun.
    Tuesday, January 26, 2016 3:51 PM
  • Hi from Greece. I'd like to know a solution for this too.

    First person shooter game.

    The camera rotation stops when the mouse pointer reaches the edge of the screen!


    UPDATE:   I THINK I"VE FOUND SOMETHING!!!!
    IT IS NOW POSSIBLE TO SET THE MOUSE POINTER POSITION PROGRAMMATICALLY!!!

     Window.Current.CoreWindow.PointerPosition = new Windows.Foundation.Point(400, 400);


    Saturday, December 10, 2016 6:27 PM