/// <summary>
        /// The callback for the keyboard hook
        /// </summary>
        /// <param name="code">The hook code, if it isn't >= 0, the function shouldn't do anyting</param>
        /// <param name="wParam">The event type</param>
        /// <param name="lParam">The keyhook event information</param>
        /// <returns></returns>
        private IntPtr hookProc(int code, IntPtr wParam, ref NativeMethods.keyboardHookStruct lParam)
        {
            if (code >= 0)
            {
                Key key = KeyInterop.KeyFromVirtualKey(lParam.vkCode);
                if (HookedKeys.Contains(key))
                {
                    var wparam = wParam.ToInt64();

                    var args = new KeyHookEventArgs(key);

                    if ((wparam == NativeMethods.WM_KEYDOWN || wparam == NativeMethods.WM_SYSKEYDOWN) && (KeyDown != null))
                    {
                        KeyDown.Invoke(this, args);
                    }
                    else if ((wparam == NativeMethods.WM_KEYUP || wparam == NativeMethods.WM_SYSKEYUP) && (KeyUp != null))
                    {
                        KeyUp.Invoke(this, args);
                    }

                    if (args.Handled)
                    {
                        return(new IntPtr(1));
                    }
                }
            }
            return(NativeMethods.CallNextHookEx(hhook, code, wParam, ref lParam));
        }
        protected virtual bool RaiseKeyUp(int virtualKeyCode)
        {
            var e = new KeyEventArgs((Keys)virtualKeyCode);

            KeyUp?.Invoke(this, e);
            return(e.Handled);
        }
Exemple #3
0
        public FileViewerInternal()
        {
            InitializeComponent();
            InitializeComplete();

            _currentViewPositionCache = new CurrentViewPositionCache(this);

            TextEditor.TextChanged += (s, e) => TextChanged?.Invoke(s, e);
            TextEditor.ActiveTextAreaControl.VScrollBar.ValueChanged += (s, e) => ScrollPosChanged?.Invoke(s, e);
            TextEditor.ActiveTextAreaControl.TextArea.KeyUp          += (s, e) => KeyUp?.Invoke(s, e);
            TextEditor.ActiveTextAreaControl.TextArea.DoubleClick    += (s, e) => DoubleClick?.Invoke(s, e);
            TextEditor.ActiveTextAreaControl.TextArea.MouseMove      += (s, e) => MouseMove?.Invoke(s, e);
            TextEditor.ActiveTextAreaControl.TextArea.MouseEnter     += (s, e) => MouseEnter?.Invoke(s, e);
            TextEditor.ActiveTextAreaControl.TextArea.MouseLeave     += (s, e) => MouseLeave?.Invoke(s, e);
            TextEditor.ActiveTextAreaControl.TextArea.MouseDown      += (s, e) =>
            {
                SelectedLineChanged?.Invoke(
                    this,
                    new SelectedLineEventArgs(
                        TextEditor.ActiveTextAreaControl.TextArea.TextView.GetLogicalLine(e.Y)));
            };

            HighlightingManager.Manager.DefaultHighlighting.SetColorFor("LineNumbers", new HighlightColor(Color.FromArgb(80, 0, 0, 0), Color.White, false, false));
            TextEditor.ActiveTextAreaControl.TextEditorProperties.EnableFolding = false;

            _lineNumbersControl = new DiffViewerLineNumberControl(TextEditor.ActiveTextAreaControl.TextArea);

            VRulerPosition = AppSettings.DiffVerticalRulerPosition;
        }
Exemple #4
0
        private void OnKeyUp(object sender, KeyEventArgs e)
        {
            Keys key = e.KeyCode;

            if (key == Keys.LControlKey || key == Keys.RControlKey)
            {
                ctrl = false;
            }

            if (key == Keys.LMenu || key == Keys.RMenu)
            {
                alt = false;
            }

            if (key == Keys.LShiftKey || key == Keys.RShiftKey)
            {
                shift = false;
            }

            foreach (HotKeyToAction hotKeyToAction in HotKeys)
            {
                if (hotKeyToAction.HotKey.Match(key, ctrl, alt, shift))
                {
                    KeyUp?.Invoke(sender, e, hotKeyToAction);
                    return;
                }
            }
        }
