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();