Search code examples
c#opentk

OpenTK does not find OpenTK.Input.Keyboard.GetState();


So i was trying to detect the keywords with OpenTK and in their tutorial gave this code:

protected override void OnUpdateFrame(FrameEventArgs args)
        {
            //Get the state of the keyboard this frame
            KeyboardState input = OpenTK.Input.Keyboard.GetState();

            if (input.IsKeyDown(Key.Escape))
            {
                Exit();
            }

            base.OnUpdateFrame(args);

        }

But what happens is that it gives an error at "Keyboard.GetState()". The error says: "The type or namespace name 'Keyboard' does not exist in the namespace 'OpenTK.Input' (are you missing an assembly reference?)". Or if i dont add the 'OpenTK.Input', it wont let me import anything.

I have the same problem with "Key.Escape" and "Exit()"


Solution

  • Okey i solved it using KeyboardState.IsKeyDown(Keys.Space). With this i can take the space input. If any other key is wanted you change the the "Keys" class function.

    An exemple with this would be:

    protected override void OnUpdateFrame(FrameEventArgs args)
            {
    
                if (KeyboardState.IsKeyDown(Keys.Space))
                {
                    Console.WriteLine("Somebody once told me");
                }else if (KeyboardState.IsKeyDown(Keys.Escape))
                {
                    this.Close();
                }
    
                base.OnUpdateFrame(args);
    
            }
    
    

    As you can see, the "Exit()" is replaced by "Close()" too!