Exemple #1
0
        private static int HookCallback(int code, IntPtr wParam, ref NativeHook.KBDLLHOOKSTRUCT lParam)
        {
            if (code < 0)
            {
                return(NativeHook.CallNextHookEx(IntPtr.Zero, code, wParam, ref lParam));
            }
            KeyboardHookEventArgs.EventType evtType;
            switch ((NativeHook.WmKeyUpDown)wParam.ToInt32())
            {
            case NativeHook.WmKeyUpDown.WM_KEYDOWN:
            case NativeHook.WmKeyUpDown.WM_SYSKEYDOWN:
                evtType = KeyboardHookEventArgs.EventType.KeyDown;
                break;

            case NativeHook.WmKeyUpDown.WM_KEYUP:
            case NativeHook.WmKeyUpDown.WM_SYSKEYUP:
                evtType = KeyboardHookEventArgs.EventType.KeyUp;
                break;

            default:
                Trace.TraceWarning($"Received unexpected WM_KEYXXXX ({wParam.ToInt32()}) in HookCallback");
                return(NativeHook.CallNextHookEx(IntPtr.Zero, code, wParam, ref lParam));
            }

            var evt = new KeyboardHookEventArgs(KeyInterop.KeyFromVirtualKey(lParam.vkCode), evtType);

            KeyboardHookCalled?.Invoke(null, evt);
            return(evt.Handled ? 1 : NativeHook.CallNextHookEx(IntPtr.Zero, code, wParam, ref lParam));
        }
Exemple #2
0
 public static void DisableKeyboardHook()
 {
     if (_currentHook != IntPtr.Zero)
     {
         NativeHook.UnhookWindowsHookEx(_currentHook);
         _currentHook = IntPtr.Zero;
     }
 }
Exemple #3
0
        public static void ActivateKeyboardHook()
        {
            if (_currentHook != IntPtr.Zero)
            {
                throw new Exception("LL Keyboard Hook already set.");
            }
#pragma warning disable CS0618 // Type or member is obsolete
            _currentHook = NativeHook.SetWindowsHookExA(NativeHook.HookType.WH_KEYBOARD_LL, _hookFunction, IntPtr.Zero, 0);
#pragma warning restore CS0618 // Type or member is obsolete
            if (_currentHook == IntPtr.Zero)
            {
                int res = Marshal.GetLastWin32Error();
                throw new Exception($"Error setting up LL Hook: {res}");
            }
        }
        private static void Main(string[] args)
        {
            // Managed hook example.
            MethodBase writeLineMethod   = "System.Console".GetMethod("WriteLine", new[] { typeof(string) }, BindingFlags.Static | BindingFlags.Public);
            MethodBase replacementMethod = "DotNetHook.Program".GetMethod("WriteLineReplacement", BindingFlags.Static | BindingFlags.NonPublic);

            _managedHook = new ManagedHook(writeLineMethod, replacementMethod);

            Console.WriteLine("Before Hook");

            // Apply the hook.
            _managedHook.Apply();

            // Call the method we hooked.
            Console.WriteLine("Modified Hello");


            // Remove the hook, alternatively you can use a "using" statement to dispose of the hook.
            _managedHook.Remove();

            Console.WriteLine("After Hook");

            Console.ReadLine();


            // Native hook example.
            MethodBase nativeReplacementMethod = "DotNetHook.Program".GetMethod("MessageBoxReplacement", BindingFlags.Static | BindingFlags.NonPublic);

            _nativeHook = new NativeHook(new NativeMethod("MessageBoxA", "user32.dll"), nativeReplacementMethod);

            MessageBoxA(IntPtr.Zero, "Before Hook", "The title!", 0);

            // Apply the hook.
            _nativeHook.Apply();

            // Call the method hooked.
            MessageBoxA(IntPtr.Zero, "Modified Hello", "The title!", 0);

            // Remove the hook, alternatively you can use a "using" statement to dispose of the hook.
            _nativeHook.Remove();

            MessageBoxA(IntPtr.Zero, "After Hook", "The title!", 0);
        }