Exemple #1
0
        private void FireKeyboardEvents()
        {
            // Check through each key in the key list
            foreach (Keys key in KeyList)
            {
                // Is the key currently down?
                if (CurrentKeyboardState.IsKeyDown(key))
                {
                    // Fire the OnKeyDown event
                    OnKeyDown?.Invoke(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                }

                // Has the key been released? (Was down and is now up)
                if (PrevKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key))
                {
                    OnKeyUp?.Invoke(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                }

                // Has the key been pressed? (Was up and is now down)
                if (PrevKeyboardState.IsKeyUp(key) && CurrentKeyboardState.IsKeyDown(key))
                {
                    OnKeyPressed?.Invoke(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                }
            }
        }
Exemple #2
0
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                return(CallNextHookEx(_hookId, nCode, wParam, lParam));
            }

            var vkCode  = Marshal.ReadInt32(lParam);
            var wmParam = (Wm)wParam;
            var isDown  = wmParam is Wm.Keydown or Wm.SysKeyDown;
            var isUp    = wmParam is Wm.Keyup or Wm.SysKeyUp;

            if (isDown || isUp)
            {
                var hotKey = HotKeyHandler.HasKey(vkCode);

                if (hotKey != null)
                {
                    Task.Run(() => HotKeyHandler.TryRunAction(hotKey, wmParam));
                    return(IntPtr.Parse("1"));
                }
            }

            if (isDown)
            {
                KeyDown?.Invoke(vkCode);
            }

            return(CallNextHookEx(_hookId, nCode, wParam, lParam));
        }
 private void InvokeEvents()
 {
     foreach (var key in Enum.GetValues(typeof(Keys)).Cast <Keys>())
     {
         if (IsKeyDown(key))
         {
             OnKeyDown?.Invoke(key);
             if (IsKeyPressed(key))
             {
                 OnKeyPressed?.Invoke(key);
             }
             if (IsKeyClicked(key))
             {
                 OnKeyClicked?.Invoke(key);
             }
         }
         else
         {
             OnKeyUp?.Invoke(key);
             if (IsKeyReleased(key))
             {
                 OnKeyReleased?.Invoke(key);
             }
         }
     }
 }
Exemple #4
0
        private void HookKeyboardCallback(HookData hookData)
        {
            KeyEventInformation info = KeyEventInformation.Get(hookData);

            Keys key = info.KeyCode |
                       (info.Control ? Keys.Control : Keys.None) |
                       (info.Shift ? Keys.Shift : Keys.None) |
                       (info.Alt ? Keys.Alt : Keys.None);


            if (info.IsKeyDown)
            {
                m_KeyState[info.KeyCode] = true;

                OnKeyCombo?.Invoke(key);

                OnKeyDown?.Invoke(key);

                m_KeyboardDownHandlers.TryInvoke(key);
            }

            if (info.IsKeyUp)
            {
                m_KeyState[info.KeyCode] = false;

                OnKeyUp?.Invoke(key);

                m_KeyboardUpHandlers.TryInvoke(key);
            }
        }
Exemple #5
0
        public static void Update()
        {
            if (OnKeyPressed == null && OnKeyDown == null)
            {
                return;
            }

            KeyboardState state = Keyboard.GetState();

            var keys = state.GetPressedKeys();

            foreach (var pressedKey in PressedKeys.ToArray())
            {
                if (keys.Contains(pressedKey) == false)
                {
                    PressedKeys.Remove(pressedKey);
                    OnKeyPressed?.Invoke(pressedKey);
                }
            }

            foreach (var key in keys)
            {
                if (PressedKeys.Contains(key) == false)
                {
                    OnKeyDown?.Invoke(key);
                    PressedKeys.Add(key);
                }
            }
        }
 private void Update()
 {
     if (Input.GetKeyDown(keyCode))
     {
         OnKeyDown?.Invoke(null);
     }
 }
        internal override void GHook_KeyDown(object sender, KeyEventArgs e)
        {
            if (!Loaded)
            {
                return;
            }

            if (e.Alt && e.KeyCode == Keys.LShiftKey)
            {
                KeyboardHelper.SwitchLang();
            }

            if (e.KeyCode == Keys.Insert)
            {
                if (Window.IsActivated)
                {
                    Window.Deactivate();
                }
                else
                {
                    Window.Activate();
                }
                Window.BringToFront();
            }


            OnKeyDown?.Invoke(sender, e);
        }
