Esempio n. 1
0
        /// <summary>
        ///     获取键盘消息
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            // 如果该消息被丢弃(nCode<0
            if (nCode < 0)
            {
                return(Win32Helper.CallNextHookEx(_hKeyboardHook, nCode, wParam, lParam));
            }

            var keyboardHookStruct =
                (Win32Helper.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Helper.KeyboardHookStruct));
            var key = KeyInterop.KeyFromVirtualKey(keyboardHookStruct.vkCode);

            switch (wParam)
            {
            case Win32Helper.WM_KEYDOWN:
            case Win32Helper.WM_SYSKEYDOWN:
                //WM_KEYDOWN和WM_SYSKEYDOWN消息,将会引发OnKeyDownEvent事件
                // 此处触发键盘按下事件
                _onKeyDown?.Invoke(key);
                break;

            case Win32Helper.WM_KEYUP:
            case Win32Helper.WM_SYSKEYUP:
                //WM_KEYUP和WM_SYSKEYUP消息,将引发OnKeyUpEvent事件
                // 此处触发键盘抬起事件
                _onKeyUp?.Invoke(key);
                break;
            }

            return(Win32Helper.CallNextHookEx(_hKeyboardHook, nCode, wParam, lParam));
        }
Esempio n. 2
0
        protected bool OnKeyUpEvent(Win32Api.KBDLLHOOKSTRUCT kb)
        {
            KeyHookEventArgs e = new(kb);

            KeyUpEvent?.Invoke(this, e);
            return(e.Cancel);
        }
Esempio n. 3
0
        private IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam)
        {
            var keyboardInput = Marshal.PtrToStructure <User32.KEYBDINPUT>(lParam);
            var pressedKey    = (Keys)keyboardInput.wVk;

            if (code >= 0 && HookedKey == pressedKey)
            {
                var keyEvent = (int)wParam;

                Logger.Information("Key {pressedKey} was found", pressedKey);

                var keyEventArgs = new KeyEventArgs(pressedKey);
                // key down
                if (keyEvent == Constants.WM_KEYDOWN || keyEvent == Constants.WM_SYSKEYDOWN)
                {
                    KeyDownEvent?.Invoke(this, keyEventArgs);
                }
                // key up
                else if (keyEvent == Constants.WM_KEYUP || keyEvent == Constants.WM_SYSKEYUP)
                {
                    KeyUpEvent?.Invoke(this, keyEventArgs);
                }

                if (keyEventArgs.Handled)
                {
                    return(new IntPtr(1));
                }
            }

            return(User32.CallNextHookEx(_hook, code, wParam, lParam));
        }
