Example #1
0
    private int LowLevelHook(int code, int wParam, ref KeyboardLowLevelHookStruct lParam)
    {
        if (code == Hc_Action)
        {
            if ((wParam == WindowsMessage.KeyDown) || (wParam == WindowsMessage.SystemKeyDown) || (wParam == WindowsMessage.KeyUp) || (wParam == WindowsMessage.SystemKeyUp))
            {
                //Dim alt As Boolean = (GetAsyncKeyState(Vk.Alt) And &H8000) = &H8000
                //Dim shift As Boolean = (GetAsyncKeyState(Vk.Shift) And &H8000) = &H8000
                bool control = (GetAsyncKeyState(Vk.Control) & 0x8000) == 0x8000;

                bool suppress = false;

                // CTRL+ESC
                if (control && lParam.VirtualKeyCode == Vk.Escape)
                {
                    suppress = true;
                }

                // ALT+TAB
                if ((lParam.Flags & LowLevelKeyboardHfAltDown) == LowLevelKeyboardHfAltDown && lParam.VirtualKeyCode == Vk.Tab)
                {
                    suppress = true;
                }

                // ALT+ESC
                if ((lParam.Flags & LowLevelKeyboardHfAltDown) == LowLevelKeyboardHfAltDown && lParam.VirtualKeyCode == Vk.Escape)
                {
                    suppress = true;
                }

                // Left Windows button.
                if (lParam.VirtualKeyCode == Vk.LeftWindows)
                {
                    suppress = true;
                    MessageBox.Show("Pressed Left windows key");
                }

                // Right Windows button.
                if (lParam.VirtualKeyCode == Vk.RightWindows)
                {
                    suppress = true;
                    MessageBox.Show("Pressed Right windows key");
                }

                // Applications button.
                if (lParam.VirtualKeyCode == Vk.Applications)
                {
                    suppress = true;
                }

                if (suppress)
                {
                    return(1);
                }
            }


            return(CallNextHookEx(m_hookHandle, code, wParam, lParam));
        }
    }
        bool processKeyUp(KeyboardLowLevelHookStruct data)
        {
            data.scanCode &= 0xFF;
            if ((data.flags & 0x01) == 0x01)
            {
                data.scanCode |= 0x100;
            }
            KeyEventArgsExt    e      = new KeyEventArgsExt((System.Windows.Forms.Keys)data.vkCode, data.scanCode);
            KeyEventHandlerExt eKeyUp = this.GlobalKeyUp;

            if (eKeyUp != null)
            {
                eKeyUp(this, e);
            }
            return(e.Handled);
        }
Example #3
0
        /// <summary>
        /// Handler to run on keychange.
        /// </summary>
        /// <param name="nCode">Event code.</param>
        /// <param name="wParam">Type of windows message.</param>
        /// <param name="lParam">Keyboard status struct.</param>
        /// <returns>Pointer to next callback.</returns>
        private IntPtr LowLevelKeyboardHook(int nCode, WindowsMessages wParam, IntPtr lParam)
        {
            bool isKeyDown = wParam == WindowsMessages.KEYDOWN || wParam == WindowsMessages.SYSKEYDOWN;
            bool isKeyUp   = wParam == WindowsMessages.KEYUP || wParam == WindowsMessages.SYSKEYUP;
            bool isAlt     = false;

            KeyboardLowLevelHookStruct kbd = (KeyboardLowLevelHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardLowLevelHookStruct));

            if (kbd.flags.HasFlag(KeyboardLowLevelHookStructFlags.LLKHF_ALTDOWN))
            {
                isAlt = true;
            }

            if (nCode >= 0 && FromAllowedWindow())
            {
                if (isKeyDown)
                {
                    LastMessageReceived = DateTime.Now;

                    RaiseEvent(new KeyboardChangeEventArgs()
                    {
                        Direction = KeyChangeDirection.KeyDown,
                        Key       = (Keys)kbd.vkCode,
                        Alt       = isAlt,
                    });
                }
                else if (isKeyUp)
                {
                    LastMessageReceived = DateTime.Now;

                    RaiseEvent(new KeyboardChangeEventArgs()
                    {
                        Direction = KeyChangeDirection.KeyUp,
                        Key       = (Keys)kbd.vkCode,
                        Alt       = isAlt,
                    });
                }
            }

            return(WinApi.User32.Api.CallNextHookEx(IntPtr.Zero, nCode, (IntPtr)wParam, lParam));
        }
 int keyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
 {
     if (nCode == 0)
     {
         KeyboardHookMessages       message = (KeyboardHookMessages)wParam;
         KeyboardLowLevelHookStruct data    = (KeyboardLowLevelHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardLowLevelHookStruct));
         bool handled = false;
         if (message == KeyboardHookMessages.KeyDown || message == KeyboardHookMessages.SysKeyDown)
         {
             handled = processKeyDown(data);
         }
         else if (message == KeyboardHookMessages.KeyUp || message == KeyboardHookMessages.SysKeyUp)
         {
             handled = processKeyUp(data);
         }
         if (handled)
         {
             return(-1);
         }
     }
     return(CallNextHookEx(this.myHhk, nCode, wParam, lParam));
 }