Exemple #8
0
        private void Update()
        {
#if UNITY_ANDROID || UNITY_IOS
            if (Input.touchCount > 0)
            {
                Touch touch = Input.GetTouch(0);

                switch (touch.phase)
                {
                case TouchPhase.Moved:
                    OnMouseClick1Held?.Invoke(touch.deltaPosition.x, touch.deltaPosition.y);
                    break;
                }
            }
#else
            if (Input.GetMouseButton(1))
            {
                OnMouseClick1Held?.Invoke(Input.GetAxis(mouseX), Input.GetAxis(mouseY));                         //envoyer un event seulement si la souris bouges aussi !
            }
            if (Input.GetAxis(vertical) != 0 || Input.GetAxis(horizontal) != 0)
            {
                OnKeyDown?.Invoke(Input.GetAxis(horizontal), Input.GetAxis(vertical));
            }

            if (Input.GetMouseButtonDown(0))
            {
                OnMouse0Down?.Invoke();
            }
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                OnEchapDown?.Invoke();
            }
#endif
        }
Exemple #9
0
        private void Update()
        {
            if (!IsTracking)
            {
                return;
            }

            bool isKey     = Input.GetKey(key);
            bool isKeyDown = Input.GetKeyDown(key);
            bool isKeyUp   = Input.GetKeyUp(key);

            if (isKey)
            {
                OnKey?.Invoke();
            }

            if (isKeyDown)
            {
                OnKeyDown?.Invoke();
            }

            if (isKeyUp)
            {
                OnKeyUp?.Invoke();
            }
        }
Exemple #10
0
 // Update is called once per frame
 void Update()
 {
     if (Input.anyKey)
     {
         foreach (KeyCode keyCode in Enum.GetValues(typeof(KeyCode)))
         {
             if (Input.GetKey(keyCode))
             {
                 OnKey?.Invoke(keyCode);
                 break;
             }
         }
     }
     if (Input.anyKeyDown)
     {
         foreach (KeyCode keyCode in Enum.GetValues(typeof(KeyCode)))
         {
             if (Input.GetKey(keyCode))
             {
                 codes.Add(keyCode);
                 OnKeyDown?.Invoke(keyCode);
                 break;
             }
         }
     }
     foreach (KeyCode keyCode in codes)
     {
         if (Input.GetKeyUp(keyCode))
         {
             OnKeyUp?.Invoke(keyCode);
             break;
         }
     }
 }
Exemple #11
0
        /// <summary>
        /// keyhook callback happens.
        /// </summary>
        /// <param name="nCode">not sure</param>
        /// <param name="wParam">event</param>
        /// <param name="lParam">the key being pressed</param>
        /// <returns></returns>
        private IntPtr KbHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                return(CallNextHookEx(_kbHookID, nCode, wParam, lParam));
            }

            int vkCode = Marshal.ReadInt32(lParam);

            switch ((int)wParam)
            {
            case WM_KEYDOWN:
            case WM_SYSKEYDOWN:
                if (!_currentlyPressed.Contains(vkCode))
                {
                    _currentlyPressed.Add(vkCode);
                    OnKeyDown?.Invoke(this, new KeyEventArgs(vkCode));
                }
                break;

            case WM_KEYUP:
            case WM_SYSKEYUP:
                if (_currentlyPressed.Contains(vkCode))
                {
                    _currentlyPressed.Remove(vkCode);
                }
                OnKeyUp?.Invoke(this, new KeyEventArgs(vkCode));
                break;
            }
            _hookTimeoutTimer.Stop();
            _hookTimeoutTimer.Start();

            return(CallNextHookEx(_kbHookID, nCode, wParam, lParam));
        }
