示例#1
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;
         }
     }
 }
示例#2
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();
            }
        }
示例#3
0
        private void CreateNewInputBindingBundle(string name, OnKey handler, Key keybind,
                                                 Dictionary <Entity, string> contextRefs, ActionDelegate code)
        {
            InputBindingBundle input = new InputBindingBundle();

            input.name     = name;
            input.act      = InputManager.CreateAction(name);
            input.keyBinds = new List <Key>();
            input.keyBinds.Add(keybind);
            input.handlers = new List <OnKey>();
            input.handlers.Add(handler);
            input.contextRefs = contextRefs;
            if (handler == OnKey.UP || handler == OnKey.RELEASED)
            {
                input.codeUp = code;
            }
            else
            {
                input.codeDown = code;
            }
            if (input.act != null)
            {
                input.act.Primary = InputManager.CreateKeyboardBinding(keybind);
            }
            _actions.Add(input);
        }
示例#4
0
        private void InputSystemOnKeyChange(Input.IInput obj)
        {
            Dispatcher.Invoke(() => {
                SetBlock(_elmGrid[(int)(Math.Round(obj.BlockPos.X + 1)), (int)(Math.Round(-obj.BlockPos.Y + 1))]);

                if (Math.Abs(obj.CharPos.X) > 0.1f || Math.Abs(obj.CharPos.Y) > 0.1f)
                {
                    var c = _activeElement.GetStr((int)obj.CharPos.X, (int)-obj.CharPos.Y);
                    OnKey?.Invoke(c);
                }
            });
        }
示例#5
0
 public void AddAction(string name, OnKey handler, Key keybind,
                       Dictionary <Entity, string> contextRefs, ActionDelegate code)
 {
     if (IsKeyAlreadyBinded(keybind, name))
     {
         AddCodeToKeyAlreadyBinded(name, handler, code);
     }
     else
     {
         CreateNewInputBindingBundle(name, handler, keybind, contextRefs, code);
     }
 }
示例#6
0
 void sendEvent(KeyCode keycode, KeyboardEventType evType)
 {
     if (evType == KeyboardEventType.keyDown)
     {
         OnKey.Invoke(keycode);
     }
     if (evType == KeyboardEventType.KeyUp)
     {
         OnKeyUp.Invoke(keycode);
     }
     if (evType == KeyboardEventType.down)
     {
         OnKeyDown.Invoke(keycode);
     }
 }
        public InputManager()
        {
            var keyCodes = (Enum.GetValues(typeof(KeyCode)) as KeyCode[]).ToList();

            Observable
            .EveryUpdate()
            .Where(_ => Input.anyKey)
            .Select(_ => keyCodes.Find(Input.GetKey))
            .Subscribe(k => OnKey.OnNext(k));

            Observable
            .EveryUpdate()
            .Where(_ => Input.anyKeyDown)
            .Select(_ => keyCodes.Find(Input.GetKeyDown))
            .Subscribe(k => OnKeyDown.OnNext(k));
        }
示例#8
0
        private void AddCodeToKeyAlreadyBinded(string name, OnKey handler, ActionDelegate code)
        {
            var inputBindingBundle = _actions.Find(i => i.name == name);

            if (inputBindingBundle == null)
            {
                throw new NullReferenceException("No InputBindingBundle found\n");
            }
            if (!inputBindingBundle.handlers.Any(i => i == handler))
            {
                inputBindingBundle.handlers.Add(handler);
            }
            if (handler == OnKey.UP || handler == OnKey.RELEASED)
            {
                inputBindingBundle.codeUp = code;
            }
            else
            {
                inputBindingBundle.codeDown = code;
            }
        }
示例#9
0
        /// <summary>
        /// Setup the native platform and creates a window.
        /// </summary>
        /// <param name="config">Configuration for the platform - usually passed from the engine.</param>
        internal virtual void Setup(Configurator config)
        {
            OnMouseScroll.AddListener(scroll =>
            {
                _mouseScrollAccum += scroll;
                return(true);
            });
            PopulateKeyCodes();

            SetupPlatform(config);

            // Check if the platform initialization was successful.
            if (Window == null)
            {
                Engine.SubmitError(new Exception("Platform couldn't create window."));
                return;
            }

            if (Window.Context == null)
            {
                Engine.SubmitError(new Exception("Platform couldn't create context."));
                return;
            }

            // Bind this window and its context.
            // "There /can/ be only one."
            Window.Context.MakeCurrent();
            Gl.BindAPI(Window.Context.GetProcAddress);

            // Set display mode, show and focus.
            Window.DisplayMode = config.InitialDisplayMode;
            Window.WindowState = WindowState.Normal;

            // Attach default key behavior.
            OnKey.AddListener(DefaultButtonBehavior);

            IsSetup = true;
            IsOpen  = true;
        }