Esempio n. 4
0
        protected override IntPtr CustomHookProc(IntPtr wParam, IntPtr lParam)
        {
            KBDLLHOOKSTRUCT kbd =
                (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
            bool injected = (kbd.flags & (uint)LLKHF.INJECTED) != 0;

            switch ((WM)wParam)
            {
            case WM.KEYDOWN:
            case WM.SYSKEYDOWN:
                if (GetAsyncKeyState((int)kbd.vkCode) >= 0 && KeyDownEvent?.Invoke(this, kbd.vkCode, injected) == true)
                {
                    return((IntPtr)(-1));
                }
                if (KeyPressEvent?.Invoke(this, kbd.vkCode, injected) == true)
                {
                    return((IntPtr)(-1));
                }
                break;

            case WM.KEYUP:
            case WM.SYSKEYUP:
                if (KeyUpEvent?.Invoke(this, kbd.vkCode, injected) == true)
                {
                    return((IntPtr)(-1));
                }
                break;
            }
            return(IntPtr.Zero);
        }
Esempio n. 5
0
        /// <summary>
        /// 键盘输入(扫码枪扫描)钩子
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            //如果该消息被丢弃(nCode<0)或者没有事件绑定处理程序则不会触发事件
            if (nCode >= 0 && (KeyDownEvent != null || KeyUpEvent != null || KeyPressEvent != null))
            {
                var keyDataFromHook = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                var keyData         = (Keys)keyDataFromHook.vkCode;
                //按下控制键
                if ((KeyDownEvent != null || KeyPressEvent != null) && (wParam == SwWmKeydown || wParam == SwWmSysKeyDown))
                {
                    if (IsCtrlAltShiftKeys(keyData) && _preKeysList.IndexOf(keyData) == -1)
                    {
                        _preKeysList.Add(keyData);
                    }
                }
                //WM_KEYDOWN和WM_SYS KEYDOWN消息,将会引发KeyDownEvent事件
                if (KeyDownEvent != null && (wParam == SwWmKeydown || wParam == SwWmSysKeyDown))
                {
                    var e = new KeyEventArgs(GetDownKeys(keyData));

                    KeyDownEvent?.Invoke(this, e);
                }
                //WM_KEYDOWN消息将引发KeyPressEvent
                if (KeyPressEvent != null && wParam == SwWmKeydown)
                {
                    var keyState = new byte[256];
                    GetKeyboardState(keyState);
                    var inBuffer = new byte[2];
                    if (ToAscii(keyDataFromHook.vkCode, keyDataFromHook.scanCode, keyState, inBuffer, keyDataFromHook.flags) == 1)
                    {
                        var e = new KeyPressEventArgs((char)inBuffer[0]);
                        KeyPressEvent(this, e);
                    }
                }
                //松开控制键
                if ((KeyDownEvent != null || KeyPressEvent != null) && (wParam == SwWmKeyup || wParam == SwWmSysKeyUp))
                {
                    if (IsCtrlAltShiftKeys(keyData))
                    {
                        for (var i = _preKeysList.Count - 1; i >= 0; i--)
                        {
                            if (_preKeysList[i] == keyData)
                            {
                                _preKeysList.RemoveAt(i);
                            }
                        }
                    }
                }
                //WM_KEYUP和WM_SYS KEYUP消息,将引发KeyUpEvent事件
                if (KeyUpEvent != null && (wParam == SwWmKeyup || wParam == SwWmSysKeyUp))
                {
                    var e = new KeyEventArgs(GetDownKeys(keyData));
                    KeyUpEvent?.Invoke(this, e);
                }
            }
            return((int)CallNextHookEx(new IntPtr(_hKeyboardHook), nCode, new IntPtr(wParam), lParam));
            //返回1吃掉按键 不往下传递
            //return 1;
        }
Esempio n. 6
0
 protected void OnKeyUpEvent(int keyCode)
 {
     if (KeyUpEvent == null)
     {
         return;
     }
     KeyUpEvent.Invoke(this, new OriginalKeyEventArg(keyCode));
 }
Esempio n. 7
0
 private void KeyboardHookListener_KeyUp(object sender, KeyEventArgs e)
 {
     if (IsFocused)
     {
         KeyUpEvent?.Invoke(sender, e);
         ProcessKey(false, (int)e.KeyCode, (int)e.Modifiers);
     }
 }
Esempio n. 8
0
        //地图控件键盘弹起事件
        private void axMapControl_eventKeyUp(object sender, _DHOSOFTMapControlEvents_eventKeyUpEvent e)
        {
            if (Utils.bPublishEvent == false)
            {
                return;
            }
            MFKeyEventArgs args = new MFKeyEventArgs();

            args.KeyEventArgs = new KeyEventArgs((Keys)e.nChar);
            if (KeyUpEvent != null)
            {
                KeyUpEvent.Invoke(this, args);
            }
        }
