Esempio n. 1
0
        public NativeKeyWatcher()
            : base()
        {
            IntPtr KeyboardProcessor(int nCode, IntPtr wParam, IntPtr lParam)
            {
                if (nCode >= 0 && wParam == (IntPtr)Native.WM_KEYDOWN)
                {
                    var keyCode = Marshal.ReadInt32(lParam);
                    this.HandleKey(keyCode);
                }

                return(Native.CallNextHookEx(this.hookId, nCode, wParam, lParam));
            }

            IntPtr SetHook()
            {
                this.keyboardProc = new Native.LowLevelKeyboardProc(KeyboardProcessor);

                using (var process = Process.GetCurrentProcess())
                {
                    using (var module = process.MainModule)
                    {
                        return(Native.SetWindowsHookEx(Native.WH_KEYBOARD_LL,
                                                       this.keyboardProc,
                                                       Native.GetModuleHandle(module.ModuleName), 0));
                    }
                }
            }

            this.hookId = SetHook();
        }
Esempio n. 2
0
 protected virtual void Dispose(bool disposing)
 {
     if (!this.isDisposed)
     {
         Native.UnhookWindowsHookEx(this.hookId);
         this.keyboardProc = null;
         this.isDisposed   = true;
     }
 }
Esempio n. 3
0
 static KeybordCapture()
 {
     s_hook = Native.SetWindowsHookEx(Native.WH_KEYBOARD_LL,
         s_proc = new Native.LowLevelKeyboardProc(LowLevelKeyboardProc),
         System.Runtime.InteropServices.Marshal.GetHINSTANCE(typeof(KeybordCapture).Module),
         //Native.GetModuleHandle(null),
         0);
     AppDomain.CurrentDomain.DomainUnload += delegate
     {
         if(s_hook != IntPtr.Zero)
             Native.UnhookWindowsHookEx(s_hook);
     };
 }
Esempio n. 4
0
 /// <summary>
 /// Unregisters the keyboard hook.
 /// </summary>
 public void Unregister()
 {
     try
     {
         Logger.Write("KManager.Unregister(): Unregistering keyboard hook using id: " + _hookID);
         Native.UnhookWindowsHookEx(_hookID);
         _proc   = null;
         _hookID = IntPtr.Zero;
         Logger.Write("KManager.Unregister(): Unregistered keyboard hook. ");
     }
     catch (Exception ex)
     {
         Logger.Write("Error. " + ex.Message, MessagePriority.High, MessageKind.Error);
     }
 }
Esempio n. 5
0
 static KeybordCapture()
 {
     s_hook = Native.SetWindowsHookEx(Native.WH_KEYBOARD_LL,
                                      s_proc = new Native.LowLevelKeyboardProc(LowLevelKeyboardProc),
                                      System.Runtime.InteropServices.Marshal.GetHINSTANCE(typeof(KeybordCapture).Module),
                                      //Native.GetModuleHandle(null),
                                      0);
     AppDomain.CurrentDomain.DomainUnload += delegate
     {
         if (s_hook != IntPtr.Zero)
         {
             Native.UnhookWindowsHookEx(s_hook);
         }
     };
 }
Esempio n. 6
0
 /// <summary>
 /// Registers the keyboard hook with low-level keyboard procedure.
 /// </summary>
 public void Register()
 {
     try
     {
         Logger.Write("KManager.Register(): Registering hook...", MessagePriority.Low, MessageKind.Info);
         if (_proc != null || _hookID != IntPtr.Zero)
         {
             Native.UnhookWindowsHookEx(_hookID);
             _proc   = null;
             _hookID = IntPtr.Zero;
         }
         _proc   = HookCallback;
         _hookID = Native.SetHook(_proc);
         Logger.Log("KManager.Register(): Registered keyboard hook with hook id: " + _hookID);
         Logger.Notify("The application can now intercept or forward keyboard input. ", MessageKind.Info);
     }
     catch (Exception ex)
     {
         Logger.Write(ex.Message, MessagePriority.High, MessageKind.Error);
     }
 }
Esempio n. 7
0
 internal static extern IntPtr SetWindowsHookEx(
     int idHook, Native.LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);