views:

28

answers:

1

I need an event for my CF application, that would trigger after user has pressed an held his finger on the control for 2 seconds. What event can i use, since keyDown event is already used.

+2  A: 

Well, KeyDown is pretty irrelevant for capturing the length of time a finger is pressed. The use of the finger relates to the events Click, MouseDown, MouseUp and MouseMove.

To get the behaviour you're after, the events you should be interested in are MouseDown and MouseUp.

I suggest the best way to do this would be to create your own control base class. Here's one I made earlier (not tested, but should give you a general idea of what to do):

public partial class BaseControl : UserControl
{
    public BaseControl()
    {
        InitializeComponent();

        base.MouseDown += new MouseEventHandler(BaseControl_MouseDown);
        base.MouseUp += new MouseEventHandler(BaseControl_MouseUp);

        MouseHeldTimer = new Timer();
        MouseHeldTimer.Interval = 2000;
        MouseHeldTimer.Tick += new EventHandler(mouseHeldTimer_Tick);

    }

    protected Timer MouseHeldTimer;
    protected bool MouseIsDown;

    void mouseHeldTimer_Tick(object sender, EventArgs e)
    {
        this.MouseHeldTimer.Enabled = false;
        if (this.MouseHeldDown != null)
        {
            this.MouseHeldDown(sender, e);
        }
    }

    void BaseControl_MouseDown(object sender, MouseEventArgs e)
    {
        this.MouseHeldTimer.Enabled = true;
    }

    void BaseControl_MouseUp(object sender, MouseEventArgs e)
    {
        this.MouseHeldTimer.Enabled = false;
    }

    public event MouseHeldDownHandler MouseHeldDown;
    public delegate void MouseHeldDownHandler(object sender, EventArgs e);

}

Basically, the MouseHeldTimer will start with an interval of 2 seconds the moment the user touches their finger to the screen. If the user lifts their finger the timer is stopped. If the user's finger is down for longer than 2 seconds, the delegate event MouseHeldDown will fire. You can then capture this event on your form by doing the following:

control.MouseHeldDown+= new EventHandler(control_MouseHeldDown);

Alternatively, if you only care about the form, you can just use the Form's DoubleClick event as that will fire after holding the mouse down for a second or two.

GenericTypeTea
thanx for the anwser. Yes, i made a mistake and wrote key instead of mouse. Your solution works perfectly.
no9

related questions