예제 #1
0
 public void Hook()
 {
     if (hookId == User32.SafeHookHandle.Null)
     {
         proc = HookProcedure;
         using (var curProcess = Process.GetCurrentProcess()) {
             using (ProcessModule curModule = curProcess.MainModule) {
                 hookId = User32.SetWindowsHookEx(User32.WindowsHookType.WH_KEYBOARD_LL, proc, Kernel32.GetModuleHandle(curModule.ModuleName), 0);
             }
         }
     }
 }
예제 #2
0
 private static User32.SafeHookHandle SetKeyboardHook(User32.WindowsHookDelegate proc)
 {
     using (Process curProcess = Process.GetCurrentProcess())
         using (ProcessModule curModule = curProcess.MainModule)
         {
             var hook = User32.SetWindowsHookEx(
                 User32.WindowsHookType.WH_KEYBOARD_LL,
                 proc,
                 Kernel32.GetModuleHandle(curModule.ModuleName).DangerousGetHandle(),
                 0);
             _hooks.Add(hook);
             return(hook);
         }
 }
        /// <inheritdoc />
        /// <summary>
        ///   Starts capturing mouse events
        /// </summary>
        protected override void Lock()
        {
            if (this.hookHandle != IntPtr.Zero)
            {
                throw new InvalidOperationException("The previous hook must be released before capturing the mouse again.");
            }

            GC.KeepAlive(this.lowLevelMouseProc = LowLevelMouseProc);
            if ((this.hookHandle = User32.SetWindowsHookEx(User32.WindowsHookType.WH_MOUSE_LL, this.lowLevelMouseProc)) ==
                IntPtr.Zero)
            {
                Log.Error($"SetWindowHookEx() failed (LE 0x{Marshal.GetLastWin32Error():x8})");
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            Log.Debug("low-level mouse hook has been installed");
        }
        /// <inheritdoc />
        /// <summary>
        ///   Starts capturing keyboard events
        /// </summary>
        protected override void Lock()
        {
            if (this.hookHandle != IntPtr.Zero)
            {
                throw new InvalidOperationException("The previous hook must be released before capturing the keyboard again.");
            }

            GC.KeepAlive(this.lowLevelKeyboardHook = LowLevelKeyboardProc);
            if ((this.hookHandle =
                     User32.SetWindowsHookEx(User32.WindowsHookType.WH_KEYBOARD_LL, this.lowLevelKeyboardHook)) ==
                IntPtr.Zero)
            {
                Log.Error($"SetWindowHookEx() failed (last error: 0x{Marshal.GetLastWin32Error():x8})");
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            Log.Info("desktop keyboard hook locked");
        }
예제 #5
0
        /// <inheritdoc />
        /// <summary>
        ///   Starts capturing keyboard events
        /// </summary>
        protected override void Lock()
        {
            if (this.hookHandle != IntPtr.Zero)
            {
                throw new InvalidOperationException("The previous hook must be released before capturing the keyboard again.");
            }

            GC.KeepAlive(this.lowLevelKeyboardHook = LowLevelKeyboardProc);
            if ((this.hookHandle =
                     User32.SetWindowsHookEx(User32.WindowsHookType.WH_KEYBOARD_LL, this.lowLevelKeyboardHook)) ==
                IntPtr.Zero)
            {
                Log.WriteLine(LogLevel.Error, $"SetWindowHookEx() failed (LE 0x{Marshal.GetLastWin32Error():x8})");
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            Log.WriteLine(LogLevel.Debug, "low-level keyboard hook has been installed");
        }
예제 #6
0
        // ReSharper restore PrivateFieldCanBeConvertedToLocalVariable

        /// <summary>
        ///     Set up hooks
        /// </summary>
        static WinHook()
        {
            AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit;

            // Set up hooks
            EventProcDelegate    = WinEventProc;
            CbForegroundPtr      = Marshal.GetFunctionPointerForDelegate(EventProcDelegate);
            ForegroundWindowHook = User32.SetWinEventHook(User32.WindowsEventHookType.EVENT_SYSTEM_FOREGROUND,
                                                          User32.WindowsEventHookType.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, CbForegroundPtr, 0, 0,
                                                          User32.WindowsEventHookFlags.WINEVENT_OUTOFCONTEXT);

            MouseHookDelegate = MouseHookProc;
            CbMouseLlPtr      = Marshal.GetFunctionPointerForDelegate(MouseHookDelegate);
            MouseHook         = User32.SetWindowsHookEx(User32.WindowsHookType.WH_MOUSE_LL, CbMouseLlPtr,
                                                        Kernel32.GetModuleHandle(null).DangerousGetHandle(), 0);

            // Run standard event loop
            //EventLoop.Run();
        }
예제 #7
0
 public HookKeys()
 {
     windowsHook = HookCallback;//make that delegate not release by GC
 }