Esempio n. 1
0
        private static int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            bool flag = false;

            if (nCode >= 0)
            {
                HookManager.KeyboardHookStruct keyboardHookStruct = (HookManager.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(HookManager.KeyboardHookStruct));
                if (HookManager.s_KeyDown != null && (wParam == 256 || wParam == 260))
                {
                    Keys         virtualKeyCode = (Keys)keyboardHookStruct.VirtualKeyCode;
                    KeyEventArgs keyEventArgs   = new KeyEventArgs(virtualKeyCode);
                    HookManager.s_KeyDown(null, keyEventArgs);
                    flag = keyEventArgs.Handled;
                }
                if (HookManager.s_KeyPress != null && wParam == 256)
                {
                    bool   flag2    = (HookManager.GetKeyState(16) & 128) == 128;
                    bool   keyState = HookManager.GetKeyState(20) != 0;
                    byte[] array    = new byte[256];
                    HookManager.GetKeyboardState(array);
                    byte[] array2 = new byte[2];
                    if (HookManager.ToAscii(keyboardHookStruct.VirtualKeyCode, keyboardHookStruct.ScanCode, array, array2, keyboardHookStruct.Flags) == 1)
                    {
                        char c = (char)array2[0];
                        if ((keyState ^ flag2) && char.IsLetter(c))
                        {
                            c = char.ToUpper(c);
                        }
                        KeyPressEventArgs keyPressEventArgs = new KeyPressEventArgs(c);
                        HookManager.s_KeyPress(null, keyPressEventArgs);
                        flag = (flag || keyPressEventArgs.Handled);
                    }
                }
                if (HookManager.s_KeyUp != null && (wParam == 257 || wParam == 261))
                {
                    Keys         virtualKeyCode = (Keys)keyboardHookStruct.VirtualKeyCode;
                    KeyEventArgs keyEventArgs   = new KeyEventArgs(virtualKeyCode);
                    HookManager.s_KeyUp(null, keyEventArgs);
                    flag = (flag || keyEventArgs.Handled);
                }
            }
            int result;

            if (flag)
            {
                result = -1;
            }
            else
            {
                result = HookManager.CallNextHookEx(HookManager.s_KeyboardHookHandle, nCode, wParam, lParam);
            }
            return(result);
        }
        private static int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            bool flag1 = false;

            if (nCode >= 0)
            {
                HookManager.KeyboardHookStruct structure = (HookManager.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(HookManager.KeyboardHookStruct));
                if (HookManager.s_KeyDown != null && (wParam == 256 || wParam == 260))
                {
                    KeyEventArgs e = new KeyEventArgs((Keys)structure.VirtualKeyCode);
                    HookManager.s_KeyDown((object)null, e);
                    flag1 = e.Handled;
                }
                if (HookManager.s_KeyPress != null && wParam == 256)
                {
                    bool   flag2    = ((int)HookManager.GetKeyState(16) & 128) == 128;
                    bool   flag3    = (int)HookManager.GetKeyState(20) != 0;
                    byte[] numArray = new byte[256];
                    HookManager.GetKeyboardState(numArray);
                    byte[] lpwTransKey = new byte[2];
                    if (HookManager.ToAscii(structure.VirtualKeyCode, structure.ScanCode, numArray, lpwTransKey, structure.Flags) == 1)
                    {
                        char upper = (char)lpwTransKey[0];
                        if (flag3 ^ flag2 && char.IsLetter(upper))
                        {
                            upper = char.ToUpper(upper);
                        }
                        KeyPressEventArgs e = new KeyPressEventArgs(upper);
                        HookManager.s_KeyPress((object)null, e);
                        flag1 = flag1 || e.Handled;
                    }
                }
                if (HookManager.s_KeyUp != null && (wParam == 257 || wParam == 261))
                {
                    KeyEventArgs e = new KeyEventArgs((Keys)structure.VirtualKeyCode);
                    HookManager.s_KeyUp((object)null, e);
                    flag1 = flag1 || e.Handled;
                }
            }
            if (flag1)
            {
                return(-1);
            }
            return(HookManager.CallNextHookEx(HookManager.s_KeyboardHookHandle, nCode, wParam, lParam));
        }