Page 1 of 1
how to set key repeat delay?
Posted: Tue Apr 19, 2022 11:28 am
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.
Re: how to set key repeat delay?
Posted: Tue Apr 19, 2022 12:22 pm
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
Re: how to set key repeat delay?
Posted: Wed Apr 20, 2022 12:30 am
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
Re: how to set key repeat delay?
Posted: Wed Apr 20, 2022 6:47 am
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.
Re: how to set key repeat delay?
Posted: Wed Apr 20, 2022 8:16 am
by Lesommeil
Thank you all for the response, it's working. and really nice code!