Exemple #12
0
        /// <summary>
        /// Internal keyboard hook procedure
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                bool alt     = (Keyboard.Modifiers & ModifierKeys.Alt) != 0;
                bool control = (Keyboard.Modifiers & ModifierKeys.Control) != 0;

                if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_KEYUP)
                {
                    int forms_key = Marshal.ReadInt32(lParam);

                    Key key = KeyInterop.KeyFromVirtualKey(forms_key);

                    if (wParam == (IntPtr)WM_KEYDOWN && key == Key.Escape && control)
                    {
                        OnKeyDown?.Invoke(this, key, Keyboard.Modifiers);

                        return((IntPtr)1);
                    }

                    if (wParam == (IntPtr)WM_KEYUP && key == Key.Escape)
                    {
                        OnKeyUp?.Invoke(this, key, Keyboard.Modifiers);
                    }
                }
            }

            return(CallNextHookEx(m_hook_id, nCode, wParam, lParam));
        }
Exemple #13
0
        public Window(WindowConfig config)
        {
            _config = config;

            var opts = WindowOptions.DefaultVulkan;

            opts.WindowBorder = WindowBorder.Resizable;
            opts.Size         = new Silk.NET.Maths.Vector2D <int>((int)config.Width, (int)config.Height);
            opts.Title        = config.Title;
            opts.WindowState  = config.Fullscreen ? WindowState.Fullscreen : WindowState.Normal;

            _window         = Silk.NET.Windowing.Window.Create(opts);
            _window.Render += (time) => DrawFrame?.Invoke(time);

            _window.Initialize();

            _input = _window.CreateInput();
            var primaryKeyboard = _input.Keyboards.FirstOrDefault();

            if (primaryKeyboard != null)
            {
                primaryKeyboard.KeyDown += (keyboard, key, code) => OnKeyDown?.Invoke(key);
                primaryKeyboard.KeyUp   += (keyboard, key, code) => OnKeyUp?.Invoke(key);
            }
            for (int i = 0; i < _input.Mice.Count; i++)
            {
                _input.Mice[i].Cursor.CursorMode = config.CursorDisabled ? CursorMode.Disabled : CursorMode.Normal;
                _input.Mice[i].MouseMove        += (mouse, pos) => OnCursorPosition?.Invoke(pos.X, pos.Y);
                _input.Mice[i].Scroll           += (mouse, wheel) => OnScroll?.Invoke(wheel.X, wheel.Y);
                _input.Mice[i].MouseDown        += (mouse, button) => OnMouseDown?.Invoke(button);
                _input.Mice[i].MouseUp          += (mouse, button) => OnMouseUp?.Invoke(button);
            }
        }
Exemple #14
0
        private IntPtr _Hook(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code >= 0)
            {
                var kb = (NativeStructs.KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(NativeStructs.KBDLLHOOKSTRUCT));

                if (kb.flags == NativeEnums.KBDLLHOOKSTRUCTFlags.LLKHF_DOWN ||
                    kb.flags == NativeEnums.KBDLLHOOKSTRUCTFlags.LLKHF_ALTDOWN ||
                    kb.flags == NativeEnums.KBDLLHOOKSTRUCTFlags.LLKHF_EXTENDED)
                {
                    bool throwInput = false;
                    OnKeyDown?.Invoke(this, (Keys)kb.vkCode, ref throwInput);

                    if (throwInput)
                    {
                        //return new IntPtr(1);
                    }
                }
                else
                {
                    OnKeyUp?.Invoke(this, (Keys)kb.vkCode);
                }
            }

            return(NativeMethods.CallNextHookEx(mHookPtr, code, wParam, lParam));
        }
        public static void Initialize(ECSWorld world)
        {
            if (initialized)
            {
                return;
            }
            if (world == null)
            {
                throw  new ArgumentNullException(nameof(world), "The ECSWorld provided can not be null.");
            }
            if (!world.IsMainWorld)
            {
                throw new ArgumentException("The ECSWorld of the window needs to be the mainWorld", nameof(world));
            }

            WindowInstance     = Process.GetCurrentProcess().SafeHandle.DangerousGetHandle();
            window             = new Sdl2Window("ECS", 50, 50, 1280, 720, SDL_WindowFlags.Resizable, threadedProcessing: false);
            window.X           = 50;
            window.Y           = 50;
            window.Visible     = true;
            window.MouseWheel += (x) => OnMouseWheel?.Invoke(x);
            window.MouseMove  += (x) => OnMouseMove?.Invoke(x);
            window.MouseDown  += (x) => OnMouseDown?.Invoke(x);
            window.KeyDown    += (x) => OnKeyDown?.Invoke(x);
            window.Closed     += () => OnWindowClose?.Invoke();
            window.Resized    += () => OnWindowResize?.Invoke(window.Width, window.Height);

            world.EarlyUpdate += PumpEvents;

            initialized = true;
            GraphicsContext.Initialize(world);
        }
        public override void OnInputEvent(DeviceInputEventArgs e)
        {
            if (e.DeviceType != Type)
            {
                return;
            }
            var inputEvent = e as KeyboardInputEventArgs;

            if (inputEvent.KeyboardEvent.KeyState == KeyState.Press)
            {
                if (!mThePressKeys.Contains(inputEvent.KeyboardEvent.KeyCode))
                {
                    mThePressKeys.Add(inputEvent.KeyboardEvent.KeyCode);
                    mKeysStateDic[inputEvent.KeyboardEvent.KeyCode] = KeyState.Press;
                    TriggerActionMapping((int)inputEvent.KeyboardEvent.KeyCode, KeyState.Press);
                    OnKeyDown?.Invoke(this, inputEvent.KeyboardEvent);
                }
            }
            else
            {
                mKeysStateDic[inputEvent.KeyboardEvent.KeyCode] = KeyState.Release;
                mThePressKeys.Remove(inputEvent.KeyboardEvent.KeyCode);
                PulseEndAxisMapping((int)inputEvent.KeyboardEvent.KeyCode);
                TriggerActionMapping((int)inputEvent.KeyboardEvent.KeyCode, KeyState.Release);
                OnKeyUp?.Invoke(this, inputEvent.KeyboardEvent);
            }
        }
