Godot (as of version 4.4.1) doesn’t support the common alt+enter
shortcut convention used in games. https://github.com/godotengine/godot-proposals/issues/1983
Someone posted a workaround, but I did not think their workaround was very good, in part because it uses the Input
class instead of using the event
parameter for the _input
function. So, I came up with my own workaround:
extends Node
func _input(event) -> void:
if event.is_action_pressed("toggle_fullscreen"):
toggle_fullscreen()
static func toggle_fullscreen() -> void:
var mode := DisplayServer.window_get_mode()
match mode:
DisplayServer.WINDOW_MODE_WINDOWED:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
DisplayServer.WINDOW_MODE_MINIMIZED:
pass
DisplayServer.WINDOW_MODE_MAXIMIZED:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
DisplayServer.WINDOW_MODE_FULLSCREEN:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
Or, if you don’t want to configure an action:
extends Node
func _input(event: InputEvent) -> void:
if event is InputEventKey:
var key_event: InputEventKey = event
handle_key_event(key_event)
static func handle_key_event(event: InputEventKey) -> void:
if not event.pressed:
return
if event.alt_pressed and event.physical_keycode == KEY_ENTER:
toggle_fullscreen()
static func toggle_fullscreen() -> void:
var mode := DisplayServer.window_get_mode()
match mode:
DisplayServer.WINDOW_MODE_WINDOWED:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
DisplayServer.WINDOW_MODE_MINIMIZED:
pass
DisplayServer.WINDOW_MODE_MAXIMIZED:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
DisplayServer.WINDOW_MODE_FULLSCREEN:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
In either case, adding this script as a global autoload adds the shortcut convention to the game. I like to add it named as GlobalShortcut
.