I have code very similar to this question running in a Windows-tray application, even with this exact code from the question I get the same behavior. All of this works well in classic Windows applications like Firefox, Chrome, Windows Explorer, etc. However, when the mouse focus falls on a UWP application such as Edge or Calendar or Mail, the scroll becomes unstable and after several tens of scrolls my application runs, it hangs and cannot even be terminated from the task manager (permission denied), this behavior is very reproducible.
Paste the code from the question here:
using System; using System.Diagnostics; using System.Windows.Forms; using System.Runtime.InteropServices; namespace EnableMacScrolling { class InterceptMouse { const int INPUT_MOUSE = 0; const int MOUSEEVENTF_WHEEL = 0x0800; const int WH_MOUSE_LL = 14; private static LowLevelMouseProc _proc = HookCallback; private static IntPtr _hookID = IntPtr.Zero; public static void Main() { _hookID = SetHook(_proc); if (_hookID == null) { MessageBox.Show("SetWindowsHookEx Failed"); return; } 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_MOUSEWHEEL == (MouseMessages)wParam) { MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); Console.WriteLine(hookStruct.mouseData); if (hookStruct.flags != -1)
Is it possible that I am dealing with a window error here? Any guidance on how I can find out what is going on?
Update:
I created a Win32 test application in C ++ to more easily reproduce / demonstrate the problem. The problem is SendCommand, which, when executed, when some classic application is in focus, works fine. However, when executed while the UWP application is in focus, or even the Windows start / start menu causes the calling application (my application) to freeze and get stuck until windows restart.
An effective workaround / solution to this problem is to make a SendCommand call to another thread from the thread that processes the hook callback. Immediately starting the thread that SendCommand executes and returning from the hook callback causes the desired behavior and does not cause any problems.
Vasil source share