Exemple #5
0
        public async Task OnKeyUp(KeyPress keyPress)
        {
            lock (this)
            {
                var key = keyPress.Key;

                if (keyPress.Id == _lastKeyPressId)
                {
                    Debug.WriteLine("Ignoring duplicate keypress");
                    return;
                }

                _lastKeyPressId = keyPress.Id;

                Debug.WriteLine("OnKeyUp");
                byte keyCode = 0x00;
                if (key.Length == 1)
                {
                    keyCode = (byte)key[0];
                }

                _eventBuffer.Write(
                    new KeyboardEvent(
                        (byte)(StatusBits.AsciiAvailable | StatusBits.KeyUp | StatusBits.ScanCodeAvailable),
                        keyCode,
                        keyCode));

                KeyUp?.Invoke(this, keyCode);

                RequestInterrupt?.Invoke(this, null);
            }
            await Task.Delay(0);
        }
        private int HookProc(int code, int wParam, ref User32.KeyboardHookStruct lParam)
        {
            if (code >= 0)
            {
                var key = (Keys)lParam.vkCode;
                if (HookedKeys.Contains(key))
                {
                    key = AddModifiers(key);

                    var kea = new KeyEventArgs(key);
                    if (wParam == User32.WM_KEYDOWN || wParam == User32.WM_SYSKEYDOWN)
                    {
                        KeyDown?.Invoke(this, kea);
                    }
                    if (wParam == User32.WM_KEYUP || wParam == User32.WM_SYSKEYUP)
                    {
                        KeyUp?.Invoke(this, kea);
                    }
                    if (kea.Handled)
                    {
                        return(1);
                    }
                }
            }
            return(User32.CallNextHookEx(_hhook, code, wParam, ref lParam));
        }
Exemple #7
0
        static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                var hookStruct = Marshal.PtrToStructure <KBDLLHOOKSTRUCT>(lParam);

                int vkCode = hookStruct.vkCode;
                int flags  = hookStruct.flags;

                if (wParam == (IntPtr)WM_KEYDOWN ||
                    (GetSystemKeyEvent && wParam == (IntPtr)WM_SYSKEYDOWN))
                {
                    if (KeyDown?.Invoke(vkCode) == false)
                    {
                        return((IntPtr)1);
                    }
                }

                if (wParam == (IntPtr)WM_KEYUP ||
                    (GetSystemKeyEvent && wParam == (IntPtr)WM_SYSTEMKEYUP))
                {
                    if (KeyUp?.Invoke(vkCode) == false)
                    {
                        return((IntPtr)1);
                    }
                }
            }

            return(CallNextHookEx(_hookID, nCode, wParam, lParam));
        }
