Trying to use KeyPress with arguments but don't know how to pass args correcly

123 Views Asked by At

I want the program to exit when I press Escape, the way it is right now, just close whenever a press anybutton.

Here is my code

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

I using https://github.com/ppy/osuTK this as reference in my project. Both KeyPress and KeyPressEventArgs Inherit from osuTK.Input

There is also this code bellow

Key.Escape

The Key also Inherit from osuTK.Input.

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

This code above doesn't work, but something close to that would be perfect.

2

There are 2 best solutions below

0
Nguyen Thanh On BEST ANSWER

You can try with this code according to KeyPressEventArgs.KeyChar:

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

KeyPressEventArgs has the KeyChar property. Use that to test what key was pressed:

if (e.KeyChar == (char)Keys.Return)
{
    e.Handled = true;
}