Esempio n. 9
0
        // 地图控件按键按下事件
        private void mapControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (MapFrame.GMap.Common.Utils.bPublishEvent == false)
            {
                return;
            }

            MFKeyEventArgs args = new MFKeyEventArgs();

            args.KeyEventArgs = e;
            if (KeyUpEvent != null)
            {
                KeyUpEvent.Invoke(this, args);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Returns callback method result.
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        public static IntPtr Callback(int nCode, IntPtr wParam, ref int lParam)
        {
            KeyCode = lParam;

            if (nCode >= 0 && wParam == (IntPtr)WmKeydown || wParam == (IntPtr)WmSyskeydown)
            {
                KeyDownEvent?.Invoke(lParam);
            }
            else if (nCode >= 0 && wParam == (IntPtr)WmKeyup || wParam == (IntPtr)WmSyskeyup)
            {
                KeyUpEvent?.Invoke(lParam);
            }

            return(CallNextHookEx(HookId, nCode, wParam, ref lParam));
        }
Esempio n. 11
0
        private bool KeyboardProcedure(int message, API.KBDLLHOOKSTRUCT param)
        {
            var isInjected = ((param.flags & (1 << 1)) | (param.flags & (1 << 4))) != 0;
            var isALTDown  = (param.flags & (1 << 5)) != 0;
            var args       = new KeyEventArgs(param.vkCode, isInjected, isALTDown);

            if (message == API.WM_KEYDOWN || message == API.WM_SYSKEYDOWN)
            {
                KeyDownEvent?.Invoke(this, args);
            }
            else if (message == API.WM_KEYUP || message == API.WM_SYSKEYUP)
            {
                KeyUpEvent?.Invoke(this, args);
            }
            return(args.IsPrevent);
        }
 void Update()
 {
     if (Input.GetKey(_keyCode))
     {
         if (_keyboardClickTime <= 0)
         {
             _keyDownEvent.Invoke();
         }
         _keyboardClickTime += Time.deltaTime;
         _keyClickEvent.Invoke(_keyboardClickTime);
     }
     else
     {
         if (_keyboardClickTime > 0)
         {
             _keyUpEvent.Invoke(_keyboardClickTime);
         }
         _keyboardClickTime = 0;
     }
 }
 public void Update(){
     KeyCode[] keyCodes = (KeyCode[]) Enum.GetValues(typeof(KeyCode));
     foreach(KeyCode keyCode in keyCodes) {
         if(Input.GetKey(keyCode)){
             if(PressedKeyCodes.ContainsKey(keyCode)){
                 OnKeyPressedEventData e = PressedKeyCodes[keyCode];
                 e.duration += Time.deltaTime;
                 OnKeyPressed.Invoke(e);
             } else {
                 OnKeyDown.Invoke(new OnKeyDownEventData(keyCode));
                 PressedKeyCodes.Add(keyCode, new OnKeyPressedEventData(keyCode));
             }
         } else {
             if(PressedKeyCodes.ContainsKey(keyCode)){
                 PressedKeyCodes.Remove(keyCode);
                 OnKeyUp.Invoke(new OnKeyUpEventData(keyCode));
             }
         }
     }
 }
Esempio n. 14
0
        /// <summary>
        /// Returns callback method result.
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        public static IntPtr Callback(int nCode, IntPtr wParam, ref int lParam)
        {
            if (wParam == null)
            {
                throw new ArgumentNullException(nameof(wParam));
            }

            KeyCode = lParam;

            if (nCode >= 0 && wParam == (IntPtr)WmKeydown || wParam == (IntPtr)WmSyskeydown)
            {
                KeyDownEvent?.Invoke(lParam);
            }
            else if (nCode >= 0 && wParam == (IntPtr)WmKeyup || wParam == (IntPtr)WmSyskeyup)
            {
                KeyUpEvent?.Invoke(lParam);
            }

            return(CallNextHookEx(HookId, nCode, wParam, ref lParam));
        }
Esempio n. 15
0
        private int HookCallback(int idHook, int wParam, IntPtr lParam)
        {
            if (idHook == HC_ACTION)
            {
                KeyboardHookStruct keyboardHookStruct = Marshal.PtrToStructure <KeyboardHookStruct>(lParam);
                switch (wParam)
                {
                case WM_KEYDOWN:
                    KeyDownEvent?.Invoke((CCKeyCode)keyboardHookStruct.vkCode);
                    break;

                case WM_KEYUP:
                    KeyUpEvent?.Invoke((CCKeyCode)keyboardHookStruct.vkCode);
                    break;

                default:
                    break;
                }
            }
            return(CallNextHookEx(hHook, idHook, wParam, lParam));
        }
Esempio n. 16
0
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (!IsKeySuppressionActive || nCode < 0)
            {
                return(CallNextHookEx(_hookID, nCode, wParam, lParam)); //continue call chain
            }

            var key = Marshal.PtrToStructure <Win32Structures.KBDLLHOOKSTRUCT>(lParam);

            //bufKey will hold both the scan code and the extended keyboard flag
            var bufKey = key.scanCode << 8;
            var ext    = (key.flags & Win32Structures.KBDLLHOOKSTRUCTFlags.LLKHF_EXTENDED);
            var alt    = (key.flags & Win32Structures.KBDLLHOOKSTRUCTFlags.LLKHF_ALTDOWN);

            bufKey |= (uint)ext;

            var isExtended = ext == Win32Structures.KBDLLHOOKSTRUCTFlags.LLKHF_EXTENDED;
            var isKeyUp    = (key.flags & Win32Structures.KBDLLHOOKSTRUCTFlags.LLKHF_UP) == Win32Structures.KBDLLHOOKSTRUCTFlags.LLKHF_UP;

            if (isKeyUp)
            {
                if (_lstKeyDownBuffer.Contains(bufKey))
                {
                    _lstKeyDownBuffer.Remove(bufKey);
                    KeyUpEvent?.Invoke(null, new KeystrokeEventArgs(key.scanCode, true, isExtended));
                }
            }
            else
            {
                //suppress key repeating events from firing the event
                if (!_lstKeyDownBuffer.Contains(bufKey))
                {
                    _lstKeyDownBuffer.Add(bufKey);
                    KeyDownEvent?.Invoke(null, new KeystrokeEventArgs(key.scanCode, false, isExtended));
                }
            }

            return((IntPtr)1); //don't continue handling this keystroke. prevents ALT from opening menu, alt-tab from switching screens, etc. Only do this when recording keypresses.
        }
 /// <summary>
 /// Fires the KeyUp event with the given parameters.
 /// </summary>
 /// <param name="keyboardInfo">Details about the key pressed, along with modifier key states.</param>
 public void KeyUp(KeyboardInfo keyboardInfo)
 {
     KeyUpEvent?.Invoke(this, keyboardInfo);
 }
