简体   繁体   English

Visual Studio C#Keydown互相阻止

[英]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
        }
    }

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. 您需要做的是使用KeyDown,KeyUp和Deactivate控制状态,并有一个可以完成实际工作的计时器。 The timer will control how fast your "game loop" runs... for my example I just enabled the timer and set the interval to 10. 计时器将控制“游戏循环”的运行速度...对于我的示例,我刚刚启用了计时器并将时间间隔设置为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;
    }
}

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). 但是它需要使用一些特殊的按键(例如Alt', Control , Shift等)。

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. 使用ThreadPool.QueueUserWorkItem可以执行非阻塞工作,但是如果您尝试从工作线程中更新UI,则需要ThreadPool.QueueUserWorkItem小心。 You might also need to look into Control.Invoke if you are using WinForms. 如果您使用的是WinForms,则可能还需要查看Control.Invoke

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM