Capturing Mouse Movement

Many of you have probably seen "mouse meters" that track how many pixels the mouse has moved. I'm trying to do that but I can't figure out how to execute code every time there's a mousemove (on any control, let alone the entire screen). This is what I've gotten so far but it doesn't work. Any ideas


namespace mmove
{
    public partial class Form1 : Form
    {
        int pxls = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            pxls++;
            textBox1.Text = pxls.ToString();
        }
       
    }
}

 



Answer this question

Capturing Mouse Movement

  • Amit Bhave

    I've rewritten my code but it still only works within the application window. Help


    namespace mmove
    {
        public partial class Form1 : Form
        {
            int pxls = 0;
            int lx = 0;
            int ly = 0;
            bool started = false;
            public Form1()
            {
                InitializeComponent();
            }

            protected override void OnMouseMove(MouseEventArgs e)
            {
                if (started == false)
                {
                    lx = e.X;
                    ly = e.Y;
                    started = true;
                }
                if (e.Y - ly == 0)
                    pxls = pxls + (int)Math.Abs(e.X - lx);
                else if (e.X - lx == 0)
                    pxls = pxls + (int)Math.Abs(e.Y - ly);
                else
                {
                    int slope = (int)Math.Abs((e.Y - ly) / (e.X - lx));
                    if (slope >= 1)
                        pxls = pxls + (int)Math.Abs(e.Y - ly);
                    else if (slope < 1)
                        pxls = pxls + (int)Math.Abs(e.X - lx);
                }
                lx = e.X;
                ly = e.Y;

            }

            private void Form1_Load(object sender, EventArgs e)
            {
                this.Capture = true;
            }

            private void timer1_Tick(object sender, EventArgs e)
            {
                textBox1.Text = pxls.ToString();
            }

        }
    }

     


  • gabriewo

  • shhameed

    If you want to capture mouse events when the mouse has moved away from your control, you'll need to set "capture" to on.

    Try calling

    this.Capture = true;

    on your form's OnLoad override.

  • dodo.net

    also it takes a triple-click on the X to close the program... does anyone know how I can fix this

  • Ken Wilson

    OK, but that seems to freeze my computer :p

    I rewrote it slightly and I got a working program but it only counts while the mouse is in the application window. I have this.capture set as i was told... here's my complete code


    namespace mmove
    {
        public partial class Form1 : Form
        {
            int pxls = 0;
            public Form1()
            {
                InitializeComponent();
            }

            protected override void OnMouseMove(MouseEventArgs e)
            {
                pxls++;
                textBox1.Text = pxls.ToString();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                this.Capture = true;
            }
          
        }
    }

     


  • M.D.luffy

    Hi,



    You could try implementing DirectX's Direct Input. It handles mouse moves all over the screen... And also you could capture keyboards, joystick and other input devices...






    cheers,


    Paul June A. Domag

  • Capturing Mouse Movement