2013-04-08 34 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