How do I Combine Keypresses?

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

How do I Combine Keypresses?

Post by GalacticPixelStudios » Wed Aug 07, 2013 3:21 pm

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

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

Re: How do I Combine Keypresses?

Post by DaedalusYoung » Wed Aug 07, 2013 3:48 pm

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

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

Re: How do I Combine Keypresses?

Post by Davidobot » Wed Aug 07, 2013 3:59 pm

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!

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

Re: How do I Combine Keypresses?

Post by DaedalusYoung » Wed Aug 07, 2013 4:58 pm

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.

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

Re: How do I Combine Keypresses?

Post by raidho36 » Wed Aug 07, 2013 5:39 pm

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

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

Re: How do I Combine Keypresses?

Post by Daniel Eakins » Wed Aug 07, 2013 6:01 pm


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

Re: How do I Combine Keypresses?

Post by GalacticPixelStudios » Wed Aug 07, 2013 10:39 pm

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 :brows:


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

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

Re: How do I Combine Keypresses?

Post by DaedalusYoung » Thu Aug 08, 2013 12:02 am

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?

Post by GalacticPixelStudios » Thu Aug 08, 2013 3:06 am

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

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests