Joscplan
Joscplan

Reputation: 1034

Key down/up issue

here is my code:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Point lastClick;
        private Dictionary<Keys, bool> keyIsDown = new Dictionary<Keys, bool>();
        private Timer timer;
        private int stream1;
        public Form1()
        {
            InitializeComponent();

            keyIsDown.Add(Keys.D1, false);
            keyIsDown.Add(Keys.D2, false);
            keyIsDown.Add(Keys.D3, false);
            keyIsDown.Add(Keys.D4, false);
            keyIsDown.Add(Keys.A, false);
            keyIsDown.Add(Keys.S, false);
            keyIsDown.Add(Keys.D, false);
            keyIsDown.Add(Keys.F, false);
            keyIsDown.Add(Keys.Z, false);
            keyIsDown.Add(Keys.X, false);
            keyIsDown.Add(Keys.C, false);
            keyIsDown.Add(Keys.V, false);

            this.KeyPreview = true;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (true == keyIsDown.ContainsKey(e.KeyCode))
            {
                keyIsDown[e.KeyCode] = true;
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (true == keyIsDown.ContainsKey(e.KeyCode))
            {
                keyIsDown[e.KeyCode] = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // This makes the computer constantly call the playKeys method
            timer = new Timer();
            timer.Interval = 10000;
            timer.Tick += new EventHandler(playKeys);
            timer.Enabled = true;
        }

        private void playKeys(Object source, EventArgs e)
        {
            if (true == keyIsDown[Keys.D1])
            {
                Beat1.Image = Beatpadpc.Properties.Resources.white_square_button;
            }
            else
            {
                Beat1.Image = Beatpadpc.Properties.Resources.black_square_button;

            }
            if (true == keyIsDown[Keys.D2])
            {
                Beat2.Image = Beatpadpc.Properties.Resources.white_square_button;
            }
            else
            {
                Beat2.Image = Beatpadpc.Properties.Resources.black_square_button;
            }

        }

And what happens is this:

Key D1 down = key D1 white.
Key D1 up = nothing happens until timer os over.
Key D2 down = Key D1 black.
Key D2 up = nothing happens until timer is over.
Key D1 down = Key D2 black...
etc...

Key D1 down + Key D2 down = Key D1 white + Key D2 white.
Key D1 up + Key D2 up = Key D1 and D2 stays the same until timer is over.

It should be:

Key D1 down = Key D1 white.
Key D1 up = Key D1 black.
Key D2 down = Key D2 white.
Key D2 up = Key D2 black.

(Even if both keys are down or up it shouldn't affect the behavior of the other key).

What it should happen when key is up should be immediately after the key is up, it only happens when its down.

How do i fix this?

Upvotes: 1

Views: 154

Answers (1)

King King
King King

Reputation: 63317

I posted this incomplete code just to help you have some idea, you can add more case to fully implement your code:

bool keyDown;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if(keyIsDown[e.KeyCode]) return;
    switch(e.KeyCode){
      case Keys.D1:
           Beat1.Image = Beatpadpc.Properties.Resources.white_square_button;
           break;
      case Keys.D2:
           Beat2.Image = Beatpadpc.Properties.Resources.white_square_button;
           break;
    }
    keyIsDown[e.KeyCode] = true;
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{        
    switch(e.KeyCode){
      case Keys.D1:
           Beat1.Image = Beatpadpc.Properties.Resources.black_square_button;
           break;
      case Keys.D2:
           Beat2.Image = Beatpadpc.Properties.Resources.black_square_button;
           break;
    }
    keyIsDown[e.KeyCode] = false;
}

Upvotes: 1

Related Questions