Example #5
0
 public static extern int CallNextHookEx(IntPtr idHook, int nCode,
                                         IntPtr wParam, ref KeyboardLowLevelHookStruct lParam);
Example #6
0
 private static extern int CallNextHookEx(int handle, int code, int wParam, KeyboardLowLevelHookStruct lParam);
Example #7
0
        private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            bool handled = false;

            if ((nCode >= 0) && (KeyDownEvent != null || KeyUpEvent != null || KeyPressEvent != null))
            {
                KeyboardLowLevelHookStruct keyboardLowLevelHookStruct = new KeyboardLowLevelHookStruct();
                Marshal.PtrToStructure(lParam, keyboardLowLevelHookStruct);

                bool isDownControl  = ((GetKeyState(VK_LCONTROL) & 0x80) != 0) || ((GetKeyState(VK_RCONTROL) & 0x80) != 0);
                bool isDownShift    = ((GetKeyState(VK_LSHIFT) & 0x80) != 0) || ((GetKeyState(VK_RSHIFT) & 0x80) != 0);
                bool isDownAlt      = ((GetKeyState(VK_LALT) & 0x80) != 0) || ((GetKeyState(VK_RALT) & 0x80) != 0);
                bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0);

                KeyEventArgs e = new KeyEventArgs((Keys)(keyboardLowLevelHookStruct.vkCode |
                                                         (isDownControl ? (int)Keys.Control : 0) |
                                                         (isDownShift ? (int)Keys.Shift : 0) |
                                                         (isDownAlt ? (int)Keys.Alt : 0)));

                if ((KeyDownEvent != null) && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
                {
                    KeyDownEvent(this, e);
                    handled = e.Handled;
                }
                else if ((KeyUpEvent != null) && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
                {
                    KeyUpEvent(this, e);
                    handled = e.Handled;
                }
                if ((KeyPressEvent != null) && (wParam == WM_KEYDOWN) && !handled && !e.SuppressKeyPress)
                {
                    byte[] keyState = new byte[256];
                    GetKeyboardState(keyState);

                    byte[] inBuffer = new byte[2];
                    if (ToAscii(keyboardLowLevelHookStruct.vkCode,
                                keyboardLowLevelHookStruct.scanCode,
                                keyState,
                                inBuffer,
                                keyboardLowLevelHookStruct.flags) == 1)
                    {
                        char key = (char)inBuffer[0];
                        if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key))
                        {
                            key = Char.ToUpperInvariant(key);
                        }
                        KeyPressEventArgs kea = new KeyPressEventArgs(key);
                        KeyPressEvent(this, kea);
                        handled = kea.Handled;
                    }
                }
            }

            if (handled)
            {
                return(1);
            }
            else
            {
                return(CallNextHookEx(hKeyboardHookId, nCode, wParam, lParam));
            }
        }