Esempio n. 1
0
        private void GlobalKeyboardHook_KeyDown(object sender, KeyHookEventArgs e)
        {
            if (Interlocked.CompareExchange(ref this.m_stasis, 1, 0) == 1) return;

            this.Invoke(new Action(this.GlobalKeyboardHook_KeyDown));
        }
Esempio n. 2
0
        private IntPtr hookProc(int code, IntPtr wParam, ref NativeMethods.keyboardHookStruct lParam)
        {
            if (code >= 0)
            {
                var key = (Keys)lParam.vkCode;
                var wparam = wParam.ToInt64();
                bool d = (wparam == NativeMethods.WM_KEYDOWN || wparam == NativeMethods.WM_SYSKEYDOWN);
                bool u = (wparam == NativeMethods.WM_KEYUP   || wparam == NativeMethods.WM_SYSKEYUP);

                if (d ^ u)
                {
                    switch (key)
                    {
                        case Keys.Control:
                        case Keys.ControlKey:
                        case Keys.LControlKey:
                        case Keys.RControlKey:
                            this.m_control = d ? Keys.Control : Keys.None;
                            break;

                        case Keys.Shift:
                        case Keys.ShiftKey:
                        case Keys.LShiftKey:
                        case Keys.RShiftKey:
                            this.m_shift = d ? Keys.Shift : Keys.None;
                            break;

                        case Keys.Alt:
                        case Keys.Menu:
                        case Keys.RMenu:
                        case Keys.LMenu:
                            this.m_alt = d ? Keys.Alt : Keys.None;
                            break;
                    }
                }

                key = key | this.m_control | this.m_shift | this.m_alt;

                if (this.KeyUp != null && this.Up.Contains(key))
                {
                    var arg = new KeyHookEventArgs(key);
                    KeyUp.Invoke(this, arg);
                    if (arg.Handled)
                        return new IntPtr(1);
                }

                if (this.KeyDown != null && this.Down.Contains(key))
                {
                    var arg = new KeyHookEventArgs(key);
                    KeyDown.Invoke(this, arg);
                    if (arg.Handled)
                        return new IntPtr(1);
                }
            }
            return NativeMethods.CallNextHookEx(m_hwnd, code, wParam, ref lParam);
        }