public static Deleter CreateGlobalOutOfContextHook(ISet <uint> eventTypes, WinEventCallback handler)
        {
            uint min = eventTypes.Min();
            uint max = eventTypes.Max();

            WINEVENTPROC?callback = delegate(HWINEVENTHOOK hWinEventHook, uint eventType,
                                             HWND hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
            {
                if (eventTypes.Contains(eventType))
                {
                    handler(eventType, hwnd, idObject, idChild, dwEventThread, dwmsEventTime);
                }
            };

            IntPtr hHook = SetWinEventHook(min, max, IntPtr.Zero, callback, idProcess: 0, idThread: 0, WINEVENT_OUTOFCONTEXT);

            if (IntPtr.Zero == hHook)
            {
                throw new Win32Exception().WithMessage("Could not set a window message event hook!");
            }

            return(new Deleter(() =>
            {
                // Capture and null callback
                callback = null;
                UnhookWinEvent(new(hHook));
            }));
        }
Exemple #2
0
        private void MessagePump(IWinEventCallbacks callbacks, uint pid)
        {
            Log.Comment("Accessibility message pump thread started");

            WinEventCallback callback = new WinEventCallback(callbacks, pid);

            IntPtr hWinEventHook = User32.SetWinEventHook(
                User32.WinEventId.EVENT_CONSOLE_CARET,
                User32.WinEventId.EVENT_CONSOLE_END_APPLICATION,
                IntPtr.Zero,                                        // Use our own module
                new User32.WinEventDelegate(callback.WinEventProc), // Our callback function
                0,                                                  // All processes
                0,                                                  // All threads
                User32.WinEventFlags.WINEVENT_SKIPOWNPROCESS | User32.WinEventFlags.WINEVENT_SKIPOWNTHREAD);

            NativeMethods.Win32NullHelper(hWinEventHook, "Registering accessibility event hook.");

            Log.Comment("Entering accessibility pump loop.");
            while (!messagePumpDone)
            {
                User32.MSG msg;
                if (User32.PeekMessage(out msg, IntPtr.Zero, 0, 0, User32.PM.PM_REMOVE))
                {
                    User32.DispatchMessage(ref msg);
                }

                Thread.Sleep(200);
            }
            Log.Comment("Exiting accessibility pump loop.");

            NativeMethods.Win32BoolHelper(User32.UnhookWinEvent(hWinEventHook), "Unregistering accessibility event hook.");
            Log.Comment("Accessibility message pump thread ended");
        }
Exemple #3
0
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            _windowHandle = new WindowInteropHelper(this).Handle;
            Handle        = _windowHandle;
            _source       = HwndSource.FromHwnd(_windowHandle);
            _source.AddHook(HookCallbackManager.WindowsKeyHookCallback);

            WinApiProxy.RegisterHotKey(_windowHandle, KeyHookConstants.HOTKEY_ID, KeyHookConstants.MOD_ALT, KeyHookConstants.VK_OEM_COMMA); //ALT + ,
            _windowEventCallback = HookCallbackManager.WindowsWindowCreatedHook;
            HookManager.RegisterCallBack(ref _windowEventCallback);
            HookManager.SubscribeToWindowEvents();
        }
 public extern static IntPtr SetWinEventHook(
     uint eventMin, uint eventMax,
     IntPtr hmodWinEventProc,
     WinEventCallback callback,
     uint idProcess, uint idThread,
     uint dwFlags);
Exemple #5
0
 public static extern IntPtr SetWinEventHook(
     uint eventMin, uint eventMax, IntPtr dllHandle,
     WinEventCallback callback, uint processId,
     uint threadId, uint flags);
Exemple #6
0
 public static void RegisterCallBack(ref WinEventCallback window)
 {
     _mainWin = window;
 }