Swapping between fullscreen and windowed flashes rapidly between the two

So I have this issue where when I swap between fullscreen and windowed, the window rapidly transitions between them before sticking with one of them. It goes from fullscreen to windowed to fullscreen to windowed extremely fast. The code I have for this looks like the following:

// Make fullscreen or take from fullscreen to windowed
	if (check_key(GLFW_KEY_F) == GLFW_PRESS)
	{
		// Get the video mode of the monitor
		const GLFWvidmode* mode = glfwGetVideoMode(monitor);

		// Set the window to be fullscreen
		if (!fullscreen)
		{
			glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
		}
		// Set the window to be windowed
		else
		{
			glfwSetWindowMonitor(window, NULL, 0, 0, width, height, mode->refreshRate);
		}

		fullscreen = !fullscreen;
	}

where check_key is just a function that has a std::map<int,int> that returns the state of that key on that frame.The code above is called inside the render loop on each window update. I have also a callback function for size to set the new viewport:

static void framebuffer_size_callback_function(GLFWwindow* glfw_window, int window_width, int window_height)
{
	// set the new height and width of the window
	glViewport(0, 0, window_width, window_height);
}

Any idea what may be causing this?

From the information posted it looks like you are using the current state of the key instead of whether it has just been pressed. The state GLFW_PRESS means the key is currently pressed down, which will potentially be for several frames unless the user presses the key very quickly.

I would recommend performing toggles such as this based on a key callback which will only trigger once when pressed with GLFW_PRESS.

So I use a key callback to update the state of the key

static void key_callback_function(GLFWwindow* glfw_window, int key, int scancode, int action, int mods)
{
	update_input(key, action);
}

which then gets stored in this map

static std::map<int, int> input_handler;

void update_input(int key, int action)
{
	input_handler[key] = action;
}

int check_key(int key)
{
	const auto iterator = input_handler.find(key);
	if (iterator != input_handler.end())
		return iterator->second;
	else
		return 0;
}

Would it then be smarter to use this input handler for everything other than updating fullscreen?

Just a quick response to say do not store the value in a map, you will have the same problem. Simply set thr fullscreen state in the callback.

I will add a short example later if you need.

So that makes sense to me, but I want to be able to poll what keys are pressed without passing in a window since say my entity system shouldn’t know which window I’m in.

What would be better than storing in a map? I had an idea of using an event system and then call the specific events in the callback, but that seems like a lot of overhead.

How you handle this really depends on your application, however you need to be able to deal with state transitions (or events) such as the callback for a key being pressed or released, and the current state of a key such as you would store in a map (or more simply in an array since there are a finite set of keys).

An event system could be a good way to handle things, or if you don’t have many events you can code them directly in the callback.