Toggling Fullscreen with Escape

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
bigdevlover
Prole
Posts: 1
Joined: Sat Oct 22, 2016 2:38 am

Toggling Fullscreen with Escape

Post by bigdevlover »

I can't seem to figure out how I would toggle fullscreen with escape. This is the code I currently have in my love.udate function:

Code: Select all

function love.update (dt)
  if love.keyboard.isDown("d") then
    x = x +1
    y = y + 1
  end
  if love.keyboard.isDown("a") then
    x = x - 1
    y = y - 1
  end
  if love.keyboard.isDown("escape") then
    if fullscreen == false then
      love.window.setFullscreen(true, "desktop")
      fullscreen = true
    end
    if fullscreen == true then
      love.window.setFullscreen(false, "desktop")
      fullscreen = false
    end
  end
Currently, when I press escape the window maximizes and then minimizes very quickly. Am I doing something wrong or is this the wrong way to go about it?
drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

Re: Toggling Fullscreen with Escape

Post by drunken_munki »

I assume love.keyboard.isDown() is firing every update, even in the miliseconds when you release the key; which may account for your side-effect.

Try to use love.keypressed(key, scancode, repeating) which will fire once when the key is pressed: https://love2d.org/wiki/love.keypressed

Something like:

Code: Select all

function love.keypressed(key, scancode, repeating)
  if key == "escape" then
    if fullscreen == false then
      love.window.setFullscreen(true, "desktop")
      fullscreen = true
    end
    if fullscreen == true then
      love.window.setFullscreen(false, "desktop")
      fullscreen = false
    end
  end
end
Fuzzlix
Citizen
Posts: 60
Joined: Thu Oct 13, 2016 5:36 pm

Re: Toggling Fullscreen with Escape

Post by Fuzzlix »

bigdevlover wrote:I can't seem to figure out how I would toggle fullscreen with escape. This is the code I currently have in my love.udate function:

Code: Select all

function love.update (dt)
  if love.keyboard.isDown("d") then
    x = x +1
    y = y + 1
  end
  if love.keyboard.isDown("a") then
    x = x - 1
    y = y - 1
  end
  if love.keyboard.isDown("escape") then
    if fullscreen == false then
      love.window.setFullscreen(true, "desktop")
      fullscreen = true
    end
    if fullscreen == true then
      love.window.setFullscreen(false, "desktop")
      fullscreen = false
    end
  end
Currently, when I press escape the window maximizes and then minimizes very quickly. Am I doing something wrong or is this the wrong way to go about it?
Your escape button is still down when you enter love.update(dt) the second time. It seems to be the wrong idea to use love.keyboard.isDown("escape") for this scenario. I would think about using love.keypressed() instead.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Toggling Fullscreen with Escape

Post by ivan »

Code: Select all

function toggleFullscreen()
  local fs = love.window.getFullscreen()
  fs = not fs
  local rs = love.window.setFullscreen(fs, 'desktop')
  return rs and fs or nil
end
Returns nil if the toggle fails or a Boolean if the mode was toggled successfully.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Toggling Fullscreen with Escape

Post by zorg »

People, people!

Code: Select all

if fullscreen == false then
      love.window.setFullscreen(true, "desktop")
      fullscreen = true
end
if fullscreen == true then
      love.window.setFullscreen(false, "desktop")
      fullscreen = false
end
The above snippet amounts to this:

Code: Select all

if not a then a = true end
if a then a = false end -- a will always be true here, because of the above, so it'll be set back to false -in the same frame-!
Which is short for toggling a twice, back to what it was. :3
Both the op's code and drunken_munki's code had this... so yeah, use an else instead:

Code: Select all

function love.keypressed(key, scancode, repeating)
  if key == "escape" then
    if fullscreen == false then
      love.window.setFullscreen(true, "desktop")
      fullscreen = true
    else
      love.window.setFullscreen(false, "desktop")
      fullscreen = false
    end
  end