Esempio n. 18
0
 public void KeyUp(object sender, KeyEventArgs args)
 {
     KeyUpEvent?.Invoke(sender, args);
 }
 protected void OnKeyUpEvent(int keyCode)
 {
     KeyUpEvent?.Invoke(this, new OriginalKeyEventArg(keyCode));
 }
Esempio n. 20
0
 /// <summary>
 /// 激发KeyUpEvent事件
 /// </summary>
 private void OnKeyUpEvent(char chr)
 {
     KeyUpEvent?.Invoke(chr);
 }
 private void OnKeyUp(Keys key) => KeyUpEvent?.Invoke(this, new KeyEventArgs(key), DateTime.Now);
Esempio n. 22
0
 protected void OnKeyUpEvent(int keyCode, OriginalKey key, bool isVirtualInput)
 {
     KeyUpEvent?.Invoke(this, new OriginalKeyEventArg(keyCode, key, isVirtualInput));
 }
Esempio n. 23
0
 protected bool OnKeyUpEvent(int keyCode, List <int> modifiers)
 {
     return((bool)(KeyUpEvent?.Invoke(this, new OriginalKeyEventArg(keyCode, modifiers))));
 }
Esempio n. 24
0
 public void OnKeyUpEvent(Slot slot)
 {
     KeyUpEvent?.Invoke(slot);
 }