2013-04-08 39 views
1

我正在C#中开发一个Kinect游戏,其中用户需要同时单击2个按钮,方法是用一只手将鼠标悬停在一个按钮上。处理多个悬停以单击C#中kinect的事件

但是,就我现在的代码而言,当用户用一只手悬停在按钮上时,其他按钮被禁用,而另一只手只能在第一只手停在按钮上方时才能点击。

为了解决这个问题,我正在考虑在第一次点击正在处理时排队第二次点击。要做到这一点,我已经根据this link

private Queue<System.Windows.Controls.Button> Button_Queue = new Queue<System.Windows.Controls.Button>(); 
private bool isProcessing = false; 

private void Button_Click((object sender, RoutedEventArgs e){ 

if(isProcessing){ 
Button_Queue.Enqueue(this); 
} 
else 
{ 
isProcessing = true; 

// code here 

isProcessing = false; 
while(Button_Queue.Count > 0){ 
    Button_Queue.Dequeue().PerformClick(); 
} 
} 

用下面的代码然而,Button_Queue.Enqueue(this)行显示错误

“为Queue.Enqueue的最佳重载的方法匹配具有无效 参数。 “

我猜这是因为按钮点击事件不能与类型Button声明的队列中排队。

对于如何创建此按钮点击事件队列或以其他方式处理来自用户的多个点击,您有任何建议吗?

回答

1

您不需要排队事件。如果isProcessing为真,那么另一个按钮已经被点击,所以你可以处理该事件,从这一点开始按钮点击。

您可以测量两次点击之间的时间,以确定它是否验证为“同时点击两个按钮”事件。

1

您是否考虑过更低级别的方法?首先想到的是创建两个热点区域而不是按钮,并监视用户的手是否同时在这些区域内。

1

目前还不清楚为什么当您的手悬停在另一个对象上时另一个按钮被禁用。如果没有看到代码,我会说你正在做一些会导致这种情况的事情 - 而且没有理由。

此外,您应该使用以手势系统为中心的交互概念,而不是为鼠标/键盘输入编写的内容。使用常规的UI对象并以并行传统输入的方式与它们进行交互只会混淆用户。

看一看下面两个例子,其中使用“鼠标悬停点击”和“按即点击”互动

在这两种情况下,您都在自定义控件上使用命中测试来处理事件。下面是我在我的应用程序之一,使用的命中测试函数的例子:

private void HitTestHand(HandPosition hand) 
{ 
    // quick fix to null pointer exception on exit. 
    if (Application.Current.MainWindow == null) 
     return; 

    Point pt = new Point(hand.X, hand.Y); 
    IInputElement input = Application.Current.MainWindow.InputHitTest(pt); 

    if (hand.CurrentElement != input) 
    { 
     var inputObject = input as DependencyObject; 
     var currentObject = hand.CurrentElement as DependencyObject; 

     // If the new input is a child of the current element then don't fire the leave event. 
     // It will be fired later when the current input moves to the parent of the current element. 
     if (hand.CurrentElement != null && Utility.IsElementChild(currentObject, inputObject) == false) 
     { 
      // Raise the HandLeaveEvent on the CurrentElement, which at this point is the previous element the hand was over. 
      hand.CurrentElement.RaiseEvent(new HandInputEventArgs(HoverDwellButton.HandLeaveEvent, hand.CurrentElement, hand)); 
     } 

     // If the current element is the parent of the new input element then don't 
     // raise the entered event as it has already been fired. 
     if (input != null && Utility.IsElementChild(inputObject, currentObject) == false) 
     { 
      input.RaiseEvent(new HandInputEventArgs(HoverDwellButton.HandEnterEvent, input, hand)); 
     } 

     hand.CurrentElement = input; 
    } 
    else if (hand.CurrentElement != null) 
    { 
     hand.CurrentElement.RaiseEvent(new HandInputEventArgs(HoverDwellButton.HandMoveEvent, hand.CurrentElement, hand)); 
    } 
} 

注意的事件被手光标下方的元素上发射。这些元素的例子可以在上面的两个链接中找到(HoverDwellButton就是我用上面的代码示例)。

两个不同元素或相同元素上的两个事件可以随时触发。你可以很容易地跟踪哪个用户在哪个按钮上,如果该按钮正在被按下,或者它已被按下。

所有这一切的关键是不使用不是为手势系统设计的UI范例!不要试图将键盘/鼠标事件结构硬塞进一个基于手势的系统 - 它只会让你长期受到更多的痛苦,并引起用户的困惑。

+0

非常感谢这个建议!我发现早些时候一直在处理代码的另一位程序员一直在使用同一个变量来输入双手,这就是为什么我无法同时使用它们的原因。我现在已经改变了这一点,以便双手分开处理。 – Agrim 2013-04-09 02:32:20