Exemple #8
0
 public override void ProcessWindowMessage(ref System.Windows.Forms.Message m)
 {
     if (m.Msg == _MsgID_KeyboardLL)
     {
         KBDLLHOOKSTRUCT kbhs = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(m.LParam, typeof(KBDLLHOOKSTRUCT));
         if (m.WParam == (IntPtr)WindowsApi.WM_KEYDOWN && KeyDown != null)
         {
             KeyDown(this, new KeyEventArgs((Keys)kbhs.vkCode));
         }
         else
         {
             if (m.WParam == (IntPtr)WindowsApi.WM_KEYUP && KeyUp != null)
             {
                 KeyUp.Invoke(this, new KeyEventArgs((Keys)kbhs.vkCode));
             }
             if (m.WParam == (IntPtr)WindowsApi.WM_KEYUP && KeyPress != null)
             {
                 KeyPress(this, new KeyEventArgs((Keys)kbhs.vkCode));
             }
         }
     }
     else if (m.Msg == _MsgID_KeyboardLL_HookReplaced)
     {
         if (HookReplaced != null)
         {
             HookReplaced();
         }
     }
 }
        public override void ResponseReceived(byte[] parameter)
        {
            switch ((LiveKeyloggerCommunication)parameter[0])
            {
            case LiveKeyloggerCommunication.StringDown:
                StringDown?.Invoke(this, Encoding.UTF8.GetString(parameter, 1, parameter.Length - 1));
                break;

            case LiveKeyloggerCommunication.SpecialKeyDown:
                KeyDown?.Invoke(this,
                                new Serializer(new[] { typeof(KeyLogEntry), typeof(SpecialKey), typeof(StandardKey) })
                                .Deserialize <KeyLogEntry>(parameter, 1));
                break;

            case LiveKeyloggerCommunication.SpecialKeyUp:
                KeyUp?.Invoke(this,
                              new Serializer(new[] { typeof(KeyLogEntry), typeof(SpecialKey), typeof(StandardKey) })
                              .Deserialize <KeyLogEntry>(parameter, 1));
                break;

            case LiveKeyloggerCommunication.WindowChanged:
                WindowChanged?.Invoke(this, Encoding.UTF8.GetString(parameter, 1, parameter.Length - 1));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemple #10
0
        public void Update()
        {
            var keyboardState = _keyboard.GetState();

            var pressedKeys         = keyboardState.GetPressedKeys().ToHashSet();
            var previousPressedKeys = _previousKeyboardState.GetPressedKeys().ToHashSet();

            if (KeyUp != null)
            {
                foreach (var key in previousPressedKeys)
                {
                    if (!pressedKeys.Contains(key))
                    {
                        KeyUp.Invoke(this, new KeyEventArgs(key));
                    }
                }
            }

            if (KeyDown != null)
            {
                foreach (var key in pressedKeys)
                {
                    if (!previousPressedKeys.Contains(key))
                    {
                        KeyDown.Invoke(this, new KeyEventArgs(key));
                    }
                }
            }

            _previousKeyboardState = keyboardState;
        }
Exemple #11
0
        private static int KeyboardCallBack(int code, WinAPI.User32.WindowsMessage wParam, ref WinAPI.User32.KeyboardHookStruct lParam)
        {
            // Если code < 0, мы не должны обрабатывать это сообщение системы
            if (code >= 0)
            {
                var key = KeyInterop.KeyFromVirtualKey((int)lParam.VKCode);
                KeyEventArgsCustom eventArgs = new KeyEventArgsCustom(key);

                // В зависимости от типа пришедшего сообщения вызовем то или иное событие
                if (lParam.dwExtraInfo == IntPtr.Zero)
                {
                    switch (wParam)
                    {
                    case WinAPI.User32.WindowsMessage.KeyDown:
                    case WinAPI.User32.WindowsMessage.SysKeyDown:
                        KeyDown?.Invoke(null, eventArgs);
                        break;

                    case WinAPI.User32.WindowsMessage.KeyUp:
                    case WinAPI.User32.WindowsMessage.SysKeyUp:
                        KeyUp?.Invoke(null, eventArgs);
                        break;
                    }
                }
                // Если событие помечено приложением как обработанное,
                // прервём дальнейшее распространение сообщения
                if (eventArgs.Handled)
                {
                    return(1);
                }
            }
            // Вызовем следующий обработчик
            return(WinAPI.User32.CallNextHookEx(_keyboardHookHandle, code, wParam, ref lParam));
        }
        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                int  vkCode = Marshal.ReadInt32(lParam);
                Keys key    = (Keys)vkCode;

                var eventArgs = new KeyEventArgs(key);

                if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)260)
                {
                    DownKeys.Add(key);
                    KeyDown?.Invoke(eventArgs);
                }
                else if (wParam == (IntPtr)WM_KEYUP)
                {
                    DownKeys.Remove(key);
                    KeyUp?.Invoke(eventArgs);
                }

                if (eventArgs.Cancel)
                {
                    return(new IntPtr(1));
                }
            }

            return(CallNextHookEx(HookID, nCode, wParam, lParam));
        }
Exemple #13
0
        public int HookProc(int code, int wParam, ref KeyBoardHookStruct lParam)
        {
            if (code >= 0)
            {
                var key = (Keys)lParam.VkCode;
                if (HookedKeys.Contains(key))
                {
                    var Arg = new KeyEventArgs(key);
                    switch (wParam)
                    {
                    case WM_KEYDOWN:
                    case WM_SYSKEYDOWN:
                        KeyDown?.Invoke(this, Arg);
                        break;

                    case WM_KEYUP:
                    case WM_SYSKEYUP:
                        KeyUp?.Invoke(this, Arg);
                        break;
                    }

                    if (Arg.Handled)
                    {
                        return(1);
                    }
                }
            }

            return(CallNextHookEx(Hook, code, wParam, ref lParam));
        }
        public async void WheelStopped()
        {
            await Task.Delay(100);

            KeyUp?.Invoke(this, new HookEventArgs("Wheel Down"));
            KeyUp?.Invoke(this, new HookEventArgs("Wheel Up"));
        }
