禁用触摸屏鼠标设备事件使用 setwindowshookex (user32.dll)

6
我有一个WPF C#应用程序。我有两个屏幕,并且我想取消其中一个屏幕上的鼠标事件,因为它是一个触摸屏,每当用户按下它时,它会将焦点从主屏幕上移开。我想在向用户请求数据时启用触摸,然后阻止它。
我找到了一些关于使用user32.dll的应用程序钩子的好例子,这可以帮助我捕获设备输入。一个例子在这里显示:http://www.pinvoke.net/default.aspx/user32.setwindowshookex。另一个更适合鼠标事件的示例可以在此处显示:How to avoid mouse move on Touch 我的问题不在于捕获设备输入,而在于禁用它。上面的第一个示例显示如何捕获键盘输入并提取按下的键,但不能禁用它。
第二个示例也不起作用。它捕获了常规usb鼠标设备和触摸屏设备的所有鼠标事件。我可以区分哪个是哪个,但我无法取消触摸屏设备。在示例中,对于触摸设备返回了new IntPtr(1);,但这对设备输入没有任何影响。
另一方面,当我做相反的事情,即在除触摸事件之外的每个鼠标事件上返回new IntPtr(1);时,我的常规鼠标似乎根本不动(而触摸设备鼠标确实移动)。
这种方法可以帮助阻止常规鼠标,但无法阻止触摸鼠标,因为我无论如何都不能阻止该鼠标(我找到了该鼠标并返回了一个代码,我检查了很多次,但该代码无效)。
以下是基于上述示例的我的代码:
   static readonly LowLevelMouseProc hookCallback = HookCallback;
        static IntPtr hookId = IntPtr.Zero;

        public DisableTouchConversionToMouse()
        {
            hookId = SetHook(hookCallback);
        }

        static IntPtr SetHook(LowLevelMouseProc proc)
        {
            var moduleHandle = UnsafeNativeMethods.GetModuleHandle(null);

            var setHookResult = UnsafeNativeMethods.SetWindowsHookEx(HookType.WH_MOUSE_LL, 
proc, moduleHandle, 0);
            if (setHookResult == IntPtr.Zero)
            {
                throw new Win32Exception();
            }
            return setHookResult;
        }

        delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

        static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {

            if (nCode >= 0)
            {
                var info = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));

                var extraInfo = (uint)info.dwExtraInfo.ToInt32();

               // if ((extraInfo & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH)
                if (extraInfo>0)
                {
               Console.WriteLine("TOUCH SCREEN FOUND!");
                    return new IntPtr(1);
                }
            }

            return UnsafeNativeMethods.CallNextHookEx(hookId, nCode, wParam, lParam);
        }
   public enum HookType : int
        {
            WH_JOURNALRECORD = 0,
            WH_JOURNALPLAYBACK = 1,
            WH_KEYBOARD = 2,
            WH_GETMESSAGE = 3,
            WH_CALLWNDPROC = 4,
            WH_CBT = 5,
            WH_SYSMSGFILTER = 6,
            WH_MOUSE = 7,
            WH_HARDWARE = 8,
            WH_DEBUG = 9,
            WH_SHELL = 10,
            WH_FOREGROUNDIDLE = 11,
            WH_CALLWNDPROCRET = 12,
            WH_KEYBOARD_LL = 13,
            WH_MOUSE_LL = 14
        }

        [StructLayout(LayoutKind.Sequential)]
        struct POINT
        {

            public int x;
            public int y;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct MSLLHOOKSTRUCT
        {
            public POINT pt;
            public uint mouseData;
            public uint flags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        [SuppressUnmanagedCodeSecurity]
        static class UnsafeNativeMethods
        {
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern IntPtr SetWindowsHookEx(HookType code, LowLevelMouseProc lpfn, IntPtr hMod,
                uint dwThreadId);

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool UnhookWindowsHookEx(IntPtr hhk);

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
                IntPtr wParam, IntPtr lParam);

            [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern IntPtr GetModuleHandle(string lpModuleName);
        }
1个回答

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接