Exemple #17
0
 private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
 {
     if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
     {
         int vkCode = Marshal.ReadInt32(lParam);
         OnKeyDown?.Invoke(new KeyEventArgs((Keys)vkCode));
     }
     return(CallNextHookEx(_hookID, nCode, wParam, lParam));
 }
Exemple #18
0
 public void Tick()
 {
     foreach (KeyCode keyCode in _keyCodes)
     {
         if (Input.GetKeyDown(keyCode))
         {
             OnKeyDown?.Invoke(keyCode);
         }
     }
 }
        internal void InitKeyboardHandlers()
        {
            _keyboardHandler = new KeyboardHandler();

            _keyboardHandler.OnKeyDown     += (key) => OnKeyDown?.Invoke(key);
            _keyboardHandler.OnKeyUp       += (key) => OnKeyUp?.Invoke(key);
            _keyboardHandler.OnKeyPressed  += (key) => OnKeyPressed?.Invoke(key);
            _keyboardHandler.OnKeyClicked  += (key) => OnKeyClicked?.Invoke(key);
            _keyboardHandler.OnKeyReleased += (key) => OnKeyReleased?.Invoke(key);
        }
Exemple #20
0
        void OnPlatformTextEnter(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            string entry = e.TextComposition.Text;

            if (entry.Length == 1 && char.IsLetter(entry [0]))
            {
                KeyArgs.Character = entry;
                OnKeyDown?.Invoke(this, KeyArgs);
            }
        }
Exemple #21
0
        void OnPlatformKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            string entry = e.Key.ToString();

            if (entry.Length > 1)
            {
                KeyArgs.Character = e.Key.ToString();
                OnKeyDown?.Invoke(this, KeyArgs);
            }
        }
Exemple #22
0
 public override void KeyDown(NSEvent theEvent)
 {
     base.KeyDown(theEvent);
     if (OnKeyDown != null)
     {
         OnKeyDown.Invoke(this, new NSEventArgs {
             Event = theEvent
         });
     }
 }
Exemple #23
0
 private void ConsoleWindow_KeyDown(object sender, KeyboardKeyEventArgs e)
 {
     if (OnKeyDown != null)
     {
         OnKeyDown.Invoke(e);
     }
     if (!pressedKeys.Contains(e.Key))
     {
         pressedKeys.Add(e.Key);
     }
 }
Exemple #24
0
        private void Form_KeyDown(object sender, KeyEventArgs e)
        {
            if (OnKeyDown != null)
            {
                OnKeyDown.Invoke(e, pressed.Contains(e.KeyCode));
            }

            if (!pressed.Contains(e.KeyCode))
            {
                pressed.Add(e.KeyCode);
            }
        }
