c# - 如何从SetWindowsHookEx中区分触摸与鼠标事件 [英] how to distinguish touch vs mouse event from SetWindowsHookEx in c#
问题描述
我找到了一种监听鼠标事件的方法,但我真正想要的是触摸事件而不是鼠标.他们似乎共享相同的代码.有没有办法判断事件是触摸而不是鼠标?谢谢
I have found a way to listen to the mouse event, but what I really want is the touch event not mouse. They seem to share the same code. Is there any way to tell if the event was touch and not mouse? Thanks
[DllImport("user32.dll")]
static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc callback, IntPtr hInstance, uint threadId);
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);
private delegate IntPtr LowLevelMouseProc (int nCode, IntPtr wParam, IntPtr lParam);
const int WH_MOUSE_LL = 14;
const int WM_KEYDOWN = 0x100;
private LowLevelMouseProc _proc = hookProc;
private static IntPtr hhook = IntPtr.Zero;
public void SetHook()
{
IntPtr hInstance = LoadLibrary("User32");
hhook = SetWindowsHookEx(WH_MOUSE_LL, _proc, hInstance, 0);
}
public static IntPtr hookProc(int code, IntPtr wParam, IntPtr lParam)
{
System.Diagnostics.Debug.Print("Param: " + wParam + ", CODE: " + code + "\n");
}
private void Form1_Load(object sender, RoutedEventArgs e)
{
SetHook();
}
推荐答案
hookProc
回调的 lParam
参数是一个指向 MSLLHOOKSTRUCT
的指针>.它包含一个非常记录不佳的dwExtraInfo
变量,它告诉你它是否是通过触摸生成的.
The lParam
argument of your hookProc
callback is a pointer to an MSLLHOOKSTRUCT
. It contains a very poorly documented dwExtraInfo
variable, which tells you whether it was generated from a touch.
如果 0xFF515700
中的所有位都在 dwExtraInfo
中设置,则回调被调用以响应触摸:
If all of the bits in 0xFF515700
are set in dwExtraInfo
, then the callback was invoked in response to a touch:
[StructLayout(LayoutKind.Sequential)]
struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
const int TOUCH_FLAG = 0xFF515700;
bool IsTouch(IntPtr lParam)
{
MSLLHOOKSTRUCT hookData = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam,
typeof(MSLLHOOKSTRUCT));
uint extraInfo = (uint)info.dwExtraInfo.ToInt32();
if ((extraInfo & TOUCH_FLAG) == TOUCH_FLAG)
return true;
return false;
}
这篇关于c# - 如何从SetWindowsHookEx中区分触摸与鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!