end
(Or use ivan's solution :3)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
UsualBasementKid
Prole
Posts: 1
Joined: Sun Jun 19, 2022 12:20 pm

Re: Toggling Fullscreen with Escape

Post by UsualBasementKid »

My setup for toggling fullscreen:

Code: Select all

-- In love.load(), 

love.window.setFullscreen(false) -- <- change this to true if you want your game to be fullscreen when you run
fullscreen = false -- <- change this to true if you want your game to be fullscreen when you run

-- In love.update(),

-- This is the important stuff, I kinda don't wanna explain but it works sooooo,

    -- Fullscreen / Not Fullscreen

    if fullscreen == true then
        love.window.setFullscreen(true)
    elseif fullscreen == false then
        love.window.setFullscreen(false)
    end

    function love.keypressed(k)
        if k == "escape" and fullscreen == false then
            fullscreen = true 
        elseif k == "escape" and fullscreen == true then
            fullscreen = false
        end
    end
   
Last edited by UsualBasementKid on Sun Jun 19, 2022 12:37 pm, edited 1 time in total.
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: Toggling Fullscreen with Escape

Post by GVovkiv »

UsualBasementKid wrote: Sun Jun 19, 2022 12:34 pm My setup for toggling fullscreen:

Code: Select all

-- In love.load(), 

love.window.setFullscreen(false) -- <- change this to true if you want your game to be fullscreen when you run
fullscreen = false -- <- change this to true if you want your game to be fullscreen when you run

-- In love.update(),

-- This is the important stuff, I kinda don't wanna explain but it works sooooo,

    -- Fullscreen / Not Fullscreen

    if fullscreen == true then
        love.window.setFullscreen(true)
    elseif fullscreen == false then
        love.window.setFullscreen(false)
    end

    function love.keypressed(k)
        if k == "escape" and fullscreen == false then
            fullscreen = true 
        elseif k == "escape" and fullscreen == true then
            fullscreen = false
        end
    end
   
Huh, why you check for key escape 2 times?
Why you elseif when you can just else?
And why not wrap it in function, so you can call this outside of love keypress?
And, most important, why necropost on like 6 year post?
nephele
Prole
Posts: 10
Joined: Sun Jun 19, 2022 8:06 am

Re: Toggling Fullscreen with Escape

Post by nephele »

In general you should only use keydown in love.update for things that should happen continuosly.

For one off things (as in you press and release the key for the action) love.keypressed or love.keyreleased is the right tool :)

This is how I would solve this problem:

Code: Select all

local fullscreen = love.window.getFullscreen()
love.keyreleased = function(key, _)
  if key == "escape" then
    fullscreen = not fullscreen
    love.window.setFullscreen(fullscreen, "desktop")
  end
end
User avatar
darkfrei
Party member
Posts: 1171
Joined: Sat Feb 08, 2020 11:09 pm

Re: Toggling Fullscreen with Escape

Post by darkfrei »

nephele wrote: Mon Jun 20, 2022 6:53 am In general you should only use keydown in love.update for things that should happen continuosly.

For one off things (as in you press and release the key for the action) love.keypressed or love.keyreleased is the right tool :)

This is how I would solve this problem:

Code: Select all

Fullscreen = love.window.getFullscreen() -- global
function love.keypressed (key, scancode) -- faster than key released
  if key == "f11" 
  and love.keyboard.isDown( "rctrl", "lctrl" )
  and not love.keyboard.isDown( "lalt", "ralt", "lshift", "rshift") then
    Fullscreen = not Fullscreen
    love.window.setFullscreen(Fullscreen, "desktop")
  end
end

The best solution! But set the Fullscreen as global and set fullscreen on Ctrl + F11, not on Escape :)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
marclurr
Party member
Posts: 102
Joined: Fri Apr 22, 2022 9:25 am

Re: Toggling Fullscreen with Escape

Post by marclurr »

Just to add yet another variation on the same thing :) , this is the toggle fullscreen implementation I have in my game:

Code: Select all

function toggleFullscreen()
    love.window.setFullscreen(not love.window.getFullscreen(), "desktop")
    updateDrawScaling()
end
It's triggered by a callback in my UI code, which its self is ultimately triggered by keypressed/keyreleased events. updateDrawScaling just figures out how much the drawing canvas must be scaled to fill the new window and it handles the case when the aspect ratios don't match (it's pretty inelegant though, and is driven by the window width ignoring the height).
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests