OpenTK:区分GLControl中的触摸和鼠标事件
OpenTK: Distinguish between touch and mouse events in GLControl
GLControl定义了各种鼠标事件(如MouseDown
、MouseMove
等)。即使使用触摸或手写笔进行交互,也会引发这些事件。
但是,我很想知道是否有办法区分这些事件。换句话说,我想以不同于鼠标事件的方式处理触摸事件。如何做到这一点?
据我所见,您必须自己确定事件。这 question and this MSDN page 帮助了我。
总而言之,为了确定是什么触发了事件,您需要从 user32.dll
调用 GetMessageExtraInfo()
。 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;
}
为了我的目的,我已经覆盖了 GLControl class 中的 OnMouse*
事件并使用上面的函数执行检查并相应地调用我的自定义事件处理程序。
GLControl定义了各种鼠标事件(如MouseDown
、MouseMove
等)。即使使用触摸或手写笔进行交互,也会引发这些事件。
但是,我很想知道是否有办法区分这些事件。换句话说,我想以不同于鼠标事件的方式处理触摸事件。如何做到这一点?
据我所见,您必须自己确定事件。这 question and this MSDN page 帮助了我。
总而言之,为了确定是什么触发了事件,您需要从 user32.dll
调用 GetMessageExtraInfo()
。 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;
}
为了我的目的,我已经覆盖了 GLControl class 中的 OnMouse*
事件并使用上面的函数执行检查并相应地调用我的自定义事件处理程序。