示例#10
0
        protected void UpdateKeyStatus(Key key, bool down)
        {
            var keyIndex = (short)key;

            // If it was down, and still is - then it's held.
            if (_keys[keyIndex] && down)
            {
                OnKey.Invoke(key, KeyStatus.Held);
            }

            // If it was down, but no longer is - it was let go.
            if (_keys[keyIndex] && !down)
            {
                OnKey.Invoke(key, KeyStatus.Up);
            }

            // If it was up, and now is down - it was pressed.
            if (!_keys[keyIndex] && down)
            {
                OnKey.Invoke(key, KeyStatus.Down);
            }
            _keys[keyIndex] = down;
        }
示例#11
0
        public static void Run()
        {
            try
            {
                Console.Clear();

                while (_running)
                {
                    ConsoleEx.WrapInOutputLock(() => {
                        Console.CursorVisible = false;
                        Console.SetCursorPosition(0, 0);
                        ConsoleEx.WriteLineEnh(SetTitle.Invoke().FixedWidthEnh(ConsoleEx.WindowWidth - 12) + "    " + DateTime.Now.ToStringHH24mmss());
                        ConsoleEx.WriteLineEnh(SetKeysHelp.Invoke().FixedWidthEnh(ConsoleEx.WindowWidth - 8 /*16*/) + "  " + "- *Q*U|T" /*$" * R*estart - *Q*U|T"*/);

                        OnRefresh?.Invoke();
                        Console.CursorVisible = true;
                    });

                    var keyInfo = ConsoleEx.ReadKeyWithTimeout(RefreshInterval);
                    if (keyInfo == null)
                    {
                        continue;
                    }

                    if (keyInfo.Value.KeyChar != 'Q' && keyInfo.Value.KeyChar != '|')
                    {
                        OnKey?.Invoke(keyInfo.Value.KeyChar.ToString());
                    }

                    HandleStandardKey(keyInfo);
                }
            }
            finally
            {
                _cts.Cancel();
            }
        }
示例#12
0
        void Update()
        {
            if (!IsTracking)
            {
                return;
            }

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

            if (isKey)
            {
                OnKey?.Invoke();
            }
            if (isKeyDown)
            {
                OnKeyDown?.Invoke();
            }
            if (isKeyUp)
            {
                OnKeyUp?.Invoke();
            }
        }
示例#13
0
        private void Update()
        {
            if (SDL_Installed && !SDL_Initialized)
            {
                InitSDL();
            }
            if (!SDL_Initialized)
            {
                return;
            }

            SDL.SDL_Event e;

            while (SDL.SDL_PollEvent(out e) > 0)
            {
                switch (e.type)
                {
                case SDL.SDL_EventType.SDL_KEYDOWN:
                    OnKey?.Invoke(e, true);
                    break;

                case SDL.SDL_EventType.SDL_KEYUP:
                    OnKey?.Invoke(e, false);
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERAXISMOTION:
                    OnAxisMotion?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERBUTTONDOWN:
                    OnButton?.Invoke(e, true);
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERBUTTONUP:
                    OnButton?.Invoke(e, false);
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERDEVICEADDED:
                    Controller.AddDevice(e.cdevice.which);
                    OnDeviceAdded?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERDEVICEREMOVED:
                    Controller.RemoveDisconnected();
                    OnDeviceRemoved?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERDEVICEREMAPPED:
                    OnDeviceRemapped?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_JOYAXISMOTION:
                    OnAxisMotion?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_JOYBALLMOTION:
                    OnBallMotion?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_JOYHATMOTION:
                    OnHatMotion?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_JOYBUTTONDOWN:
                    OnButton?.Invoke(e, true);
                    break;

                case SDL.SDL_EventType.SDL_JOYBUTTONUP:
                    OnButton?.Invoke(e, false);
                    break;

                case SDL.SDL_EventType.SDL_JOYDEVICEADDED:
                    Controller.AddDevice(e.jdevice.which);
                    OnDeviceAdded?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_JOYDEVICEREMOVED:
                    Controller.RemoveDisconnected();
                    OnDeviceRemoved?.Invoke(e);
                    break;

                default:
                    break;
                }
            }
        }
