Search code examples
c#keyopentk.net-standard-2.0eventargs

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


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.


Solution

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

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