Exemple #15
0
        private void OnKeyEvent(KeyActivityEvent ev)
        {
            switch (ev.Action)
            {
            case KeyEventActions.Down:
            {
                Noesis.Key key = _keyTable[(int)ev.KeyCode];
                if (key != Noesis.Key.None)
                {
                    KeyDown?.Invoke(this, key);
                }

                if (ev.Char != 0)
                {
                    Char?.Invoke(this, ev.Char);
                }

                break;
            }

            case KeyEventActions.Up:
            {
                Noesis.Key key = _keyTable[(int)ev.KeyCode];
                if (key != Noesis.Key.None)
                {
                    KeyUp?.Invoke(this, key);
                }

                break;
            }
            }
        }
Exemple #16
0
        private void KeyboardInputSource_KeyboardInput(object sender, RawKeyboardInputEventArgs e)
        {
            // only evaluate this event if it is actually intended for this device
            //TODO: add some sort of conditional event invocation before this
            if (e.Header.hDevice != DeviceHandle)
            {
                return;
            }

            VirtualKeys key = (VirtualKeys)e.Data.VirtualKey;

            switch ((WM)e.Data.Message)
            {
            case WM.KEYDOWN:
            {
                KeyDown?.Invoke(this, new KeyInputEventArgs(key));
                break;
            }

            case WM.KEYUP:
            {
                KeyUp?.Invoke(this, new KeyInputEventArgs(key));
                break;
            }

            case WM.SYSKEYDOWN:
            {
                //TODO: syskeydown
                break;
            }
            }
        }
Exemple #17
0
        public void KeyCallback(int key, int action, int scancode)
        {
            Keys k = IntToKeys(key);

            //Console.WriteLine(k.ToString() + " " + (action == 1 ? "PRESSED" : "LIFTED"));
            if (key >= (int)SpecialKeys.D0 && key <= (int)SpecialKeys.D9)
            {
                LastNumberKey = key - (int)SpecialKeys.D0;
            }
            //ignores numlock
            else if (key >= (int)SpecialKeys.NP0 && key <= (int)SpecialKeys.NP9)
            {
                LastNumberKey = key - (int)SpecialKeys.NP0;
            }
            else if (key >= 65 && key <= 90)
            {
                LastLetterKey = char.ToLower((char)key);
            }
            else if (key >= 32 && key <= 127)
            {
                LastLetterKey = (char)key;
            }
            LastKey = (char)key;

            if (action == 1)
            {
                KeyDown.Invoke(k, this);
            }
            if (action == 0)
            {
                KeyUp.Invoke(k, this);
            }
        }
Exemple #18
0
        internal override IntPtr Callback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            var data = (KBDLLHOOKSTRUCT)PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

            var ea = new LowLevelKeyboardProcEventArgs(data);

            LowLevelKeyboardProc?.Invoke((WPARAM)wParam, ea);

            switch ((WPARAM)wParam)
            {
            case WPARAM.WM_KEYDOWN:
                KeyDown?.Invoke(ea);
                break;

            case WPARAM.WM_KEYUP:
                KeyUp?.Invoke(ea);
                break;

            case WPARAM.WM_SYSKEYDOWN:
                SysKeyDown?.Invoke(ea);
                break;

            case WPARAM.WM_SYSKEYUP:
                SysKeyUp?.Invoke(ea);
                break;
            }

            return(base.Callback(nCode, wParam, lParam));
        }
Exemple #19
0
        private int HookProc(int code, int wParam, ref User32.KeyboardHookStruct lParam)
        {
            if (code < 0)
            {
                return(User32.CallNextHookEx(_hhook, code, wParam, ref lParam));
            }

            if (IsWindowsKeyPressed())
            {
                return(User32.CallNextHookEx(_hhook, code, wParam, ref lParam));
            }

            var key = (Keys)lParam.vkCode;

            key = AddModifiers(key);

            var kea = new KeyEventArgs(key);

            if (wParam == User32.WM_KEYDOWN || wParam == User32.WM_SYSKEYDOWN)
            {
                KeyDown?.Invoke(this, kea);
            }
            else if (wParam == User32.WM_KEYUP || wParam == User32.WM_SYSKEYUP)
            {
                KeyUp?.Invoke(this, kea);
            }

            return(kea.Handled ? 1 : User32.CallNextHookEx(_hhook, code, wParam, ref lParam));
        }
