예제 #1
0
 public void InstallHook(Form window)
 {
     if (this.State == HookState.Uninstalled)
     {
         if (window != null)
         {
             NativeExports.SetLastError(0);
             IntPtr nextWindow = NativeExports.SetClipboardViewer(window.Handle);
             if (nextWindow == IntPtr.Zero)
             {
                 var errorCode = NativeExports.GetLastError();
                 if (errorCode == 0)
                 {
                     OnSuccessfullHook(window.Handle, nextWindow);
                 }
                 else
                 {
                     var message = ErrorCodeHelper.GetMessage(errorCode);
                     throw new Exception(message);
                 }
             }
             else
             {
                 OnSuccessfullHook(window.Handle, nextWindow);
             }
         }
     }
 }
예제 #2
0
 private void SafeRemove()
 {
     if (this.handle != IntPtr.Zero)
     {
         NativeExports.UnhookWindowsHookEx(this.handle);
         this.OnSuccessfullUnhook();
     }
 }
예제 #3
0
 private void SafeRemove()
 {
     if (this.State == HookState.Installed)
     {
         NativeExports.ChangeClipboardChain(this.clipboard.Handle, this.clipboard.NextWindow);
         this.OnSuccessfullUnhook();
     }
 }
예제 #4
0
 public void RemoveHook()
 {
     if (this.State == HookState.Installed)
     {
         if (NativeExports.UnhookWindowsHookEx(this.handle))
         {
             this.OnSuccessfullUnhook();
         }
         else
         {
             var errorCode = NativeExports.GetLastError();
             throw new Exception(ErrorCodeHelper.GetMessage(errorCode));
         }
     }
 }
예제 #5
0
 public void InstallHook(Form window)
 {
     if (this.State == HookState.Uninstalled)
     {
         this.keyboardProcessor = new KeyboardMessageEventHandler(KeyboardProc);
         this.handle            = NativeExports.SetWindowsHookEx(NativeConstants.WH_KEYBOARD_LL, this.keyboardProcessor, IntPtr.Zero, 0);
         if (this.handle == IntPtr.Zero)
         {
             this.keyboardProcessor = null;
             var errorCode = NativeExports.GetLastError();
             throw new Exception(ErrorCodeHelper.GetMessage(errorCode));
         }
         else
         {
             this.OnStateChanged(new StateChangedEventArgs(this.State));
         }
     }
 }
예제 #6
0
 public void RemoveHook()
 {
     if (this.State == HookState.Installed)
     {
         NativeExports.SetLastError(0);
         NativeExports.ChangeClipboardChain(this.clipboard.Handle, this.clipboard.NextWindow);
         var errorCode = NativeExports.GetLastError();
         if (errorCode == 0)
         {
             this.OnSuccessfullUnhook();
         }
         else
         {
             var message = ErrorCodeHelper.GetMessage(errorCode);
             throw new Exception(message);
         }
     }
 }
예제 #7
0
            protected override void WndProc(ref Message m)
            {
                switch (m.Msg)
                {
                case NativeConstants.WM_DRAWCLIPBOARD:
                {
                    if (ClipboardChanged != null)
                    {
                        ClipboardChanged(this, new ClipboardEventArgs(m.WParam));
                    }

                    NativeExports.SendMessage(this.nextWindow, (uint)m.Msg, m.WParam, m.LParam);
                }
                break;

                case NativeConstants.WM_CHANGECBCHAIN:
                {
                    if (m.WParam == this.nextWindow)
                    {
                        // The window is being removed is the next window on the clipboard chain.
                        // Change the ClipboardHook._nextWind handle with LParam.
                        // There is no need to pass this massage any farther.
                        this.nextWindow = m.LParam;
                    }
                    else
                    {
                        NativeExports.SendMessage(this.nextWindow, (uint)m.Msg, m.WParam, m.LParam);
                    }
                }
                break;

                default:
                {
                    if (m.Msg == NativeConstants.WM_DESTROY && WindowClosing != null)
                    {
                        WindowClosing(this, EventArgs.Empty);
                    }

                    base.WndProc(ref m);
                }
                break;
                }
            }
예제 #8
0
        private IntPtr KeyboardProc(int nCode, IntPtr wParam, ref KeyboardData lParam)
        {
            if (nCode >= NativeConstants.HC_ACTION)
            {
                KeyboardEventArgs e;
                var vkCode = (Keys)lParam.vkCode;
                if ((int)wParam == NativeConstants.WM_KEYDOWN | (int)wParam == NativeConstants.WM_SYSKEYDOWN)
                {
                    if (vkCode == Keys.LMenu | vkCode == Keys.RMenu)
                    {
                        this.keyData = (this.keyData | Keys.Alt);
                        e            = new KeyboardEventArgs(this.keyData | Keys.Menu, vkCode);
                    }
                    else if (vkCode == Keys.LControlKey | vkCode == Keys.RControlKey)
                    {
                        this.keyData = (this.keyData | Keys.Control);
                        e            = new KeyboardEventArgs(this.keyData | Keys.ControlKey, vkCode);
                    }
                    else if (vkCode == Keys.LShiftKey | vkCode == Keys.RShiftKey)
                    {
                        this.keyData = (this.keyData | Keys.Shift);
                        e            = new KeyboardEventArgs(this.keyData | Keys.ShiftKey, vkCode);
                    }
                    else
                    {
                        e = new KeyboardEventArgs(this.keyData | vkCode, vkCode);
                    }

                    this.OnKeyDown(e);
                    if (e.Handled)
                    {
                        return(new IntPtr(1));
                    }
                }
                else if ((int)wParam == NativeConstants.WM_KEYUP | (int)wParam == NativeConstants.WM_SYSKEYUP)
                {
                    if (vkCode == Keys.LMenu | vkCode == Keys.RMenu)
                    {
                        this.keyData = (this.keyData & ~Keys.Alt);
                        e            = new KeyboardEventArgs(this.keyData | Keys.Menu, vkCode);
                    }
                    else if (vkCode == Keys.LControlKey | vkCode == Keys.RControlKey)
                    {
                        this.keyData = (this.keyData & ~Keys.Control);
                        e            = new KeyboardEventArgs(this.keyData | Keys.ControlKey, vkCode);
                    }
                    else if (vkCode == Keys.LShiftKey | vkCode == Keys.RShiftKey)
                    {
                        this.keyData = (this.keyData & ~Keys.Shift);
                        e            = new KeyboardEventArgs(this.keyData | Keys.ShiftKey, vkCode);
                    }
                    else
                    {
                        e = new KeyboardEventArgs(this.keyData | vkCode, vkCode);
                    }

                    this.OnKeyUp(e);
                    if (e.Handled)
                    {
                        return(new IntPtr(1));
                    }
                }
            }

            return(NativeExports.CallNextHookEx(this.handle, nCode, wParam, ref lParam));
        }