Measure length of keystroke and print on screen

General discussion about LÖVE, Lua, game development, puns, and unicorns.
MrGrizzly
Prole
Posts: 7
Joined: Sun Aug 23, 2020 5:24 pm

Measure length of keystroke and print on screen

Post by MrGrizzly »

Is it possible to measure the length of time a key is pressed? For example, how long is the "m" key pressed? Once the key is released, then I would like to print that length of time on the screen.

Here is an example what I'm trying to do. I'd like the user to press and hold a key. The length of time pressed will determine the speed at which an object will move across the screen once the button is released.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Measure length of keystroke and print on screen

Post by zorg »

yes, you can use the keypressed and keyreleased callbacks to set / reset values in a table, and use love.keyboard.isDown in love.update to add the elapsed time (dt) to the appropriate table values.
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.
MrGrizzly
Prole
Posts: 7
Joined: Sun Aug 23, 2020 5:24 pm

Re: Measure length of keystroke and print on screen

Post by MrGrizzly »

Do you think you might be able to give me an example program that measures the time and prints it?
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Measure length of keystroke and print on screen

Post by zorg »

Nope but i am willing to write a snippet as an example :3

Code: Select all

local keys = {}
function love.keypressed(k,s)
    keys[s] = 0.0
end
function love.keyreleased(k,s)
    keys[s] = false
end
function love.update(dt)
    for k,v in pairs(keys) do
        if love.keyboard.isScancodeDown(k) then
            keys[k] = keys[k] + dt
        end
    end
end
function love.draw()
    local line = 0
    for k,v in pairs(keys) do
        love.graphics.print(k .. ': ' .. v .. ' seconds', 0, line)
        line = line + 12
    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
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Measure length of keystroke and print on screen

Post by ivan »

Zorg is right but I think the code can be simplified by using love.timer or os.clock:

Code: Select all

local keys = {}
function love.keypressed(k,s)
    keys[s] = love.timer.getTime()
end
function love.keyreleased(k,s)
    keys[s] = nil
end
function love.draw()
    local now = love.timer.getTime()
    local line = 0
    for k,v in pairs(keys) do
        love.graphics.print(k .. ': ' .. (now - v) .. ' seconds', 0, line)
        line = line + 12
    end
end
Last edited by ivan on Tue Aug 25, 2020 3:21 pm, edited 1 time in total.
MrGrizzly
Prole
Posts: 7
Joined: Sun Aug 23, 2020 5:24 pm

Re: Measure length of keystroke and print on screen

Post by MrGrizzly »

Thanks Zorg for the code and it works fine but when a key is released it shows this error
main.lua:18: attempt to concatenate local 'v' (a boolean value)
main.lua:18 is the print line in love.draw
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Measure length of keystroke and print on screen

Post by zorg »

yes, because on release i set it to false, a boolean, and you can't concatenate that to a string (unless you do tostring(v)), that said, ivan's solution is indeed more simple.
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.
MrGrizzly
Prole
Posts: 7
Joined: Sun Aug 23, 2020 5:24 pm

Re: Measure length of keystroke and print on screen

Post by MrGrizzly »

Oh ok, thank you! Also is there a way that it will only measure the time for certain keys?
MrGrizzly
Prole
Posts: 7
Joined: Sun Aug 23, 2020 5:24 pm

Re: Measure length of keystroke and print on screen

Post by MrGrizzly »

My only problem now is the fact that where i need the code to be is in a class. How exactly would i implement your code into this:
This is names Player1.lua and it is a class

Player1 = Class{}

local keys = {
['q'] = key
}

function Player1:init(x, y, width, height)
self.x = x
self.y = y
self.width = width
self.height = height
self.speed = 10

self.dy = 0

self.texture = love.graphics.newImage('graphics/Player_1.png')

self.frames = {}

self.currentFrame = nil

self.state = 'empty'

self.animation = {
['empty'] = Animation({
texture = self.texture,
frames = {
love.graphics.newQuad(0, 0, 36, 32, self.texture:getDimensions())
}
}),
['drawn0'] = Animation({
texture = self.texture,
frames = {
love.graphics.newQuad(360, 0, 36, 32, self.texture:getDimensions())
}
}),
['drawn1'] = Animation({
texture = self.texture,
frames = {
love.graphics.newQuad(324, 0, 36, 32, self.texture:getDimensions())
}
}),
['drawn2'] = Animation({
texture = self.texture,
frames = {
love.graphics.newQuad(288, 0, 36, 32, self.texture:getDimensions())
}
}),
['drawn3'] = Animation({
texture = self.texture,
frames = {
love.graphics.newQuad(252, 0, 36, 32, self.texture:getDimensions())
}
}),
['drawn4'] = Animation({
texture = self.texture,
frames = {
love.graphics.newQuad(216, 0, 36, 32, self.texture:getDimensions())
}
}),
['drawn5'] = Animation({
texture = self.texture,
frames = {
love.graphics.newQuad(180, 0, 36, 32, self.texture:getDimensions())
}
}),
['drawn6'] = Animation({
texture = self.texture,
frames = {
love.graphics.newQuad(144, 0, 36, 32, self.texture:getDimensions())
}
}),
['drawn7'] = Animation({
texture = self.texture,
frames = {
love.graphics.newQuad(108, 0, 36, 32, self.texture:getDimensions())
}
}),
['drawn8'] = Animation({
texture = self.texture,
frames = {
love.graphics.newQuad(72, 0, 36, 32, self.texture:getDimensions())
}
}),
['drawn9'] = Animation({
texture = self.texture,
frames = {
love.graphics.newQuad(36, 0, 36, 32, self.texture:getDimensions())
}
})
}

self.animation = self.animation['empty']
self.currentFrame = self.animation:getCurrentFrame()

self.behaviors = {
['empty'] = function(dt)
-- Here I would like to measure the time
-- the 'q' key is being held down
-- and test for when it is released
end
}


end

function Player1:update(dt)
self.behaviors[self.state](dt)
self.animation:update(dt)
self.currentFrame = self.animation:getCurrentFrame()

if self.dy < 0 then
self.y = math.max(0, self.y + self.dy * dt)
elseif self.dy > 0 then
self.y = math.min(VIRTUAL_HEIGHT - 32, self.y + self.dy * dt)
end
end

function Player1:render()
love.graphics.draw(self.texture, self.x, self.y)
end




thanks a lot!
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: Measure length of keystroke and print on screen

Post by mr_happy »

ivan wrote: Mon Aug 24, 2020 6:10 am ...
function love.keypressed(k,s)
keys[s] = love.timer.getTime()
end
function love.keypressed(k,s)
keys[s] = nil
end
...
Just spotted a typo for anyone interested: the second keypressed(k, s) should read keyreleased(k, s)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], togFox and 206 guests