Exemple #20
0
        private int HookCallback(int code, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            int result = 0;

            try
            {
                if (!IsPaused && code >= 0)
                {
                    if (wParam.ToInt32() == WM_SYSKEYDOWN || wParam.ToInt32() == WM_KEYDOWN)
                    {
                        KeyDown?.Invoke(this, new KeyboardHookEventArgs(lParam));
                    }

                    if (wParam.ToInt32() == WM_SYSKEYUP || wParam.ToInt32() == WM_KEYUP)
                    {
                        KeyUp?.Invoke(this, new KeyboardHookEventArgs(lParam));
                    }
                }
            }
            finally
            {
                result = CallNextHookEx(IntPtr.Zero, code, wParam, ref lParam);
            }

            return(result);
        }
Exemple #21
0
        private void DeviceOnKeyboardInput(object sender, KeyboardInputEventArgs e)
        {
            if ((int)e.Key == 255)
            {
                // discard "fake keys" which are part of an escaped sequence
                return;
            }
            try
            {
                KeyUtils.CorrectRawInputData(e);

                //Debug.WriteLine($"RawInput {e.Key} {e.MakeCode} {e.ScanCodeFlags} {e.GetDeviceKey()}", "InputEvents");

                if (e.ScanCodeFlags.HasFlag(ScanCodeFlags.Break))
                {
                    pressedKeySequence.RemoveAll(k => k == e.Key);
                    KeyUp?.Invoke(sender, e);
                }
                else
                {
                    if (!pressedKeySequence.Contains(e.Key))
                    {
                        pressedKeySequence.Add(e.Key);
                    }
                    KeyDown?.Invoke(sender, e);
                }
            }
            catch (Exception exc)
            {
                Global.logger.Error("Exception while handling keyboard input. Error: " + exc.ToString());
            }
        }
Exemple #22
0
 /// <summary>
 /// Receives all KeyUp events, and checks if control is focused and
 /// should receive event.
 /// </summary>
 /// <param name="args">Key event arguments.</param>
 protected virtual void KeyUpIntercept(KeyEventArgs args)
 {
     if (this.guiManager.GetFocus() == this)
     {
         KeyUp.Invoke(args);
     }
 }
Exemple #23
0
        protected override int HookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                return(CallNextHook(nCode, wParam, lParam));
            }

            var keyAttributes = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

            var e = new KeyboardHookBaseEventArgs
            {
                Key = KeyInterop.KeyFromVirtualKey(keyAttributes.VkCode)
            };

            if (wParam == (IntPtr)WPARAM.WM_KEYDOWN || wParam == (IntPtr)WPARAM.WM_SYSKEYDOWN)
            {
                KeyDown?.Invoke(this, e);
            }
            else if (wParam == (IntPtr)WPARAM.WM_KEYUP || wParam == (IntPtr)WPARAM.WM_SYSKEYUP)
            {
                KeyUp?.Invoke(this, e);
            }

            if (e.Handled)
            {
                // Do not pass Go.  Do not collect $200.
                return(-1);
            }

            return(CallNextHook(nCode, wParam, lParam));
        }
 private static int HookProc(int code, int wParam, ref KeyboardHookStruct lParam)
 {
     if (code >= 0)
     {
         if (wParam == WMKEYDOWN || wParam == WMKEYUP)
         {
             byte[] keyboardState = new byte[256];
             int    result        = GetKeyboardState(keyboardState);
             var    downKeys      = (from virtualKey in DistinctVirtualKeys
                                     where (GetKeyState(virtualKey) & 0x80) != 0
                                     select(Keys) KeyInterop.VirtualKeyFromKey(KeyInterop.KeyFromVirtualKey(virtualKey))).ToList();
             foreach (var downKey in downKeys)
             {
                 if (!LastKeysPressed.Contains(downKey))
                 {
                     KeyDown?.Invoke(null, new KeyEventArgs(downKey));
                     LastKeysPressed.Add(downKey);
                 }
             }
             List <Keys> keysToRemove = new List <Keys>();
             foreach (var key in LastKeysPressed)
             {
                 if (!downKeys.Contains(key))
                 {
                     KeyUp?.Invoke(null, new KeyEventArgs(key));
                     keysToRemove.Add(key);
                 }
             }
             LastKeysPressed.RemoveAll(k => keysToRemove.Contains(k));
         }
     }
     return(CallNextHookEx(Hhook, code, wParam, ref lParam));
 }
Exemple #25
0
        private void OnInternalKeyUp(object sender, OpenTK.Input.KeyboardKeyEventArgs e)
        {
            var args = new KeyboardKeyEventArgs((Keyboard.Key)e.Key, GameWindow.Keyboard.GetState(), e.IsRepeat);

            OnKeyUp(this, args);
            KeyUp?.Invoke(this, args);
        }
