Get time duration in key down event

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
herksaw
Prole
Posts: 3
Joined: Fri Jul 18, 2014 11:39 am

Get time duration in key down event

Post by herksaw »

Can I get the time of duration for keydown event? I just want to find a way to distinguish between keypressed and keydown event.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Get time duration in key down event

Post by Plu »

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.
User avatar
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

Post by zorg »

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 :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.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Get time duration in key down event

Post by Lafolie »

zorg wrote:you can do something like this (my original usage was that i needed to differentiate between 4 key states):
Why not just store the time when the key is pressed and use that to find the difference when the key is released?
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.
User avatar
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

Post by zorg »

Well then,

Code: Select all

keys = {}

function love.keypressed(key)
    keys[key] = os.clock()
end

function love.keyreleased(key)
    keys[key] = os.clock() - keys[key]
end
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

function getHeldTime(key)
	if love.keyboard.isDown(key) then
		return os.clock() - keys[key]
	else
		return keys[key]
	end
end
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.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Get time duration in key down event

Post by Lafolie »

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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 5 guests