|
| 1 | +using System; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using System.Threading; |
| 4 | + |
| 5 | +namespace Loupe |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Low-level mouse and keyboard hooks running on their own thread. |
| 9 | + /// |
| 10 | + /// Windows calls low-level hooks on the thread that installed them, and every mouse and key event |
| 11 | + /// in the system waits for the callback. Keeping them away from the rendering thread means a slow |
| 12 | + /// frame can never make the pointer itself lag. |
| 13 | + /// </summary> |
| 14 | + sealed class InputHooks : IDisposable |
| 15 | + { |
| 16 | + readonly Func<int, bool> onWheel; |
| 17 | + readonly Action onEscape; |
| 18 | + readonly ManualResetEvent ready = new ManualResetEvent(false); |
| 19 | + readonly Thread thread; |
| 20 | + |
| 21 | + // Delegates are kept in fields so they stay alive while the hooks are installed. |
| 22 | + Native.LowLevelHookProc mouseProc, keyboardProc; |
| 23 | + IntPtr mouseHook, keyboardHook; |
| 24 | + uint threadId; |
| 25 | + |
| 26 | + /// <param name="onWheel"> |
| 27 | + /// Called on the hook thread for every wheel notch with its delta; return true to swallow it. |
| 28 | + /// Must be quick: the whole system's input waits for it. |
| 29 | + /// </param> |
| 30 | + /// <param name="onEscape">Called on the hook thread when Esc is pressed. The key is never swallowed.</param> |
| 31 | + public InputHooks(Func<int, bool> onWheel, Action onEscape) |
| 32 | + { |
| 33 | + this.onWheel = onWheel; |
| 34 | + this.onEscape = onEscape; |
| 35 | + |
| 36 | + thread = new Thread(Run) { Name = "Loupe input hooks", IsBackground = true, Priority = ThreadPriority.AboveNormal }; |
| 37 | + thread.Start(); |
| 38 | + ready.WaitOne(); |
| 39 | + } |
| 40 | + |
| 41 | + public bool IsInstalled |
| 42 | + { |
| 43 | + get { return mouseHook != IntPtr.Zero; } |
| 44 | + } |
| 45 | + |
| 46 | + void Run() |
| 47 | + { |
| 48 | + threadId = Native.GetCurrentThreadId(); |
| 49 | + mouseProc = MouseCallback; |
| 50 | + keyboardProc = KeyboardCallback; |
| 51 | + IntPtr module = Native.GetModuleHandle(null); |
| 52 | + mouseHook = Native.SetWindowsHookEx(Native.WH_MOUSE_LL, mouseProc, module, 0); |
| 53 | + keyboardHook = Native.SetWindowsHookEx(Native.WH_KEYBOARD_LL, keyboardProc, module, 0); |
| 54 | + ready.Set(); |
| 55 | + |
| 56 | + // Hooks are delivered through this thread's message loop. |
| 57 | + Native.MSG msg; |
| 58 | + while (Native.GetMessage(out msg, IntPtr.Zero, 0, 0) > 0) |
| 59 | + { |
| 60 | + Native.TranslateMessage(ref msg); |
| 61 | + Native.DispatchMessage(ref msg); |
| 62 | + } |
| 63 | + |
| 64 | + if (mouseHook != IntPtr.Zero) Native.UnhookWindowsHookEx(mouseHook); |
| 65 | + if (keyboardHook != IntPtr.Zero) Native.UnhookWindowsHookEx(keyboardHook); |
| 66 | + } |
| 67 | + |
| 68 | + IntPtr MouseCallback(int nCode, IntPtr wParam, IntPtr lParam) |
| 69 | + { |
| 70 | + if (nCode >= 0 && wParam.ToInt32() == Native.WM_MOUSEWHEEL) |
| 71 | + { |
| 72 | + var data = (Native.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(Native.MSLLHOOKSTRUCT)); |
| 73 | + int delta = unchecked((short)(data.mouseData >> 16)); |
| 74 | + if (onWheel(delta)) |
| 75 | + return new IntPtr(1); // swallow the scroll so the app underneath doesn't see it |
| 76 | + } |
| 77 | + return Native.CallNextHookEx(mouseHook, nCode, wParam, lParam); |
| 78 | + } |
| 79 | + |
| 80 | + IntPtr KeyboardCallback(int nCode, IntPtr wParam, IntPtr lParam) |
| 81 | + { |
| 82 | + if (nCode >= 0 && wParam.ToInt32() == Native.WM_KEYDOWN && Marshal.ReadInt32(lParam) == Native.VK_ESCAPE) |
| 83 | + onEscape(); |
| 84 | + return Native.CallNextHookEx(keyboardHook, nCode, wParam, lParam); |
| 85 | + } |
| 86 | + |
| 87 | + public void Dispose() |
| 88 | + { |
| 89 | + Native.PostThreadMessage(threadId, Native.WM_QUIT, IntPtr.Zero, IntPtr.Zero); |
| 90 | + thread.Join(1000); |
| 91 | + ready.Close(); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments