trenmost's picture

Input help

Hi!

I'm writing a game, and i would like be able to get a list of the currently pressed key(s), with the opentk input. I tried the following:

 private void handle(object sender, OpenTK.Input.KeyboardKeyEventArgs e)
        {
            switch (state)
            {
                case 0: left = e.Key; state++; break;
                case 1: right = e.Key; this.DialogResult = System.Windows.Forms.DialogResult.OK; break;
                default: break;
            }
        }

Now i only need to register the handle method to an event, but i dont know to which event should i register to?

opentk.input.KeyBoardDevice has a KeyDown event, but i couldnt make an instance of that class, because it has no constructors. What should i do?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mOfl's picture

Are you using the GameWindow or the GLControl?

GameWindow ("this" is the GameWindow instance itself):

private void UpdateFrame(FrameEventArgs e) {
    if (this.Keyboard[Key.Escape]) {
         this.Exit();
 
        return;
    }
}

GLControl embedded in a Windows Form:

private void UpdateFrame() {
    if (System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.Escape)) {
        this.Close();
 
        return;
    }
}

For keyboard input, you do not have to implement any events, as everything you need is globally available. Notice that the "key listener" is implemented in the update function of the main loop and is called at the update rate of the application. For GameWindow, you specify this with the parameter (here target update rate is 60 times per second) when running it: this.Run(60.0);

trenmost's picture

that do work, but what i would like to do is a menu, where i can set the controls. So when i press 'S', then i get an S OpenTK.Input.Key back. I solved this by creating a huge switch () {} statement, which converts Windows.Forms.Keys to OpenTK.Input.Key, because i can read the exact character when using windows.forms.