Esempio n. 1
0
 private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
 {
     if (nCode >= 0 && this.OnMouseActivity != null)
     {
         UserActivityHook.MouseLLHookStruct mouseHookStruct = (UserActivityHook.MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(UserActivityHook.MouseLLHookStruct));
         MouseButtons button     = MouseButtons.None;
         short        mouseDelta = 0;
         int          num        = wParam;
         if (num == 513)
         {
             button = MouseButtons.Left;
         }
         else if (num == 516)
         {
             button = MouseButtons.Right;
         }
         else if (num == 522)
         {
             mouseDelta = (short)(mouseHookStruct.mouseData >> 16 & 65535);
         }
         int clickCount = 0;
         if (button != MouseButtons.None)
         {
             clickCount = (wParam == 515 || wParam == 518 ? 2 : 1);
         }
         MouseEventArgs e = new MouseEventArgs(button, clickCount, mouseHookStruct.pt.x, mouseHookStruct.pt.y, mouseDelta);
         this.OnMouseActivity(this, e);
     }
     return(UserActivityHook.CallNextHookEx(this.hMouseHook, nCode, wParam, lParam));
 }
Esempio n. 2
0
        private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            bool handled = false;

            if (nCode >= 0 && (this.KeyDown != null || this.KeyUp != null || this.KeyPress != null))
            {
                UserActivityHook.KeyboardHookStruct MyKeyboardHookStruct = (UserActivityHook.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(UserActivityHook.KeyboardHookStruct));
                if (this.KeyDown != null && (wParam == 256 || wParam == 260))
                {
                    KeyEventArgs e = new KeyEventArgs((Keys)MyKeyboardHookStruct.vkCode);
                    this.KeyDown(this, e);
                    handled = (handled ? true : e.Handled);
                }
                if (this.KeyPress != null && wParam == 256)
                {
                    bool   isDownShift    = ((UserActivityHook.GetKeyState(16) & 128) == 128 ? true : false);
                    bool   isDownCapslock = (UserActivityHook.GetKeyState(20) != 0 ? true : false);
                    byte[] keyState       = new byte[256];
                    UserActivityHook.GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];
                    if (UserActivityHook.ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == 1)
                    {
                        char key = (char)inBuffer[0];
                        if (isDownCapslock ^ isDownShift && char.IsLetter(key))
                        {
                            key = char.ToUpper(key);
                        }
                        KeyPressEventArgs e = new KeyPressEventArgs(key);
                        this.KeyPress(this, e);
                        handled = (handled ? true : e.Handled);
                    }
                }
                if (this.KeyUp != null && (wParam == 257 || wParam == 261))
                {
                    KeyEventArgs e = new KeyEventArgs((Keys)MyKeyboardHookStruct.vkCode);
                    this.KeyUp(this, e);
                    handled = (handled ? true : e.Handled);
                }
            }
            if (handled)
            {
                return(1);
            }
            return(UserActivityHook.CallNextHookEx(this.hKeyboardHook, nCode, wParam, lParam));
        }