为什么这个双击检测代码不可靠? [英] Why is this double-click detection code unreliable?
问题描述
我正在尝试通过一些简单的挑战来学习 Rx,首先是通过仅观看 MouseUp 事件来检测双击.我想出了以下解决方案(此代码可通过创建 WPF 项目并添加 Reactive Extensions NuGet 包进行编译):
I'm trying to learn Rx through some simple challenges, first by detecting double-clicks by watching only MouseUp events. I came up with the following solution (this code is compilable by creating a WPF project and adding the Reactive Extensions NuGet package):
using System;
using System.Reactive.Linq;
using System.Windows.Input;
namespace WpfApplication1 {
public partial class MainWindow {
public MainWindow() {
InitializeComponent();
var clickEvents = Observable
.FromEventPattern<MouseButtonEventArgs>(this, "MouseLeftButtonUp");
var intraClickInterval = TimeSpan.FromMilliseconds(500);
var interClickInterval = TimeSpan.FromMilliseconds(1000);
var doubleClicks = clickEvents
.TimeInterval()
.SkipWhile(t => t.Interval < interClickInterval)
.FirstAsync(t => t.Interval < intraClickInterval)
.Repeat();
var doubleClickReady = doubleClicks
.Delay(interClickInterval)
.Subscribe(e => Console.WriteLine("Now accepting double click!"));
doubleClicks.Subscribe(e => Console.WriteLine(e.Interval));
}
}
}
我见过的大多数解决方案都没有处理多次点击,所以这个想法是在每次双击之间总是等待一定的时间间隔(interClickInterval),然后双击是下一个鼠标向上事件间隔小于intraClickInterval.我还添加了一条消息,该消息在每次双击后的 interClickInterval 过去后打印,以便我知道何时应该注册双击.
Most solutions I've seen don't deal with multiple clicks, so the idea is to always wait for a certain interval (interClickInterval) between each double-click, and then a double-click is the next mouse up event that has an interval smaller than the intraClickInterval. I've also added a message that gets printed after the interClickInterval elapses following each double-click so that I know when double-clicks are supposed to register.
这似乎总体上运行良好,但似乎有时即使我看到现在接受双击!"他们也不会注册.信息.如果我在消息出现之前单击,这似乎尤其会发生.什么可能导致这种情况?
This seems to work well in general, but it seems that sometimes they do not register even though I see the "Now accepting double-click!" message. This seems to happen particularly if I click just before the message appears. What could be causing this?
我认为双击检测实际上是正确的,它的消息不是.基本上,它会忽略双击后的任何后续单击.我需要做类似的事情
I think the double-click detection is actually correct, it's the message that isn't. Basically, it ignores any subsequent click after a double-click. I'd need to do something like
- 对于每个双击事件
- 等待interClickInterval
- 如果此后未注册任何点击,则显示现在接受双重点击!"
但我不确定如何实现...
But I'm not sure how to achieve that...
推荐答案
所以我怀疑,检测代码是正确的,是消息时间错误.它没有考虑双击后的任何后续点击;它总是在每次双击后 1000 毫秒打印其消息.这是就绪消息的一个版本,它在每次双击后正确等待 1000 毫秒自上次单击以来已经过去:
So as I suspected, the detection code is correct, it's the message that was timed incorrectly. It didn't take into account any subsequent clicks after a double-click; it just always printed its message 1000ms after each double-click. Here's a version of the ready message that correctly waits for 1000ms to have elapsed since the last click after each double-click:
var doubleClickReady = doubleClicks
.SelectMany(clickEvents
.Buffer(interClickInterval)
.FirstAsync(b => b.Count == 0));
doubleClickReady
.Subscribe(e => Console.WriteLine("Now accepting double click!"));
这篇关于为什么这个双击检测代码不可靠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!