Example #1
0
        private int HookProcedureCallback(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code >= 0)
            {
                MouseFlag      flag        = (MouseFlag)wParam;
                MSLLHOOKSTRUCT mouseStruct = Marshal.PtrToStructure <MSLLHOOKSTRUCT>(lParam);

                HookActivated?.Invoke(mouseStruct.point, flag);
            }
            return(CallNextHookEx(hookId, code, wParam, lParam));
        }
Example #2
0
        private int HookProcedureCallback(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code < 0)
            {
                return(CallNextHookEx(IntPtr.Zero, code, wParam, lParam));
            }

            KeyFlag         flag       = (KeyFlag)wParam;
            KBDLLHOOKSTRUCT hookStruct = Marshal.PtrToStructure <KBDLLHOOKSTRUCT>(lParam);

            uint             key = hookStruct.virtualKeyCode;
            bool             isExtendedKey;
            bool             isAltPressed;
            KeyStatus        keyStatus;
            KeyInjectionType injectionType;
            DateTime         timeStamp = new DateTime(hookStruct.time);

            uint bit1 = (hookStruct.flags & 0b1);
            uint bit2 = (hookStruct.flags & 0b10) >> 1;
            uint bit5 = (hookStruct.flags & 0b10000) >> 4;
            uint bit6 = (hookStruct.flags & 0b100000) >> 5;
            uint bit8 = (hookStruct.flags & 0b10000000) >> 7;

            isExtendedKey = (bit1 == 1);
            isAltPressed  = (bit6 == 1);
            keyStatus     = (bit8 == 1) ? KeyStatus.Released : KeyStatus.Pressed;

            if (bit2 == 1)
            {
                injectionType = KeyInjectionType.LowIntegrityInjected;
            }
            else if (bit5 == 1)
            {
                injectionType = KeyInjectionType.Injected;
            }
            else
            {
                injectionType = KeyInjectionType.NonInjected;
            }

            HookActivated?.Invoke(
                key: key,
                injectionType: injectionType,
                isExtendedKey: isExtendedKey,
                isAltPressed: isAltPressed,
                keyStatus: keyStatus
                );

            return(CallNextHookEx(IntPtr.Zero, code, wParam, lParam));
        }