2015-07-19 153 views
1

GLControl定義了各種鼠標事件(例如MouseDown,MouseMove等)。即使使用觸摸筆或觸控筆進行交互,也會引發這些事件。 但是,我很想知道是否有辦法區分這些事件。換句話說,我想處理觸摸事件與鼠標事件不同。如何才能做到這一點?OpenTK:區分GLControl中的觸摸和鼠標事件

回答

1

從我看到的,你必須自己確定事件。這questionthis MSDN page幫助了我。

總而言之,爲了確定觸發事件的原因,您需要撥打GetMessageExtraInfo()user32.dll。 MSDN文章描述瞭如何根據調用結果上設置的位來區分三個輸入。下面是我颳起了用於此目的的代碼:

/// <summary> 
/// The sources of the input event that is raised and is generally 
/// recognized as mouse events. 
/// </summary> 
public enum MouseEventSource 
{ 
    /// <summary> 
    /// Events raised by the mouse 
    /// </summary> 
    Mouse, 

    /// <summary> 
    /// Events raised by a stylus 
    /// </summary> 
    Pen, 

    /// <summary> 
    /// Events raised by touching the screen 
    /// </summary> 
    Touch 
} 

/// <summary> 
/// Gets the extra information for the mouse event. 
/// </summary> 
/// <returns>The extra information provided by Windows API</returns> 
[DllImport("user32.dll")] 
private static extern uint GetMessageExtraInfo(); 

/// <summary> 
/// Determines what input device triggered the mouse event. 
/// </summary> 
/// <returns> 
/// A result indicating whether the last mouse event was triggered 
/// by a touch, pen or the mouse. 
/// </returns> 
public static MouseEventSource GetMouseEventSource() 
{ 
    uint extra = GetMessageExtraInfo(); 
    bool isTouchOrPen = ((extra & 0xFFFFFF00) == 0xFF515700); 

    if (!isTouchOrPen) 
     return MouseEventSource.Mouse; 

    bool isTouch = ((extra & 0x00000080) == 0x00000080); 

    return isTouch ? MouseEventSource.Touch : MouseEventSource.Pen; 
} 

對於我而言,我心中已經覆蓋OnMouse*事件在GLControl類,並用上面的功能進行檢查,並相應地調用我的自定義事件處理程序。