Moving the mouse according to the given coordinates

后端 未结 1 1097
花落未央
花落未央 2021-01-15 18:02

what I want is, after recording the mouse movement and saving the coordinates/ indexes/ position, I will have to load the mouse coordinates and make the mouse move according

相关标签:
1条回答
  • 2021-01-15 18:27

    This is a quick and rather dirty solution:

    Let's start with a few varibles:

     List<Point> points = null;
     Timer tt = null;
     int index = 0;
    

    Now a button to start the recoding; it initializes a List<Point> to collect the positions and creates and starts a Timer along with lambda code to do the recoding in the Timer.Tick event:

    private void btn_Record_Click(object sender, EventArgs e)
    {
        points = new List<Point>();
        index = 0;
        tt = new Timer()
        { Interval = 50, Enabled = true };
        tt.Tick += (ss, ee) => 
        {
            if (!points.Any() || points.Last() != Control.MousePosition) 
               points.Add(Control.MousePosition); 
        };
    }
    

    Next a button to stop recording:

    private void btn_Stop_Click(object sender, EventArgs e)
    {
        if (tt!=null) tt.Stop();
    }
    

    Finally the replay button; it uses an index to loop over the the points collection in a new Timer.Tick code but using the same timer:

    private void btn_Replay_Click(object sender, EventArgs e)
    {
        index = 0;
        tt = new Timer() { Interval = 50, Enabled = true };
        tt.Tick += (ss, ee) =>
        {
            if (index < points.Count)
              { System.Windows.Forms.Cursor.Position =  points[index++]; }
            else tt.Stop();
    }
    

    A few notes:

    • As asked in the question this will record and reply the Mmouse coodinates. It will do so in fixed intervals, so playback will look very similar to the original movements; in fact it is so hard to tell apart that I added a slowmo button with a longer interval to demonstrate.. (But the gif got too large)

    • The code will record the mouse positions in screen coordinates and should capture them wherever it goes, not only inside your application. See how VS is activating the code loupe!

    • It will not record any other mouse events, like up, down, click, doubleclick or wheel. For those you would need a global mouse hook for capturing and some external calls for replaying.

    • For including other mouse events you would also need a different and extended data structure; and you would also have to go from a timer-driven model to a mouse event driven one.

    • The example uses buttons to start and stop. Of course the movenments to and from those buttons get included in the recorded list of positions. Instead one could use a timed start, which would start recording after a few seconds and stop recording after a few seconds of inactivity..

    There are various ways you can save and load the points; the simplest one is to serialize to xml; using a string path = @"..." it could look as simple as this:

    private void btn_save_Click(object sender, EventArgs e)
    {
        if (points == null) points = new List<Point>();
        XmlSerializer xs = new XmlSerializer((points).GetType());
        using (TextReader tr = new StreamReader(path))
        {
            points =  (List<Point>)xs.Deserialize(tr);
            tr.Close();
        }
    }
    
    private void btn_load_Click(object sender, EventArgs e)
    {
        XmlSerializer xs = new XmlSerializer((points).GetType());
        using (TextWriter tw = new StreamWriter(path))
        {
            xs.Serialize(tw, points);
            tw.Close();
        }
    }
    

    Other ways would be using a binary formatter or a custom conversion routine. Xml is relatively stable.

    Here is a short clip:

    0 讨论(0)
提交回复
热议问题