Beispiel #1
0
        public override bool Dispatch(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code != 0)
            {
                return(true);
            }

            var ev = (WM)wParam.ToUInt32();

            try
            {
                var str = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

                var state = User32.GetAsyncKeyState(str.vkCode);

                var keyInfo = new KeyEventInfo(ev, str, this, (state & 0x8000) != 0);

                StoreSpecialKeyState(ev, keyInfo);

                foreach (var Handler in Handlers)
                {
                    if (!Handler(keyInfo))
                    {
                        return(false);
                    }
                }
            }
            catch (Exception)
            {
            }

            return(true);
        }
Beispiel #2
0
        private void StoreSpecialKeyState(WM Event, KeyEventInfo info)
        {
            var       toggle = Event == WM.KEYDOWN || Event == WM.SYSKEYDOWN;
            Modifiers Flag   = Modifiers.None;

            switch (info.VirtualKey)
            {
            case Keys.LMenu: Flag = Modifiers.LAlt; break;

            case Keys.RMenu: Flag = Modifiers.RAlt; break;

            case Keys.LControlKey: Flag = Modifiers.LCtrl; break;

            case Keys.RControlKey: Flag = Modifiers.RCtrl; break;

            case Keys.LShiftKey: Flag = Modifiers.LShift; break;

            case Keys.RShiftKey: Flag = Modifiers.RShift; break;

            default: return;
            }

            if (toggle)
            {
                ModifiersState |= Flag;
            }
            else
            {
                ModifiersState &= ~Flag;
            }
        }