Exemple #25
0
        /// <summary>
        /// Checks pressed keys.
        /// </summary>
        public void CheckInput()
        {
            if (Input.GetKey(Condition))
            {
                OnKeyPressed?.Invoke();
            }

            if (Input.GetKeyDown(Condition))
            {
                OnKeyDown?.Invoke();
            }
        }
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0 || wParam != (IntPtr)WM_KEYDOWN)
            {
                return(CallNextHookEx(_hookId, nCode, wParam, lParam));
            }
            var vkCode = Marshal.ReadInt32(lParam);

            KeyDown?.Invoke(vkCode);

            return(CallNextHookEx(_hookId, nCode, wParam, lParam));
        }
        private IntPtr HookCallback(int nCode, WindowsMessages wParam, KBDLLHOOKSTRUCT lParam)
        {
            if (nCode >= 0 && wParam == WindowsMessages.WM_KEYDOWN || wParam == WindowsMessages.WM_SYSKEYDOWN)
            {
                OnKeyDown.Invoke(this, ((Keys)lParam.vkCode));
            }
            else if (nCode >= 0 && wParam == WindowsMessages.WM_KEYUP || wParam == WindowsMessages.WM_SYSKEYUP)
            {
                OnKeyUp.Invoke(this, ((Keys)lParam.vkCode));
            }

            return(User.CallNextHookEx(_hookID, nCode, wParam, lParam));
        }
Exemple #28
0
        public static void InvokeKeyDown(object args)
        {
            string[] a = args.ToString().Replace("[", null).Replace("]", null).Split(",");

            bool ctrl  = bool.Parse(a[1]);
            bool shift = bool.Parse(a[2]);
            bool alt   = bool.Parse(a[3]);

            ConsoleKey consoleKey = (ConsoleKey)Enum.Parse(typeof(ConsoleKey), a[0]);

            keyboardState = new BWHKeyboardState(consoleKey, ctrl, shift, alt, true);
            OnKeyDown?.Invoke(keyboardState);
        }
Exemple #29
0
        internal static void DWindowOnKeyDown(object sender, KeyboardKeyEventArgs e)
        {
            DKey       key       = (DKey)e.Key;
            DModifiers modifiers = new DModifiers(e.Shift, e.Control, e.Alt);

            if (!e.IsRepeat)
            {
                rootCanvas.OnKeyPressed(key, modifiers);
                OnKeyPress?.Invoke(key, modifiers);
            }
            rootCanvas.OnKeyDown(key, modifiers);

            OnKeyDown?.Invoke(key, modifiers);
        }
Exemple #30
0
        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                int vkCode = Marshal.ReadInt32(lParam);

                // keep track of pressed keys so we don't intercept hotkeys
                if (wParam == (IntPtr)NativeMethods.WM.KEYDOWN || wParam == (IntPtr)NativeMethods.WM.SYSKEYDOWN)
                {
                    if (!keysPressed.Contains(vkCode))
                    {
                        keysPressed.Add(vkCode);
                    }

                    var kpa = new KeyEventArgs(KeyInterop.KeyFromVirtualKey(vkCode));
                    OnKeyDown?.Invoke(this, kpa);
                    if (kpa.Handled)
                    {
                        return(new IntPtr(1));
                    }
                }

                // act only when key is raised
                if (wParam == (IntPtr)NativeMethods.WM.KEYUP || wParam == (IntPtr)NativeMethods.WM.SYSKEYUP)
                {
                    // if more than one key was pressed before a key was raised, user attempted hotkey
                    if (keysPressed.Count == 1 && OnKeyPressed != null)
                    {
                        var kpaPressed = new KeyEventArgs(KeyInterop.KeyFromVirtualKey(vkCode));
                        OnKeyPressed?.Invoke(this, kpaPressed);
                        if (kpaPressed.Handled)
                        {
                            return(new IntPtr(1));
                        }
                    }

                    // reset pressed keys
                    keysPressed.Clear();

                    var kpaUp = new KeyEventArgs(KeyInterop.KeyFromVirtualKey(vkCode));
                    OnKeyUp?.Invoke(this, kpaUp);
                    if (kpaUp.Handled)
                    {
                        return(new IntPtr(1));
                    }
                }
            }

            return(CallNextHookEx(_hookID, nCode, wParam, lParam));
        }