c# - 我如何将 2 个单独的按键识别为一个键?

标签 c#

所以对于我正在工作的游戏,我有一堆不同的键来做不同的事情(例如 w 键让玩家向上看,e 键让角色向上看)。

我想知道有没有办法让 W + D 键让玩家向上看,即使这些键已经被使用。

private void FrmGM_KeyDown(object sender, KeyEventArgs e)
    {            
        if (e.KeyCode == Keys.W)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player1));
            bulletnumb = 1;
        }

        if (e.KeyCode == Keys.E)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player2));
            bulletnumb = 2;
        }

        if (e.KeyCode == Keys.D)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player3));
            bulletnumb = 3;
        }

        if (e.KeyCode == Keys.C)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player4));
            bulletnumb = 4;
        }

        if (e.KeyCode == Keys.X)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player5));
            bulletnumb = 5;
        }

        if (e.KeyCode == Keys.Z)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player6));
            bulletnumb = 6;
        }

        if (e.KeyCode == Keys.A)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player7));
            bulletnumb = 7;
        }

        if (e.KeyCode == Keys.Q)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player8));
            bulletnumb = 8;
        }

这是我按键的代码

最佳答案

KeyDown 事件到达时,将字母存储在一个集合中,并在 KeyUp 事件处理程序中删除它们。这样您的 KeyDown 就能够“看到”是否按下了“配套键”:

private readonly ISet<char> keysCurrentlyDown = new HashSet<char>();

private void FrmGM_KeyDown(object sender, KeyEventArgs e) {            
    if (e.KeyCode == Keys.W)
    {
        if (keysCurrentlyDown.Contains('D')) {
            // Call some method to handle W+D
        } else {
            ...
        }
        keysCurrentlyDown.Add('W');
    } else if (e.KeyCode == Keys.D)
    {
        if (keysCurrentlyDown.Contains('W')) {
            // Call some method to handle W+D
        } else {
            ...
        }
        keysCurrentlyDown.Add('D');
    }
    ...
}
private void FrmGM_KeyUp(object sender, KeyEventArgs e) {            
    if (e.KeyCode == Keys.W) {
        keysCurrentlyDown.Remove('W');
    } else if (e.KeyCode == Keys.D) {
        keysCurrentlyDown.Remove('D');
    } ...
}

}

关于c# - 我如何将 2 个单独的按键识别为一个键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32172350/

相关文章:

c# - 如何使用从查询中提取的另一个对象的值设置 linq 查询中所有项目的属性?

c# - 如何从命令行 (msbuild) 使用 C# 7 代码构建 .csproj

c# - 如何在 MVC Controller 中实现数据访问层

C# - DatagridView 显示完整的标题文本

c# - 获取碰撞接触力

c# - base() 和 this() 构造函数最佳实践

c# - 使用 LINQ 在 Winforms 上查找控件?

c# - 在 Dapper 中使用 Protocol Buffer 模型时如何将 C# ticks 映射到 PostgreSQL 时间戳?

C# == 操作符具体是干什么的?

c# - 将@Html.ActionLink 转换为@Url.Action