how to set key repeat delay?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Lesommeil
Prole
Posts: 4
Joined: Tue Apr 19, 2022 11:09 am

how to set key repeat delay?

Post by Lesommeil »

Sorry for the simple question.
I want to set a short time delay (like 0.2 s) on keyRepeat, but I found the wiki it was removed, and I couldn't locate a substitute.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: how to set key repeat delay?

Post by darkfrei »

Code: Select all

timer = 0
timerDefault = 0.2

function love.update (dt)
	timer = timer + dt
	if timer > timerDefault then
		timer = timer - timerDefault 
		-- other code for running every 0.2 seconds.
	end
end
or

Code: Select all

timer = 0
timerDefault = 0.2

function love.update (dt)
	local keypressed_down  = love.keyboard.isScancodeDown('down')
	if keypressed_down  then
		timer = timer + dt
		if timer > timerDefault then
			timer = timer - timerDefault 
			-- other code for running every 0.2 seconds.
		end
	else -- key not pressed
		timer = 0
	end
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: how to set key repeat delay?

Post by pgimeno »

It's a bit more involved than that.

Code: Select all

-- Configuration area
local repeat_delay = 0.3    -- Delay until repeat starts
local repeat_period = 0.05  -- Time between repetitions once repeat starts

local use_scancode = true    -- Whether to use the key's scancode

-- Our timers
local timer_delay = -1
local timer_period = -1

local s = ""

-- Use this in place of love.keypressed:
local function key_event(key)
  -- example code: exit with escape; add the key to a string
  if key == "escape" then return love.event.quit() end
  s = s .. " " .. key
end

-- Keys that must not be autorepeated
local norepeat = {
  lshift = true, lctrl = true, lalt = true, numlock = true,
  rshift = true, rctrl = true, ralt = true, capslock = true,
}

function love.keypressed(key, scancode)
  if use_scancode then -- set to false to stop using scancodes
    key = scancode
  end
  -- Autorepeat only if not in norepeat
  if not norepeat[key] then
    lastkey = key
    timer_delay = repeat_delay
    timer_period = -1
    key_event(key)
  end
end

function love.keyreleased(key, scancode)
  if use_scancode then
    key = scancode
  end
  if key == lastkey then
    timer_delay = -1
    timer_period = -1
  end
end

function love.update(dt)
  if timer_delay >= 0 then
    timer_delay = timer_delay - dt
    if timer_delay < 0 then
      key_event(lastkey)
      timer_period = repeat_period + timer_delay
      while timer_period < 0 do
        key_event(lastkey)
        timer_period = timer_period + repeat_period
      end
    end
  elseif timer_period >= 0 then
    timer_period = timer_period - dt
    while timer_period < 0 do
      key_event(lastkey)
      timer_period = timer_period + repeat_period
    end
  end
end

-- Example love.draw: print the string
function love.draw()
  love.graphics.printf(s, 0, 0, love.graphics.getWidth())
end
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: how to set key repeat delay?

Post by zorg »

The "substitute" is in your OS' settings, since that's what löve now uses.
However, if that's unsuitable for your needs, you can ignore that keyrepeat, and use the method pgimeno gave above.
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.
Lesommeil
Prole
Posts: 4
Joined: Tue Apr 19, 2022 11:09 am

Re: how to set key repeat delay?

Post by Lesommeil »

Thank you all for the response, it's working. and really nice code!
Post Reply

Who is online

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