KeyDown on Desktop

SerijSerij DEMember
edited June 2016 in CocosSharp

Is it possible to check which keys are currently down (not pressed).

CCEventListenerKeyboard only has Pressed and released.

I've tried to code a workaround, and its working, but not even perfect.

`
public class Movement
{
public Dictionary<CCKeys, CCEventKeyboard> PressedKeys { get; private set; }

    public Action<CCEventKeyboard> OnKeyDown;
    public Action<CCEventKeyboard> OnKeyUp;
    public Action<CCEventKeyboard> OnKeyPressed;

    public Movement(CCLayer layer) {
        PressedKeys = new Dictionary<CCKeys, CCEventKeyboard>();


        CCEventListenerKeyboard keyboard = new CCEventListenerKeyboard();
        keyboard.OnKeyPressed = keypress;
        keyboard.OnKeyReleased = keyrelease;
        layer.AddEventListener(keyboard);

        layer.Schedule(t => {
            foreach (CCKeys key in PressedKeys.Keys)
            {
                OnKeyDown?.Invoke(PressedKeys[key]);
            }
        });
    }

    void keypress(CCEventKeyboard e) {
        PressedKeys.Add(e.Keys, e);
        OnKeyPressed?.Invoke(e);
    }
    void keyrelease(CCEventKeyboard e) {
        PressedKeys.Remove(e.Keys);
        OnKeyUp?.Invoke(e);
    }


}

`

Tagged:

Answers

  • SerijSerij DEMember
    edited June 2016

    I've found myself a workaround.
    For those who has the same problem:

    `
    public class Movement
    {
    public enum MovementKeys
    {
    Up,
    Down,
    Right,
    Left,
    Action1,
    Action2,
    Action3,
    Action4,
    Action5
    }

        public class MovementEvent {
            public CCEventKeyboard Event;
            public MovementKeys MovementKey;
            public bool IsDown;
            public CCKeys ReactionKey;
        }
    
        public List<MovementEvent> PressedKeys { get; private set; }
    
        public Action<MovementEvent> OnKeyDown;
        public Action<MovementEvent> OnKeyUp;
        public Action<MovementEvent> OnKeyPressed;
    
        public Movement(CCLayer layer) {
            PressedKeys = new List<MovementEvent>();
    
            CCEventListenerKeyboard keyboard = new CCEventListenerKeyboard();
            keyboard.OnKeyPressed = keypress;
            keyboard.OnKeyReleased = keyrelease;
            layer.AddEventListener(keyboard);
    
            layer.Schedule(t => {
                foreach (MovementEvent e in PressedKeys)
                {
                    if (e.IsDown) {
                        OnKeyDown?.Invoke(e);
                    }
                }
            });
        }
    
        public void AssignKey(MovementKeys mKey, CCKeys keys) {
            PressedKeys.Add(new MovementEvent() {
                Event = null,
                MovementKey = mKey,
                IsDown = false,
                ReactionKey = keys
            });
        }
    
        void keypress(CCEventKeyboard e) {
           foreach (MovementEvent ev in PressedKeys)
            {
                if (ev.ReactionKey == e.Keys)
                {
                    if (!ev.IsDown)
                    {
                        OnKeyPressed?.Invoke(ev);
                        ev.IsDown = true;
            return;
                    }
                }
            }
        }
        void keyrelease(CCEventKeyboard e) {
            foreach (MovementEvent ev in PressedKeys)
            {
                if (ev.ReactionKey == e.Keys)
                {
                    if (ev.IsDown)
                    {
                        OnKeyUp?.Invoke(ev);
                        ev.IsDown = false;
                return;
                    }
                }
            }
        }
    

    // KeyDown event

    void keyDown(Movement.MovementEvent e) {

            if (e.MovementKey == Movement.MovementKeys.Up)
            {
                MoveUp();
            } else if (e.MovementKey == Movement.MovementKeys.Down)
            {
                MoveDown();
            }
        }
    

    // Init it
    Movement movement = new Movement(layer);
    movement.OnKeyDown = keyDown;
    movement.AssignKey(TrianglEngine.Input.Movement.MovementKeys.Up, CCKeys.W);
    movement.AssignKey(TrianglEngine.Input.Movement.MovementKeys.Down, CCKeys.S);
    `

Sign In or Register to comment.