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.
Measure length of keystroke and print on screen
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Measure length of keystroke and print on screen
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 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: Measure length of keystroke and print on screen
Do you think you might be able to give me an example program that measures the time and prints it?
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Measure length of keystroke and print on screen
Nope but i am willing to write a snippet as an example
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 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: Measure length of keystroke and print on screen
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.
Re: Measure length of keystroke and print on screen
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
main.lua:18: attempt to concatenate local 'v' (a boolean value)
main.lua:18 is the print line in love.draw
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Measure length of keystroke and print on screen
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 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: Measure length of keystroke and print on screen
Oh ok, thank you! Also is there a way that it will only measure the time for certain keys?
Re: Measure length of keystroke and print on screen
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!
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!
Who is online
Users browsing this forum: No registered users and 13 guests