Skip to content

Keyboard events stops working after few Window Create/Dispose calls. #1500

Closed
@MarLupin

Description

@MarLupin

Summary

It seems that in a scenario where we create and dispose a Window several times, at some point the keyboard event trigger stop working.
I pointed this out when transferring several OpenGL examples into single project, where individual examples runs from the console menu.
I'm not entirely sure if this is a bug in Windowing/Input, or maybe I'm doing something wrong ?

Steps to reproduce

  • Platform: Desktop
  • Framework Version: .NET 7
  • API: N/A

Below is an extracted code that shows the problem (console app).

  • When I use the KeyDown event to close the window, after few calls this stops working.
  • But when checking IsKeyPressed, closing works every time.

I noticed too, that when calling the ImGui Demo that way, keyboard events stop being passed to the controller, while mouse events continue to work.

Example

using Silk.NET.Windowing;
using Silk.NET.Input;

while (true)
{
    Console.WriteLine("Press any key to open window");
    Console.ReadKey(true);
    OpenWindow_WithEvent(); // <-- there is a problem
    // OpenWindow_WithoutEvent(); // <-- this is fine
    while (Console.KeyAvailable) Console.ReadKey(true);
    Console.WriteLine($"Window closed\n");
}

// Escape KeyDown event stops working after few calls.
static void OpenWindow_WithEvent()
{
    var window = Window.Create(WindowOptions.Default);
    window.Initialize();
    var input = window.CreateInput();
    Console.WriteLine("Window opened, press Escape key to close");
    input.Keyboards[0].KeyDown += (IKeyboard a1, Key a2, int a3)
       => { Console.WriteLine(a2); if (a2 == Key.Escape) window.Close(); };
    window.Run();
    input.Dispose();
    window.Dispose();
}

// Escape check by IsKeyPressed works every time.
static void OpenWindow_WithoutEvent()
{
    var window = Window.Create(WindowOptions.Default);
    window.Initialize();
    var input = window.CreateInput();
    Console.WriteLine("Window opened, press Escape key to close");
    window.Run(() => {
        if (input.Keyboards[0].IsKeyPressed(Key.Escape)) window.Close();
        window.DoEvents();
        window.SwapBuffers();
    });
    input.Dispose();
    window.Dispose();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions