Exemplo n.º 1
0
        private static int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            bool handled = false;

            if (nCode >= 0)
            {
                KeyboardHookStruct myKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                if (SKeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
                {
                    Keys         keyData = (Keys)myKeyboardHookStruct.VirtualKeyCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    SKeyDown.Invoke(null, e);
                    handled = e.Handled;
                }


                if (SKeyPress != null && wParam == WM_KEYDOWN)
                {
                    bool isDownShift    = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);
                    bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);

                    byte[] keyState = new byte[256];
                    GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];
                    if (ToAscii(myKeyboardHookStruct.VirtualKeyCode, 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);
                        SKeyPress.Invoke(null, e);
                        handled = handled || e.Handled;
                    }
                }


                if (SKeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
                {
                    Keys         keyData = (Keys)myKeyboardHookStruct.VirtualKeyCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    SKeyUp.Invoke(null, e);
                    handled = handled || e.Handled;
                }
            }

            if (handled)
            {
                return(-1);
            }

            return(CallNextHookEx(_sKeyboardHookHandle, nCode, wParam, lParam));
        }
Exemplo n.º 2
0
        private static int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            var handled = false;

            if (nCode >= 0)
            {
                var myKeyboardHookStruct =
                    (WindowsApi.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(WindowsApi.KeyboardHookStruct));


                if (SKeyPress != null && wParam == WindowsApi.WmKeydown)
                {
                    var isDownShift    = (WindowsApi.GetKeyState(WindowsApi.VkShift) & 0x80) == 0x80;
                    var isDownCapslock = WindowsApi.GetKeyState(WindowsApi.VkCapital) != 0;

                    var keyState = new byte[256];
                    WindowsApi.GetKeyboardState(keyState);
                    var inBuffer = new byte[2];
                    if (WindowsApi.ToAscii(myKeyboardHookStruct.VirtualKeyCode,
                                           myKeyboardHookStruct.ScanCode,
                                           keyState,
                                           inBuffer,
                                           myKeyboardHookStruct.Flags) == 1)
                    {
                        var key = (char)inBuffer[0];
                        if (isDownCapslock ^ isDownShift && char.IsLetter(key))
                        {
                            key = char.ToUpper(key);
                        }
                        var e = new KeyPressEventArgs(key);

                        try
                        {
                            SKeyPress.Invoke(null, e);
                        }
                        catch (Exception)
                        {
                            // ignored
                        }

                        handled = e.Handled;
                    }
                }
            }

            if (handled)
            {
                return(-1);
            }

            return(WindowsApi.CallNextHookEx(_sKeyboardHookHandle, nCode, wParam, lParam));
        }