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.
how to set key repeat delay?
Re: how to set key repeat delay?
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
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?
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
- zorg
- Party member
- Posts: 3468
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: how to set key repeat delay?
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.
However, if that's unsuitable for your needs, you can ignore that keyrepeat, and use the method pgimeno gave above.
Me and my stuff True 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.
Re: how to set key repeat delay?
Thank you all for the response, it's working. and really nice code!
Who is online
Users browsing this forum: Ahrefs [Bot] and 2 guests