Mouse event fires more than once

Hello everyone,

so I created a mouse event that detects mouse coordinates on click.
my issue the that with each click the events keeps on firing more. and I don’t know how to make it only work once.

also I want to return data from that event.

can anyone help me with this , I’ve been stuck on it for long now.
Thank you, :pray:
Youssef.


Mouse click event.gh (2.9 KB)

You are instantiating on each solution a new FeedBack() object. Each time you do this you’ll get a number of new FeedBack() objects that all react.

In your overrides add a public static FeedBack feed = new FeedBack();, and remove the instantiation from the RunScript part (var feed = new FeedBack();)

2 Likes
private void RunScript(bool run, ref object A)
{
  FeedBack.Instance.Comp = Component;
  FeedBack.Instance.Enabled = run;
  A = Point;
}
//<Custom additional code> 
public static Point3d Point;
public class FeedBack : Rhino.UI.MouseCallback
{
  public IGH_Component Comp;
  private FeedBack()
  {
    Enabled = false;
  }
  private static FeedBack _instance;
  public static FeedBack Instance
  {
    get
    {
      if(_instance == null)
        _instance = new FeedBack();
      return _instance;
    }
  }
  protected override void OnMouseDown(Rhino.UI.MouseCallbackEventArgs e)
  {
    if(e.Button != System.Windows.Forms.MouseButtons.Left) return;
    var v = e.View;
    var line = v.ActiveViewport.ClientToWorld(e.ViewportPoint);
    var cplane = v.ActiveViewport.ConstructionPlane();
    double t;
    Rhino.Geometry.Intersect.Intersection.LinePlane(line, cplane, out t);
    Point = line.PointAt(t);
    Comp.ExpireSolution(true);
    e.Cancel = true;
    base.OnMouseDown(e);
  }
}
//<Custom additional code> 

MouseCallBack.gh (3.5 KB)

1 Like

you’re so right. thank you that helped me grasp things much more :pray: :pray:

your solution basically solved my issue. and it showed me how instantiate class withing itself. Thank you :pray: