Get time duration in key down event
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Get time duration in key down event
Can I get the time of duration for keydown event? I just want to find a way to distinguish between keypressed and keydown event.
Re: Get time duration in key down event
Not by default. You'd have to measure it yourself.
But why do you want to distuinguish? They're two different functions, they can't really be confused.
But why do you want to distuinguish? They're two different functions, they can't really be confused.
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Get time duration in key down event
you can do something like this (my original usage was that i needed to differentiate between 4 key states):
Code: Select all
keys = {}
keyt = {}
function love.update(dt)
for k,v in pairs(keys) do
if v == 'pressed' and love.keyboard.isDown(k) then v = 'held' else
if v == 'released' and not love.keyboard.isDown(k) then v = 'free' end
-- and if you want to record how long the key is being held down, then:
if v == 'held' then
if keyt[k] ~= nil then
keyt[k] = keyt[k] + 1 -- or dt if you want time instead of ticks
else
keyt[k] = 1 -- or dt, see above.
end
end
end
function love.keypressed(key)
-- i'm fairly certain i had at least a decent reason for doing the thing below this way, else a simple keys[key] = 'pressed' would suffice.
if keys[key] == 'pressed' then
keys[key] = 'held'
else
keys[key] = 'pressed'
end
end
function love.keyreleased(key)
-- i'm fairly certain i had at least a decent reason for doing the thing below this way, else a simple keys[key] = 'released' would suffice.
if keys[key] == 'released' then
keys[key] = 'free'
else
keys[key] = 'released'
end
end
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: Get time duration in key down event
Why not just store the time when the key is pressed and use that to find the difference when the key is released?zorg wrote:you can do something like this (my original usage was that i needed to differentiate between 4 key states):
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Get time duration in key down event
Well then,
Though you'd need to check for the key to not be pressed since this only gives back the correct held time then...unless you use something like this:
Code: Select all
keys = {}
function love.keypressed(key)
keys[key] = os.clock()
end
function love.keyreleased(key)
keys[key] = os.clock() - keys[key]
end
Code: Select all
function getHeldTime(key)
if love.keyboard.isDown(key) then
return os.clock() - keys[key]
else
return keys[key]
end
end
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: Get time duration in key down event
See how little code there is compared to the first version?
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], slime and 1 guest