If combining both tips above, here is the code that darkens the screen and terminates the program in a second and a half:
# Exit by ESC.
if event is InputEventKey:
if event.pressed and event.keycode == KEY_ESCAPE:
var screen_size
screen_size = get_viewport_rect().size
$ColorRect.size = screen_size
$ColorRect.show()
var tween: Tween = create_tween()
tween.tween_property($ColorRect, "modulate:a", 1.0, 1.5).from(0.0)
await tween.finished
get_tree().quit()