Page 1 of 1
How do I Combine Keypresses?
Posted: Wed Aug 07, 2013 3:21 pm
by GalacticPixelStudios
To start, I have a relative understanding of "love.keyboard.isDown" as well as "love.keypressed(key, unicode)"
However, what still escapes me is, how to
combine key presses.
For Example:
lctrl + r
or
lctrl + click
I'm sure that I am overlooking something, so if any of you can post a quick answer; I would really appreciate it!
And if you guys need a code reference of what I'm thinking of, this is what I've got... It obviously doesn't work
Code: Select all
function love.keypressed(key, unicode)
if key == "ctrl" and key == "r" then
playerX = 'numeric value'
playerY = 'numeric Value'
end
Re: How do I Combine Keypresses?
Posted: Wed Aug 07, 2013 3:48 pm
by DaedalusYoung
that doesn't work like that, because
key in love.keypressed only ever holds 1 value: that of the last pressed key.
To do this, use
love.keyboard.isDown, which you can call in the love.update function.
Something like this:
Code: Select all
if love.keyboard.isDown('lctrl') and love.keyboard.isDown('r') then
-- do this
end
Re: How do I Combine Keypresses?
Posted: Wed Aug 07, 2013 3:59 pm
by Davidobot
DaedalusYoung wrote:that doesn't work like that, because
key in love.keypressed only ever holds 1 value: that of the last pressed key.
To do this, use
love.keyboard.isDown, which you can call in the love.update function.
Something like this:
Code: Select all
if love.keyboard.isDown('lctrl') and love.keyboard.isDown('r') then
-- do this
end
Or this, since love.keyboard.isDown accepts multiple keys:
Code: Select all
if love.keyboard.isDown('lctrl', 'r') then
-- do this
end
Re: How do I Combine Keypresses?
Posted: Wed Aug 07, 2013 4:58 pm
by DaedalusYoung
Yes, but does that return true if all or if any of those keys are down? In other words, does it act like an and or like an or?
The way I understood from the wiki, it's an or, so love.keyboard.isDown('lctrl', 'r') is true if either ltcrl or r is pressed, not necessarily both at the same time.
Re: How do I Combine Keypresses?
Posted: Wed Aug 07, 2013 5:39 pm
by raidho36
Yes, that's true. You will have to explicitly use isDown for every single AND key, and then manually AND them.
Re: How do I Combine Keypresses?
Posted: Wed Aug 07, 2013 6:01 pm
by Daniel Eakins
Re: How do I Combine Keypresses?
Posted: Wed Aug 07, 2013 10:39 pm
by GalacticPixelStudios
Thanks for the input, guys, I actually discovered the "
love.keypressed.isDown", on my own, before I came back online today. Although, there are two things I want to say about it.
1. Yes, it gets the Job done!
2. It must be under
love.update(dt) to work; Thus causing the callback function to simulate multiple key presses simultaneously.
-- That is what I'm trying to avoid
I want the player to
MEAN his/her spawn reset(ctrl+r), not to find out that holding ctrl+r+w[asd] will create an infinite loop of teleporting.
Re: How do I Combine Keypresses?
Posted: Thu Aug 08, 2013 12:02 am
by DaedalusYoung
You can do that by introducing an extra variable.
Code: Select all
function love.update(dt)
if love.keyboard.isDown('r') and love.keyboard.isDown('lctrl') and not hasRespawned then
hasRespawned = true -- now this chunk will not execute next frame
-- do respawn code
end
end
function love.keyreleased(key)
if key == 'r' and hasRespawned then -- so when the player releases the r key after respawning
hasRespawned = false -- or nil, so the player can respawn again as soon as he enters the key combo again
end
end
Re: How do I Combine Keypresses?
Posted: Thu Aug 08, 2013 3:06 am
by GalacticPixelStudios
DaedalusYoung wrote:You can do that by introducing an extra variable.
Code: Select all
function love.update(dt)
if love.keyboard.isDown('r') and love.keyboard.isDown('lctrl') and not hasRespawned then
hasRespawned = true -- now this chunk will not execute next frame
-- do respawn code
end
end
function love.keyreleased(key)
if key == 'r' and hasRespawned then -- so when the player releases the r key after respawning
hasRespawned = false -- or nil, so the player can respawn again as soon as he enters the key combo again
end
end
I plugged in this code and at first it didn't work, but i figured out my real problem. Although this extra variable works, it's actually unnecessary; Thanks for the input though
What my problem is, and for anyone who runs into this in the future, I was using "
elseif" statements under "
love.keyboard.isDown" this caused it to
overide your current keypress with your new keypress.
For example:
Hold '
a' = print '
a'
Add '
b' while still holding '
a' = print
'b'
To correct this, I simply turned each "
elseif" statement, to their own little "
if" statement; I dont know if this really helps optimization though, but it functions properly now