Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Win forms, log all clicks?

Is there a way to log all of the clicks in a Win Forms application? I'd like to intercept clicks and record the action and the name of the control that caused it.

Is this possible?

Thanks in advance.

UPDATE: I'm looking for an application wide solution, is there no way to add a listener to the windows event queue (or what ever it is called)?

like image 591
chillitom Avatar asked Apr 06 '10 10:04

chillitom


2 Answers

You can do this by having your app's main form implement the IMessageFilter interface. You can screen the Window messages it gets and look for clicks. For example:

  public partial class Form1 : Form, IMessageFilter {
    public Form1() {
      InitializeComponent();
      Application.AddMessageFilter(this);
      this.FormClosed += (o, e) => Application.RemoveMessageFilter(this);
    }

    public bool PreFilterMessage(ref Message m) {
      if (m.Msg == 0x201 || m.Msg == 0x203) {  // Trap left click + double-click
        string name = "Unknown";
        Control ctl = Control.FromHandle(m.HWnd);
        if (ctl != null) name = ctl.Name;
        Point pos = new Point(m.LParam.ToInt32());
        Console.WriteLine("Click {0} at {1}", name, pos);
      }
      return false;
    }
  }

Note that this logs all clicks in any window of your app.

like image 119
Hans Passant Avatar answered Oct 13 '22 10:10

Hans Passant


You could use Spy++ or WinSpy++ to achieve this.

alt text http://www.catch22.net/sites/default/files/images/winspy1.img_assist_custom.jpg

But I'm not sure how you can achieve the same thing yourself. If it's possible you'd need to do it via a low-level Windows API hook or a message handler that gives you access to all the message in your applications queue.

like image 40
Matt Warren Avatar answered Oct 13 '22 08:10

Matt Warren