Disabling touch screen mouse events Using setwindowshookex (user32.dll)

I have a WPF C # application. I have two screens, and I want to cancel the mouse event on one of the screens, since it is a touch screen and it takes focus from the main screen when the user clicks it. I want to enable the touch only when requesting data from the user, and then block it.

I found some good examples about application hooks using user32.dll, which helps me catch device input. One example shows here: http://www.pinvoke.net/default.aspx/user32.setwindowshookex . And one more, more suitable for mouse events, can be shown here: How to avoid mouse movement on the touch screen

My problem is not to catch the input of the device, but to disable it. The first example above shows how to capture keyboard inputs and retrieve a pressed key, but not turn it off.

The second example does not work. It catches both mouse events, regular usb-mouse device and touch screen device. I can say what it is, but I canโ€™t cancel the touch screen device. In this example, new IntPtr(1); returned for the touch device new IntPtr(1); . It does nothing to input the device.

On the other hand, when I do the opposite, which means returning a new IntPtr (1); on every mouse event, but the touch seems to be that my normal mouse doesnโ€™t move at all (while the mouse of the touch device moves).

This method can help block a normal mouse, but not a touch mouse, since no code that I return will block this mouse (I find this mouse and return the code, I checked it many times, the code does nothing).

Here is my code Based on the examples above:

  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); } 
+6
source share
1 answer

If your main goal is to avoid the focus being moved from your main window, then there is a simpler workaround using WS_EX_NOACTIVATE

see: Don't focus, but allow interaction?

+2
source

Source: https://habr.com/ru/post/945659/


All Articles