Long pressed button

后端 未结 5 2209
轮回少年
轮回少年 2021-01-18 13:15

I want to repeat an action when a Button is pressed during a long time, like for example the forward button of an MP3 reader. Is there an existing c# event in W

5条回答
  •  不思量自难忘°
    2021-01-18 13:36

    shortest way (and without any task/thread/timer & stopwatch) :)

    DateTime sw;
    bool buttonUp = false;
    const int holdButtonDuration = 2000;
    private void btnTest_MouseDown(object sender, MouseEventArgs e)
    {
        buttonUp = false;
        sw = DateTime.Now;
        while (e.Button == MouseButtons.Left && e.Clicks == 1 && (buttonUp == false && (DateTime.Now - sw).TotalMilliseconds < holdButtonDuration))
            Application.DoEvents();
        if ((DateTime.Now - sw).TotalMilliseconds < holdButtonDuration)
            Test_ShortClick();
        else
            Test_LongClick();
    }
    private void btnTest_MouseUp(object sender, MouseEventArgs e)
    {
        buttonUp = true;
    }
    

提交回复
热议问题