Jerka
Jerka

Reputation: 13

Visual Studio C# Keydown blocking each other

Is there a way to make two keydowns working at the same time, so they don't block each other?

    private void multiplayer_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up)
        {
            //do stuff
        }

        if (e.KeyCode == Keys.Down)
        {
            //do stuff
        }
    }

Upvotes: 1

Views: 80

Answers (3)

Matthew Whited
Matthew Whited

Reputation: 22433

You may be running into an issue with key repeat. If that is the case the down will only fire for the last key pressed. What you need to do instead is control the state with KeyDown, KeyUp, and Deactivate as well as have a timer which will do the actual work. The timer will control how fast your "game loop" runs... for my example I just enabled the timer and set the interval to 10.

public partial class Form1 : Form
{
    private bool _k1 = false;
    private bool _k2 = false;

    private bool _d1 = false;
    private bool _d2 = false;

    private int _u1 = 0;
    private int _u2 = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Up:
                _k1 = true;
                _d1= true;
                break;
            case Keys.Down:
                _k1 = true;
                _d1 = false;
                break;

            case Keys.W:
                _k2 = true;
                _d2 = true;
                break;
            case Keys.S:
                _k2 = true;
                _d2 = false;
                break;
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (_k1)
            label1.Text =( _u1 = _u1 + (_d1 ? 1 : -1)).ToString();
        if (_k2)
            label2.Text = (_u2 = _u2 + (_d2 ? 1 : -1)).ToString();
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Up:
                _k1 = false;
                _d1 = true;
                break;
            case Keys.Down:
                _k1 = false;
                _d1 = false;
                break;

            case Keys.W:
                _k2 = false;
                _d2 = true;
                break;
            case Keys.S:
                _k2 = false;
                _d2 = false;
                break;
        }
    }

    private void Form1_Deactivate(object sender, EventArgs e)
    {
        _k1 = false;
        _k2 = false;
    }
}

Upvotes: 2

keith
keith

Reputation: 5332

Use ThreadPool.QueueUserWorkItem to do non-blocking work but care needs to be taken if you try to update your UI from your worker threads. You might also need to look into Control.Invoke if you are using WinForms.

Upvotes: 0

MaKCbIMKo
MaKCbIMKo

Reputation: 2820

You can handle some key press combinations by this:

if (e.Control && e.KeyCode == Keys.K) {
    //Your code here
}

But it requires to use some special keys pressed (like, Alt',Control,Shift` etc).

Upvotes: 0

Related Questions