Search code examples
c#uwpgesture

UWP C# mouse control with gestures


I'm working on implementation of controlling mouse cursor with gestures recognihened with depth camera. I wanna move cursor and perform left mouse click. How it can be implemented in UWP C#?

I have tryed this for mouse move and it works OK, but events of the other objects are not triggered (controlling with mouse everything is good):

Window.Current.CoreWindow.PointerPosition = new Point(pointerPosition.X - deltaX, pointerPosition.Y + deltaY);

For mouse click I've tried this:

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
    //Mouse actions
    private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const uint MOUSEEVENTF_LEFTUP = 0x0004;

    public async System.Threading.Tasks.Task DoMouseClickAsync()
    {
        //Call the imported function with the cursor's current position

        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            var x = (uint)Window.Current.CoreWindow.PointerPosition.X;
            var y = (uint)Window.Current.CoreWindow.PointerPosition.Y;

            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
        });
    }

But no reaction. Could you give me a piece of advice?)


Solution

  • As mention in this answer, mouse_event cannot be used within a UWP app (for obvious security reason).