How can I disable the mouse click event system using C #?

Hey guys, I have a laptop with a very sensitive touchpad, and I wanted to code a small program that could block mouse input when I was printing paper or something like that.

I did not think it would be difficult to do, given everything I saw on the low-level hooks, but I was wrong (startling, right?).

I looked at a few examples, but the examples I saw either block the keyboard, or the mouse, or just hide the mouse.

Any help with this would be great.

+4
source share
2 answers

As you mentioned, you can do this using the low-level mouse hook ( WH_MOUSE_LL) , although it is somewhat wrong). What happens when you establish that you will receive notifications about each mouse input event ( WM_MOUSEMOVE , WM_LBUTTONDOWN , WM_RBUTTONDOWN , WM_MBUTTONDOWN , WM_XBUTTONDOWN , WM_NCXBUTTONDOWN , equivalent up events for each of them, WM_MOUSEWHEEL and WM_MOUSEHWHEEL ). After you have finished processing each event, you must call the CallNextHookEx function, which passes the event information to the next application in the hook chain. However, if you want some other program to not receive mouse input information, you can simply skip calling this function at the end of your hook procedure. The Notes section of the above documentation explains this as follows:

Calling CallNextHookEx is optional, but it is highly recommended; otherwise, other applications that Installed hooks will not receive hook notifications and may behave incorrectly as a result. You should call CallNextHookEx unless you absolutely need to prevent notification from other applications.

And as it turned out, low-level mouse hooks are actually not that complex in C #. Actually, I just encoded myself. But instead of publishing this monstrosity of the library, I refer you to a simpler piece of code posted on Steven Tuub's blog , which I reprinted here with syntax highlighting for convenience:

 class InterceptMouse { private static LowLevelMouseProc _proc = HookCallback; private static IntPtr _hookID = IntPtr.Zero; public static void Main() { _hookID = SetHook(_proc); Application.Run(); UnhookWindowsHookEx(_hookID); } private static IntPtr SetHook(LowLevelMouseProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0); } } private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam); private static IntPtr HookCallback( int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam) { MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); Console.WriteLine(hookStruct.pt.x + ", " + hookStruct.pt.y); } return CallNextHookEx(_hookID, nCode, wParam, lParam); } private const int WH_MOUSE_LL = 14; private enum MouseMessages { WM_LBUTTONDOWN = 0x0201, WM_LBUTTONUP = 0x0202, WM_MOUSEMOVE = 0x0200, WM_MOUSEWHEEL = 0x020A, WM_RBUTTONDOWN = 0x0204, WM_RBUTTONUP = 0x0205 } [StructLayout(LayoutKind.Sequential)] private struct POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] private struct MSLLHOOKSTRUCT { public POINT pt; public uint mouseData; public uint flags; public uint time; public IntPtr dwExtraInfo; } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName); } 

As I explained above, you will want to change your HookCallback method to not call CallNextHookEx after the mouse event has finished processing, but instead return something like new IntPtr(1) .

EDIT: And, as already mentioned, there are probably other simpler and cleaner solutions to this problem. Your trackpad drivers are a great place to look for the "Ignore accidental mouse input on entry" option. If you do not have this option, you are probably using standard Windows mouse drivers. Try downloading drivers from your trackpad manufacturer from the laptop manufacturer’s website (for what it's worth, most tracked tracks without Apple, which I saw Synaptics ).

+7
source

With many touchpad drivers, this is possible. That is, when you type, it ignores the touchpad input. You can also disable short-term clicks by relying on the actual buttons on the touch panel to click.

Try the driver setup utility first before trying to write your own.

+2
source

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


All Articles