예제 #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));
        }
예제 #2
0
        private static int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            var handled = false;

            if (nCode >= 0)
            {
                var myKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                if (SKeyDown != null && (wParam == WmKeydown || wParam == WmSyskeydown))
                {
                    var keyData = (Keys)myKeyboardHookStruct.VirtualKeyCode;
                    var e       = new KeyEventArgs(keyData);
                    SKeyDown.Invoke(null, e);
                    handled = e.Handled;
                }
            }
            if (handled)
            {
                return(-1);
            }
            return(CallNextHookEx(_sKeyboardHookHandle, nCode, wParam, lParam));
        }