raylib»Forums
2 posts
override escape key behavior

Hello! I made a little framework to wrap around Raylib this weekend and want to say congratulations on a really nice job with this library. Really easy to use and everything works as advertised as far as I can tell.

I just have a quick question about the the escape key behavior. My main loop uses a while (!WindowShouldClose() && m_Running) type of loop where m_Running can be set by actions in the game (ie, user clicks a quit button).

But I notice that WindowShouldClose is tied to both the close button on the window AND the user pressing the escape key. Is there any easy way to handle the escape key myself? In my experience, I usually want to do something specific with the escape key handling, and at the very least would like to pop up a "are you sure you want to close the window?" kind of thing in case the key is hit by accident. But also it can be used to close dialogs like an inventory window or something.

I'd like to just respond to the glfw window close event but not the escape key in the main loop. Can you advise any way to do that?

thanks!

Mārtiņš Možeiko
2445 posts / 2 projects
override escape key behavior
Edited by Mārtiņš Možeiko on

You could handle WindowShouldClose() explicitly:

bool userWantsToClose = false;

while (m_Running) {
  if (WindowShouldClose()) userWantsToClose = true;

  if (userWantsToClose) {
    // ... draw your UI asking user to close
    // ...
    if (userReallyWantsToClose) {
      m_Running = false;
      break; // or whatever you need to do
    }
  }
}

If you want to just disable ESC key, there's SetExitKey(key) function: https://github.com/raysan5/raylib/blob/master/src/raylib.h#L1082
Probably can set it to something impossible, like -1 to disable it completely.

2 posts
override escape key behavior
Replying to mmozeiko (#26050)

Perfect, thanks very much! I think SetExitKey works for me - very nice to find out that can be customized as needed.