尝试使用带参数的KeyPress,但不知道如何正确传递args

问题描述 投票:0回答:2

我希望程序在我按Escape时退出,就像现在一样,只要按下任何按钮就关闭。

这是我的代码

game.KeyPress += (sender, e) => { game.Exit(); };

我在我的项目中使用https://github.com/ppy/osuTK作为参考。 KeyPress和KeyPressEventArgs都是从osuTK.Input继承的

下面还有这个代码

Key.Escape

密钥也继承自osuTK.Input。

game.KeyPress<KeyPressEventArgs<Key.Escape>> += (sender, e) => { game.Exit(); };

上面的代码不起作用,但接近这个代码是完美的。

c# key opentk .net-standard-2.0 eventargs
2个回答
0
投票

您可以根据KeyPressEventArgs.KeyChar尝试使用此代码:

game.KeyPress += (sender, eventArgs) => {
    if (eventArgs.KeyChar == (char)Keys.Escape) {
        // TODO
    }
};

0
投票

KeyPressEventArgs具有KeyChar属性。用它来测试按下了什么键:

if (e.KeyChar == (char)Keys.Return)
{
    e.Handled = true;
}
© www.soinside.com 2019 - 2024. All rights reserved.