I tried with assigned F11 key on "fullscr" like:

if Input.is_action_just_pressed("fullscr"):
	OS.set_window_fullscreen = !OS.window_fullscreen

but it just instantly switches back and forth in Godot 3.3, when I press the assigned key once. :| Any advices?

'set_window_fullscreen' is a setter function, so would be called like: OS.set_window_fullscreen(!OS.window_fullscreen)

Alternatively if your preference is to assign the variable directly: OS.window_fullscreen = !OS.window_fullscreen

Not sure if setter functions have any kind of performance benefit, I tend to update directly out of habit.

Edit: I did test this in a blank project before posting, so it not working under other conditions would be due to other specifics of the project environment/usage.

@Bimbam said: 'set_window_fullscreen' is a setter function, so would be called like: OS.set_window_fullscreen(!OS.window_fullscreen)

Alternatively if your preference is to assign the variable directly: OS.window_fullscreen = !OS.window_fullscreen

Not sure if setter functions have any kind of performance benefit, I tend to update directly out of habit.

Tried like this too, but I still only get instant flip-flop from full screen back to windowed mode, when I press the assigned key.

I'd check to see if it's the case that the condition is being returned as true twice by adding something like print ("fullscreen key pressed") and then looking to see if for a single press you are getting two print statements.

One thing you could try is to add a small timer so you can only change resolution once every half second or so. Something like this:

var fullscreen_timer = 0
func _process(delta):
	if fullscreen_timer > 0:
		fullscreen_timer -= delta
	else:
		if Input.is_action_just_pressed("fullscr"):
			OS.window_fullscreen = !OS.window_fullscreen
			fullscreen_timer = 0.5

Ok, so I added variable

var rb = 0

and than i did this inside func _process(delta):

	if Input.is_action_just_pressed("fullscr"):
		rb = rb + 1
		if rb == 1:
			OS.set_window_fullscreen(true)
		if rb == 2:
			OS.set_window_fullscreen(false)
			rb = 0

and now it works :)

Thank you for other answers too!

not sure whats happening here, i use that code everyday and works good. check if you write the code twice in other script or instance etc. and thats the back and forth reason.

anyways, u could use this, u dont need variable

OS.window_fullscreen = true

OS.window_fullscreen = false

No one has mentioned this (even though the OP fixed his problem with a "flag" variable):

It depends on how you intercept the signals. If you are handling it through _input() or _gui_input(), for example, you will get both KEY DOWN and KEY UP events for a single key press.

Additionally, depending on your system settings, you might even be getting KEY REPEAT events — which to you will look like multiple KEY DOWNs.

Unfortunately from the GDScript end of things you must handle this through careful consideration of which event you will observe and using "flag" variables or watching the time when and event is fired.

Hope this helps.

Toggles should be done in the _input(event) function with event.is_action_pressed() rather than in _process(delta). You can add an input action using the Project Settings' input map manager. It's good practice to avoid hardcoding keys :)

a year later