Beispiel #1
0
        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode == HC_ACTION)
            {
                int messageID = (int)wParam;
                if ((messageID == WM_KEYDOWN) || (messageID == WM_KEYUP) ||
                    (messageID == WM_SYSKEYDOWN) || (messageID == WM_SYSKEYUP))
                {
                    if (KeyPressed != null)
                    {
                        KBDLLHookStruct hookData       = Marshal.PtrToStructure <KBDLLHookStruct>(lParam);
                        bool            keyIsDown      = ((messageID == WM_KEYDOWN) || (messageID == WM_SYSKEYDOWN));
                        bool            keyIsSystemKey = ((messageID == WM_SYSKEYDOWN) || (messageID == WM_SYSKEYUP));

                        KeyPressedEventArgs e = new KeyPressedEventArgs(hookData.vkCode, keyIsDown, keyIsSystemKey);
                        KeyPressed(this, e);
                        if (e.TrapKey)
                        {
                            return((IntPtr)1);
                        }
                    }
                }
            }
            return(CallNextHookEx(HookID, nCode, wParam, lParam));
        }
Beispiel #2
0
        //The listener that will trigger events
        private int KeybHookProc(int Code, int W, int L)
        {
            KBDLLHookStruct LS = new KBDLLHookStruct();
            if (Code < 0)
            {
                return CallNextHookEx(HookID, Code, W, L);
            }
            try
            {
                if (!Global)
                {
                    if (Code == 3)
                    {
                        IntPtr ptr = IntPtr.Zero;

                        int keydownup = L >> 30;
                        if (keydownup == 0)
                        {
                            if (KeyDown != null) KeyDown((Keys)W, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                        }
                        if (keydownup == -1)
                        {
                            if (KeyUp != null) KeyUp((Keys)W, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                        }
                        //System.Diagnostics.Debug.WriteLine("Down: " + (Keys)W);
                    }
                }
                else
                {
                    KeyEvents kEvent = (KeyEvents)W;

                    Int32 vkCode = Marshal.ReadInt32((IntPtr)L); //Leser vkCode som er de første 32 bits hvor L peker.

                    if (kEvent != KeyEvents.KeyDown && kEvent != KeyEvents.KeyUp && kEvent != KeyEvents.SKeyDown && kEvent != KeyEvents.SKeyUp)
                    {
                    }
                    if (kEvent == KeyEvents.KeyDown || kEvent == KeyEvents.SKeyDown)
                    {
                        if (KeyDown != null) KeyDown((Keys)vkCode, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                    }
                    if (kEvent == KeyEvents.KeyUp || kEvent == KeyEvents.SKeyUp)
                    {
                        if (KeyUp != null) KeyUp((Keys)vkCode, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                    }
                }
            }
            catch (Exception)
            {
                //Ignore all errors...
            }

            return CallNextHookEx(HookID, Code, W, L);

        }
Beispiel #3
0
    private static int LowLevelHookProc(int code, IntPtr wParam, IntPtr lParam) // lParam is KBDLLHookStruct and wParam is a keyState
    {
        if (code >= 0)
        {
            var kb       = KBDLLHookStruct.CreateStruct(lParam);
            var keyState = (KeyState)wParam;
            var key      = (Key)kb.vkCode;

            if (keyState == KeyState.KeyDown || keyState == KeyState.SysKeyDown)
            {
                OnKeyDown(key);
            }
            else
            {
                OnKeyUp(key);
            }
        }
        return(WindowsAPI.CallNextHookEx(hookId, code, wParam, lParam));
    }
        //The listener that will trigger events
        private int KeybHookProc(int Code, int W, int L)
        {
            KBDLLHookStruct LS = new KBDLLHookStruct();

            if (Code < 0)
            {
                return(CallNextHookEx(HookID, Code, W, L));
            }
            try
            {
                KeyEvents kEvent = (KeyEvents)W;

                Int32 vkCode = Marshal.ReadInt32((IntPtr)L); //Leser vkCode som er de første 32 bits hvor L peker.

                if (kEvent != KeyEvents.KeyDown && kEvent != KeyEvents.KeyUp && kEvent != KeyEvents.SKeyDown && kEvent != KeyEvents.SKeyUp)
                {
                }
                if (kEvent == KeyEvents.KeyDown || kEvent == KeyEvents.SKeyDown)
                {
                    if (KeyDown != null)
                    {
                        KeyDown((Keys)vkCode, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                    }
                }
                if (kEvent == KeyEvents.KeyUp || kEvent == KeyEvents.SKeyUp)
                {
                    if (KeyUp != null)
                    {
                        KeyUp((Keys)vkCode, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            return(CallNextHookEx(HookID, Code, W, L));
        }
Beispiel #5
0
        private int KeybHookProc(int Code, int W, int L)
        {
            KBDLLHookStruct LS = new KBDLLHookStruct();

            if (Code < 0)
            {
                return(CallNextHookEx(HookID, Code, W, L));
            }
            try
            {
                if (!Global)
                {
                    if (Code == 3)
                    {
                        IntPtr ptr = IntPtr.Zero;

                        int keydownup = L >> 30;
                        if (keydownup == 0)
                        {
                            if (KeyDown != null)
                            {
                                KeyDown((Keys)W, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                            }
                        }
                        if (keydownup == -1)
                        {
                            if (KeyUp != null)
                            {
                                KeyUp((Keys)W, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                            }
                        }
                    }
                }
                else
                {
                    KeyEvents kEvent = (KeyEvents)W;

                    Int32 vkCode = Marshal.ReadInt32((IntPtr)L);

                    if (kEvent != KeyEvents.KeyDown && kEvent != KeyEvents.KeyUp && kEvent != KeyEvents.SKeyDown && kEvent != KeyEvents.SKeyUp)
                    {
                    }
                    if (kEvent == KeyEvents.KeyDown || kEvent == KeyEvents.SKeyDown)
                    {
                        if (KeyDown != null)
                        {
                            KeyDown((Keys)vkCode, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                        }
                    }
                    if (kEvent == KeyEvents.KeyUp || kEvent == KeyEvents.SKeyUp)
                    {
                        if (KeyUp != null)
                        {
                            KeyUp((Keys)vkCode, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                        }
                    }
                }
            }
            catch (Exception)
            {
            }

            return(CallNextHookEx(HookID, Code, W, L));
        }
        //The listener that will trigger events
        private int KeybHookProc(int Code, int W, int L)
        {
            KBDLLHookStruct LS = new KBDLLHookStruct();
            if (Code < 0)
            {
                return CallNextHookEx(HookID, Code, W, L);
            }
            try
            {
                if (!Global)
                {
                    if (Code == 3)
                    {
                        IntPtr ptr = IntPtr.Zero;

                        int keydownup = L >> 30;
                        if (keydownup == 0)
                        {
                            if (KeyDown != null) KeyDown((Keys)W, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                        }
                        if (keydownup == -1)
                        {
                            if (KeyUp != null) KeyUp((Keys)W, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                        }
                        //System.Diagnostics.Debug.WriteLine("Down: " + (Keys)W);
                    }
                }
                else
                {
                    KeyEvents kEvent = (KeyEvents)W;

                    Int32 vkCode = Marshal.ReadInt32((IntPtr)L); //Leser vkCode som er de første 32 bits hvor L peker.

                    if (kEvent != KeyEvents.KeyDown && kEvent != KeyEvents.KeyUp && kEvent != KeyEvents.SKeyDown && kEvent != KeyEvents.SKeyUp)
                    {
                    }
                    if (kEvent == KeyEvents.KeyDown || kEvent == KeyEvents.SKeyDown)
                    {
                        if (KeyDown != null) KeyDown((Keys)vkCode, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                    }
                    if (kEvent == KeyEvents.KeyUp || kEvent == KeyEvents.SKeyUp)
                    {
                        if (KeyUp != null) KeyUp((Keys)vkCode, GetShiftPressed(), GetCtrlPressed(), GetAltPressed());
                    }
                }
            }
            catch (Exception)
            {
                //Ignore all errors...
            }

            return CallNextHookEx(HookID, Code, W, L);

        }
Beispiel #7
0
 public HookKeyEventArgs(KeyEvents Event, KBDLLHookStruct data, KeyboardUtilConstants.VirtualKeyCode vkCode)
 {
     this.Event   = Event;
     this.rawData = data;
     this.vkCode  = vkCode;
 }