CF keyDown 事件(定时)

发布于 2024-08-31 21:50:22 字数 74 浏览 4 评论 0原文

我的 CF 应用程序需要一个事件,该事件将在用户按下并按住控件 2 秒后触发。我可以使用什么事件,因为 keyDown 事件已被使用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮颜 2024-09-07 21:50:22

嗯,KeyDown 与捕获手指按下的时间长度无关。手指的使用与事件 ClickMouseDownMouseUpMouseMove 相关。

要获得您想要的行为,您应该感兴趣的事件是 MouseDownMouseUp

我建议最好的方法是创建您自己的控件基类。这是我之前制作的一个(未测试,但应该可以让您大致了解要做什么):

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);

}

基本上,当用户将手指触摸屏幕时,MouseHeldTimer 将以 2 秒的间隔启动。如果用户抬起手指,计时器就会停止。如果用户手指按下的时间超过 2 秒,将触发委托事件 MouseHeldDown。然后,您可以通过执行以下操作在表单上捕获此事件:

control.MouseHeldDown+= new EventHandler(control_MouseHeldDown);

或者,如果您只关心表单,则可以仅使用表单的 DoubleClick 事件,因为该事件会在按住鼠标一段时间后触发。第二个或两个。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文