按键/向上键问题

时间:2013-11-17 17:51:17

标签: c#

这是我的代码:

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;
            }

        }

这会发生什么:

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.

(即使两个键都向下或向上,它也不应影响另一个键的行为)。

按键启动时应该立即执行的操作应该在按键启动后立即执行,只有在按键启动时才会发生。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我发布这个不完整的代码只是为了帮助您有所了解,您可以添加更多case来完全实现您的代码:

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;
}