Exemple #26
0
        private void DeviceOnKeyboardInput(object sender, KeyboardInputEventArgs e)
        {
            if ((int)e.Key == 255)
            {
                // discard "fake keys" which are part of an escaped sequence
                return;
            }

            KeyUtils.CorrectRawInputData(e);

            //Debug.WriteLine($"RawInput {e.Key} {e.MakeCode} {e.ScanCodeFlags} {e.GetDeviceKey()}", "InputEvents");

            if (e.ScanCodeFlags.HasFlag(ScanCodeFlags.Break))
            {
                pressedKeySequence.RemoveAll(k => k == e.Key);
                KeyUp?.Invoke(sender, e);
            }
            else
            {
                if (!pressedKeySequence.Contains(e.Key))
                {
                    pressedKeySequence.Add(e.Key);
                }
                KeyDown?.Invoke(sender, e);
            }
        }
Exemple #27
0
        public virtual void OnKeyUp(KeyEventArgs e)
        {
            if (!_enabled)
            {
                return;
            }

            if (Controls != null)
            {
                for (int i = Controls.Count - 1; i >= 0; i--)
                {
                    if (e.Handled)
                    {
                        return;
                    }
                    else
                    {
                        Controls[i].OnKeyUp(e);
                    }
                }
            }

            if (KeyUp == null)
            {
                return;
            }
            KeyUp.Invoke(this, e);
        }
Exemple #28
0
        public void Update(GameTime gt)
        {
            KeyboardState keyboardState = KeyboardState;

            Keys[] last    = lastKeyboardState.GetPressedKeys();
            Keys[] current = keyboardState.GetPressedKeys();

            foreach (Keys key in current.Except(last))
            {
                KeyDown?.Invoke(this, new KeyEventArgs(key, keyboardState));
            }

            foreach (Keys key in last.Except(current))
            {
                KeyUp?.Invoke(this, new KeyEventArgs(key, keyboardState));
            }

            lastKeyboardState = KeyboardState;

            MouseState mouseState = MouseState;

            if (lastMouseState.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed)
            {
                MouseDown?.Invoke(this, new MouseEventArgs(MouseButtons.Left, mouseState));
            }

            if (lastMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released)
            {
                MouseUp?.Invoke(this, new MouseEventArgs(MouseButtons.Left, mouseState));
            }

            lastMouseState = mouseState;
        }
Exemple #29
0
        private IntPtr HookCallback(int code, WindowMessages wParam, KeyboardHookInput lParam)
        {
            IntPtr result;

            try
            {
                if (code >= 0)
                {
                    var key = (VirtualKey)Marshal.ReadInt32(new IntPtr(lParam.VirtualKey));

                    if (wParam == WindowMessages.KeyDown || wParam == WindowMessages.SysKeyDown)
                    {
                        KeyDown?.Invoke(this, key);
                    }

                    if (wParam == WindowMessages.KeyUp || wParam == WindowMessages.SysKeyUp)
                    {
                        KeyUp?.Invoke(this, key);
                    }
                }
            }
            finally
            {
                result = User32.CallNextHookEx(_hook, code, wParam, ref lParam);
            }

            return(result);
        }
Exemple #30
0
        /// <summary>
        /// The callback for the keyboard hook
        /// </summary>
        /// <param name="code">The hook code, if it isn't >= 0, the function shouldn't do anyting</param>
        /// <param name="wParam">The event type</param>
        /// <param name="lParam">The keyhook event information</param>
        /// <returns></returns>
        public int HookProc(int code, int wParam, ref KeyboardHookStruct lParam)
        {
            if (code >= 0)
            {
                try
                {
                    bool isSysKey = (wParam == WM_SYSKEYDOWN || wParam == WM_SYSKEYUP);

                    if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)
                    {
                        bool isHeld = KeyPressStateArr[lParam.vkCode];
                        var  keyArg = new RawKeyEventArgs(lParam.vkCode, isSysKey, isHeld);
                        KeyPressStateArr[lParam.vkCode] = true;
                        KeyDown?.Invoke(keyArg);
                    }
                    else if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP)
                    {
                        var keyArg = new RawKeyEventArgs(lParam.vkCode, isSysKey, false);
                        KeyPressStateArr[lParam.vkCode] = false;
                        KeyUp?.Invoke(keyArg);
                    }
                }
                catch
                {
                    //noop
                }
            }
            return(CallNextHookEx(_hhook, code, wParam, ref lParam));
        }