示例#14
0
        private static void OnInput(SInputEvent nativeEvent)
        {
            var managedEvent = new InputEvent(nativeEvent);

            if (managedEvent.DeviceType == InputDeviceType.Keyboard)
            {
                switch (managedEvent.State)
                {
                case InputState.Down:
                    SetKeyStateDown(managedEvent.KeyId, true);
                    break;

                case InputState.Released:
                    SetKeyStateDown(managedEvent.KeyId, false);
                    break;
                }

                if (KeyDown(KeyId.LShift))
                {
                    _lShiftDown = true;
                }
                if (KeyUp(KeyId.LShift))
                {
                    _lShiftDown = false;
                }
                if (KeyDown(KeyId.RShift))
                {
                    _rShiftDown = true;
                }
                if (KeyUp(KeyId.RShift))
                {
                    _rShiftDown = false;
                }

                // Preprocess keyName to contain valid Chars
                if (managedEvent.KeyId == KeyId.Space)
                {
                    managedEvent.KeyName = " ";
                }

                if (managedEvent.KeyName.Length == 1)
                {
                    if (ShiftDown)
                    {
                        managedEvent.KeyName = managedEvent.KeyName.ToUpper();
                    }

                    if (ShiftDown && char.IsDigit(managedEvent.KeyName[0]))
                    {
                        managedEvent.KeyName = "=!\"§$%&/()".Substring(managedEvent.KeyName[0] - '0', 1);
                    }
                }
                else
                {
                    string res;
                    managedEvent.KeyName = _charByDescription.TryGetValue(managedEvent.KeyName, out res) ? res : string.Empty;
                }

                OnKey?.Invoke(managedEvent);
            }
            else if (managedEvent.DeviceType == InputDeviceType.Gamepad || managedEvent.DeviceType == InputDeviceType.Mouse)
            {
                // Set keyName.key = string.Empty to avoid showing up gamepad
                // presses controller keys in text input forms
                managedEvent.KeyName = string.Empty;

                switch (managedEvent.State)
                {
                case InputState.Down:
                    SetKeyStateDown(managedEvent.KeyId, true);
                    break;

                case InputState.Released:
                    SetKeyStateDown(managedEvent.KeyId, false);
                    break;

                case InputState.Changed:
                    SetKeyStateValue(managedEvent.KeyId, managedEvent.Value);
                    break;
                }

                if (OnKey != null)
                {
                    OnKey(managedEvent);
                }
            }
            else if (managedEvent.DeviceType == InputDeviceType.EyeTracker)
            {
                if (managedEvent.KeyId == KeyId.EyeTracker_X)
                {
                    var axis = GetAxis("EyeTracker");
                    _axisByName["EyeTracker"] = new Vector2(managedEvent.Value, axis != null ? axis.y : 0);
                }
                if (managedEvent.KeyId == KeyId.EyeTracker_Y)
                {
                    var axis = GetAxis("EyeTracker");
                    _axisByName["EyeTracker"] = new Vector2(axis != null ? axis.x : 0, managedEvent.Value);
                }
            }
        }
    // returns true if autoKey successful
    public bool autoKey(Transform aobj, int frame, OnKey addCallback)
    {
        if(!_obj || aobj != _obj) { return false; }

        if(keys.Count <= 0) {
            if(position != cachedInitialPosition) {
                // if updated position, addkey
                addKey(frame, position, addCallback);
                return true;
            }
            return false;
        }
        Vector3 oldPos = getPositionAtFrame((float)frame, false);
        if(position != oldPos) {
            // if updated position, addkey
            addKey(frame, position, addCallback);
            return true;
        }

        return false;
    }
    // add a new key, default interpolation and easeType
    public AMKey addKey(int _frame, Vector3 _position, OnKey addCallback)
    {
        foreach(AMTranslationKey key in keys) {
            // if key exists on frame, update key
            if(key.frame == _frame) {
                if(addCallback != null)
                    addCallback(this, null);

                key.position = _position;
                // update cache
                updateCache();
                return null;
            }
        }
        AMTranslationKey a = gameObject.AddComponent<AMTranslationKey>();

        a.enabled = false;
        a.frame = _frame;
        a.position = _position;

        if(addCallback != null)
            addCallback(this, a);

        // add a new key
        keys.Add(a);
        // update cache
        updateCache();
        return a;
    }
示例#17
0
    // returns true if autoKey successful, sets output key
    public bool autoKey(Transform aObj, int frame, OnKey addCallback)
    {
        if(!_obj || _obj != aObj) { return false; }

        if(keys.Count <= 0) {
            if(rotation != cachedInitialRotation) {
                // if updated position, addkey
                addKey(frame, rotation, addCallback);
                return true;
            }

            return false;
        }
        Quaternion oldRot = getRotationAtFrame((float)frame);
        if(rotation != oldRot) {
            // if updated position, addkey
            addKey(frame, rotation, addCallback);
            return true;
        }

        return false;
    }
示例#18
0
 public KeyInfo(KeyCode keyCode, OnKey onKey, bool isDoubleKey)
 {
     this.keyCode     = keyCode;
     this.onKey       = onKey;
     this.isDoubleKey = isDoubleKey;
 }
示例#19
0
    // add a new key
    public AMKey addKey(int _frame, Quaternion _rotation, OnKey addCallback)
    {
        foreach(AMRotationKey key in keys) {
            // if key exists on frame, update key
            if(key.frame == _frame) {
                if(addCallback != null)
                    addCallback(this, null);

                key.rotation = _rotation;
                // update cache
                updateCache();
                return null;
            }
        }
        AMRotationKey a = gameObject.AddComponent<AMRotationKey>();
        a.enabled = false;
        a.frame = _frame;
        a.rotation = _rotation;
        // set default ease type to linear
        a.easeType = (int)EaseType.Linear;

        if(addCallback != null)
            addCallback(this, a);

        // add a new key
        keys.Add(a);
        // update cache
        updateCache();

        return a;
    }
示例#20
0
 private Input()
 {
     RootElement.OnKey       += (string key, bool isPressed) => { OnKey?.Invoke(key, isPressed); };
     RootElement.OnCharacter += (string character) => { OnCharacter?.Invoke(character); };
 }