Exemplo n.º 1
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (keyBoardHandle != IntPtr.Zero)
                {
                    WindowsHookHelper.UnhookWindowsHookEx(
                        keyBoardHandle);
                }

                disposed = true;
            }
        }
Exemplo n.º 2
0
 public void RegisterHook()
 {
     using (Process process = Process.GetCurrentProcess())
         using (ProcessModule module = process.MainModule)
         {
             IntPtr hModule = GetModuleHandle(module.ModuleName);
             messageLoopControl.BeginInvoke(new Action(() =>
             {
                 try
                 {
                     if (keyBoardHandle != IntPtr.Zero)
                     {
                         WindowsHookHelper.UnhookWindowsHookEx(keyBoardHandle);
                     }
                 }
                 catch { }
                 keyBoardHandle = WindowsHookHelper.SetWindowsHookEx(
                     WH_KEYBOARD_LL, keyBoardDelegate, hModule, 0);
             }));
         }
 }
Exemplo n.º 3
0
        private IntPtr KeyboardHookDelegate(
            Int32 Code, IntPtr wParam, IntPtr lParam)
        {
            if (Code < 0)
            {
                return(WindowsHookHelper.CallNextHookEx(
                           keyBoardHandle, Code, wParam, lParam));
            }

            var hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

            if (wParam == (IntPtr)0x0100 || wParam == (IntPtr)0x0104) //KeyDown
            {
                Keys key = (Keys)hookStruct.vkCode;

                if (key == Keys.LControlKey || key == Keys.RControlKey)
                {
                    key = Keys.ControlKey;
                    if (modifiers.HasFlag(Keys.Control))
                    {
                        return(WindowsHookHelper.CallNextHookEx(
                                   keyBoardHandle, Code, wParam, lParam));
                    }
                }
                else if (key == Keys.LMenu || key == Keys.RMenu)
                {
                    key = Keys.Menu;
                    if (modifiers.HasFlag(Keys.Alt))
                    {
                        return(WindowsHookHelper.CallNextHookEx(
                                   keyBoardHandle, Code, wParam, lParam));
                    }
                }
                else if (key == Keys.LShiftKey || key == Keys.RShiftKey)
                {
                    key = Keys.ShiftKey;
                    if (modifiers.HasFlag(Keys.Shift))
                    {
                        return(WindowsHookHelper.CallNextHookEx(
                                   keyBoardHandle, Code, wParam, lParam));
                    }
                }

                if (KeyBoardKeyPressed != null)
                {
                    KeyBoardKeyPressed(this, new KeyEventArgs(key | modifiers));
                }

                if (key == Keys.ControlKey)
                {
                    modifiers |= Keys.Control;
                }
                else if (key == Keys.Menu)
                {
                    modifiers |= Keys.Alt;
                }
                else if (key == Keys.ShiftKey)
                {
                    modifiers |= Keys.Shift;
                }
                else if (key == Keys.Delete && modifiers.HasFlag(Keys.Control) && modifiers.HasFlag(Keys.Alt))
                {
                    modifiers &= ~Keys.Control;
                    modifiers &= ~Keys.Alt;
                }
            }
            else if (wParam == (IntPtr)0x0101 || wParam == (IntPtr)0x0105) //KeyUp
            {
                Keys key = (Keys)hookStruct.vkCode;

                if (key == Keys.LControlKey || key == Keys.RControlKey)
                {
                    modifiers &= ~Keys.Control;
                }
                else if (key == Keys.LMenu || key == Keys.RMenu)
                {
                    modifiers &= ~Keys.Alt;
                }
                else if (key == Keys.LShiftKey || key == Keys.RShiftKey)
                {
                    modifiers &= ~Keys.Shift;
                }
            }

            return(WindowsHookHelper.CallNextHookEx(
                       keyBoardHandle, Code, wParam, lParam));
        }