Beispiel #1
0
        private static int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            var handled = false;

            if (nCode >= 0)
            {
                var struct2 = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                if (Instance.SKeyDown != null && (wParam == 0x100 || wParam == 260))
                {
                    var e = new KeyEventArgs((Keys)struct2.VirtualKeyCode);
                    Instance.SKeyDown(null, e);
                    handled = e.Handled;
                }
                if (Instance.SKeyPress != null && wParam == 0x100)
                {
                    var flag2      = (Win32Interop.GetKeyState(0x10) & 0x80) == 0x80;
                    var flag3      = Win32Interop.GetKeyState(20) != 0;
                    var pbKeyState = new byte[0x100];
                    Win32Interop.GetKeyboardState(pbKeyState);
                    var lpwTransKey = new byte[2];
                    if (Win32Interop.ToAscii(struct2.VirtualKeyCode, struct2.ScanCode, pbKeyState, lpwTransKey,
                                             struct2.Flags) == 1)
                    {
                        var c = (char)lpwTransKey[0];
                        if (flag3 ^ flag2 && char.IsLetter(c))
                        {
                            c = char.ToUpper(c);
                        }
                        var args2 = new KeyPressEventArgs(c);
                        Instance.SKeyPress(null, args2);
                        handled = handled || args2.Handled;
                    }
                }
                if (Instance.SKeyUp != null && (wParam == 0x101 || wParam == 0x105))
                {
                    var args3 = new KeyEventArgs((Keys)struct2.VirtualKeyCode);
                    Instance.SKeyUp(null, args3);
                    handled = handled || args3.Handled;
                }
            }
            if (handled)
            {
                return(-1);
            }
            return(Win32Interop.CallNextHookEx(Instance._sKeyboardHookHandle, nCode, wParam, lParam));
        }