0
votes

Community

for my current project (C#, WPF, VS 16.5.3, W10 1909), I need to recognize Mouse-Movement, even if the cursor is at a screen-border (i.e. top-right) and the user continues moving the mouse. When I ask for the position by

Mouse.GetPosition(Application.Current.MainWindow)

I get the position within the screen and the values stop changing if I continue moving the physical mouse while the cursor is at a screen-border. What I would like was an "virtual infinite screen" from which I would get the coordinates of the mouse-cursor (other ideas are welcome as well). How do I achieve that?

Thx

1

1 Answers

1
votes
    using System;
    using System.Runtime.InteropServices;

    namespace StackOverFlow
    {
        public class Program
        {
            [DllImport("user32.dll")]
            static extern bool GetCursorPos(out POINT lpPoint);

            public struct POINT
            {
                public int X;
                public int Y;
            }

            static int _x, _y;

            static void ShowMousePosition()
            {
                POINT point;
                if (GetCursorPos(out point) && point.X != _x && point.Y != _y)
                {
                    Console.Clear();
                    Console.WriteLine("({0},{1})", point.X, point.Y);
                    _x = point.X;
                    _y = point.Y;
                }
            }

            static void Main()
            {
                while (true)
                {
                    ShowMousePosition();
                }
            }
        }
    }