简体   繁体   English

C#如果立即按下更多键,如何不停止其他功能?

[英]C# If more keys are pressed at once how to not stop functions of others?

I know the title was little unclear but i will explain. 我知道标题不太清楚,但我会解释。 My Example: when i press 'D' my object will move to right, same for 'A' just move to left, but when i press 'Space' while holding 'A'/'D',the object will just o the function of the 'Space'. 我的示例:当我按'D'时,对象将向右移动,与'A'相同,只是向左移动,但是当我在按住'A'/'D'的同时按'Space'时,对象将随函数“太空”。

if (e.KeyCode == Keys.A)
        {
            ply.Left -= 3;
        }

        if (e.KeyCode == Keys.D)
        {
            ply.Left += 3;
        }

        if (e.KeyCode == Keys.Space) {
            ply.Top -= 100;
        }

You can track the state of multiple keys through the use of both KeyUp and KeyDown by use of a HashSet<Keys> : 您可以通过使用HashSet<Keys>同时使用KeyUpKeyDown来跟踪多个键的状态:

private HashSet<Keys> _keys = new HashSet<Keys>();

public void Control_KeyDown(object sender, KeyEventArgs e)
{
    _keys.Add(e.KeyCode);
}
public void Control_KeyUp(object sender, KeyEventArgs e)
{
    _keys.Remove(e.KeyCode);
}

public bool IsKeyDown(Keys keyCode)
{
    return _keys.Contains(keyCode);
}

Now, whenever you want to check to see if a key is down, just do: 现在,每当您要检查按键是否按下时,只需执行以下操作:

if(IsKeyDown(Keys.A))
{
    ply.Left -= 3;
}

Keep in mind that if you just check key states in one of the key events, you will probably have issues with not detecting repeats properly. 请记住,如果仅检查关键事件之一中的关键状态,则可能会遇到无法正确检测重复的问题。 If you really want to handle this properly, you should use a Timer with a fairly small Interval that periodically polls with IsKeyDown and handles all your input. 如果您确实想正确处理此问题,则应使用一个具有较小IntervalTimer ,该Timer会定期通过IsKeyDown轮询并处理所有输入。

With a Timer named timer1 that has an interval of 100 , you can handle all your input like this, and have fairly responsive input to the full range of keys supported by your hardware. 使用间隔时间为100名为timer1Timer ,您可以像这样处理所有输入,并且对硬件支持的所有键范围具有相当敏感的输入。

private void timer1_Tick(object sender, EventArgs e)
{
    if (IsKeyDown(Keys.A))
    {
        ply.Left -= 3;
    }
    if (IsKeyDown(Keys.S))
    {
        ply.Top += 3;
    }
    if (IsKeyDown(Keys.D))
    {
        ply.Left += 3;
    }
    if (IsKeyDown(Keys.W))
    {
        ply.Top -= 3;
    }
}

As Bradley said, you could save all key events to a keystate cache. 正如Bradley所说,您可以将所有键事件保存到键状态缓存中。

This could look something like this pseudo code: 这可能看起来像下面的伪代码:

Event KeyDown(e) -> keystate[e.KeyCode] = true
Event KeyUp(e)   -> keystate[e.KeyCode] = false

That way, you should always know every state. 这样,您应该始终了解每个状态。

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

相关问题 如何检查键是否被多次点击或找出一个键被按下了多长时间 c# - How to check if key has been clicked more than once or to find out how long a key has been pressed for c# 如何检测 C# 表单中是否按下了多个键 - How to detect if multiple keys are pressed in C# forms C#如何在按下输入时停止while循环 - C# How to stop while loop when enter pressed C#识别焦点不清晰时按下的键 - C# Recognize keys pressed when not in focus 如何在C#中多次为DatePicker设置正确的验证? - How to set the correct validation for DatePicker more than once in C#? 如何防止在C#中的MenuStrip中多次添加项目? - How to prevent adding item more than once at MenuStrip in c#? C#| SharpDX.XInput | 如何检测控制器上的按钮是否被按下一次并且没有按住 - C# | SharpDX.XInput | How to detect if a button on the controller was pressed once and is not getting hold down 如何检查空格键是否被按下一次? 统一 3d c# - how to check if the space key is pressed once? unity 3d c# 按下任意键后,您将如何刷新应用程序? C# - How would you go about refreshing your application once you have pressed any key? C# C#:在文本框的 keydown 事件中,如何检测当前按下的修饰符 + 键? - C#: In the keydown event of a textbox, how do you detect currently pressed modifiers + keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM