## How do I Combine Keypresses?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
GalacticPixelStudios
Prole
Posts: 7
Joined: Sun Nov 25, 2012 4:19 am

### How do I Combine Keypresses?

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

-GalacticPixelStudios

DaedalusYoung
Party member
Posts: 402
Joined: Sun Jul 14, 2013 8:04 pm

### Re: How do I Combine Keypresses?

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


Davidobot
Party member
Posts: 1150
Joined: Sat Mar 31, 2012 5:18 am
Location: Game-Dev. Land
Contact:

### Re: How do I Combine Keypresses?

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

My library:
LovelyMoon

Check out my current project:
Raycaster

Oh hey, I have a website now!

DaedalusYoung
Party member
Posts: 402
Joined: Sun Jul 14, 2013 8:04 pm

### Re: How do I Combine Keypresses?

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.

raidho36
Party member
Posts: 2052
Joined: Mon Jun 17, 2013 12:00 pm

### Re: How do I Combine Keypresses?

Yes, that's true. You will have to explicitly use isDown for every single AND key, and then manually AND them.

Daniel Eakins
Citizen
Posts: 99
Joined: Thu Jul 18, 2013 9:14 pm
Location: France

### Re: How do I Combine Keypresses?

GalacticPixelStudios
Prole
Posts: 7
Joined: Sun Nov 25, 2012 4:19 am

### Re: How do I Combine Keypresses?

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.
-GalacticPixelStudios

DaedalusYoung
Party member
Posts: 402
Joined: Sun Jul 14, 2013 8:04 pm

### Re: How do I Combine Keypresses?

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


GalacticPixelStudios
Prole
Posts: 7
Joined: Sun Nov 25, 2012 4:19 am

### Re: How do I Combine Keypresses?

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
-GalacticPixelStudios

### Who is online

Users browsing this forum: No registered users and 17 guests