How to check if 2 keys are down?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
IAsep-TrixI
Citizen
Posts: 89
Joined: Mon Aug 12, 2013 4:22 am
Location: Philippines,Asia

How to check if 2 keys are down?

Post by IAsep-TrixI » Tue Apr 08, 2014 11:33 pm

I noticed that my player goes vertical if holding A and D, now i want to change the characters picture to a vertical picture if the 2 keys are down, but how do i detect if the 2 are down?
An agent of the free

check out the game I'm doing with Jkash!

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

Re: How to check if 2 keys are down?

Post by DaedalusYoung » Tue Apr 08, 2014 11:36 pm

Code: Select all

if love.keyboard.isDown('a') and love.keyboard.isDown('d') then
   --both keys are pressed
end
Assuming you're reading the keypresses in love.update.

davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: How to check if 2 keys are down?

Post by davisdude » Wed Apr 09, 2014 12:27 am

You can also do

Code: Select all

love.keyboard.isDown( 'a' and 'd' )
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim

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

Re: How to check if 2 keys are down?

Post by DaedalusYoung » Wed Apr 09, 2014 12:33 am

Can you? 'a' and 'd' just returns true, doesn't it (regardless of what strings you put in anyway), so how would love.keyboard.isDown check against just a boolean?

davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: How to check if 2 keys are down?

Post by davisdude » Wed Apr 09, 2014 12:51 am

It works. Of course, you would have to do if love.keyboard.isDown( 'a' and 'd' ) then, but yes, it does work.
Attachments
main.lua
Simple example
(372 Bytes) Downloaded 30 times
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim

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

Re: How to check if 2 keys are down?

Post by DaedalusYoung » Wed Apr 09, 2014 1:06 am

Hmm, that doesn't seem to work for me though. If I press just 'd' it already says "You did it!", it doesn't see I haven't pressed 'a'.

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How to check if 2 keys are down?

Post by bartbes » Wed Apr 09, 2014 6:57 am

"'a' and 'd'" returns 'd', so that's why that works. What he's confused with is that you can pass both to isDown and it will check if either is down.

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to check if 2 keys are down?

Post by Robin » Wed Apr 09, 2014 10:10 am

Just to be super-extra clear:

Code: Select all

love.keyboard.isDown( 'a' and 'd' )
is NOT a substitute for

Code: Select all

love.keyboard.isDown('a') and love.keyboard.isDown('d')
(instead, it's a weird way of saying

Code: Select all

love.keyboard.isDown( 'd' )
)

On the other hand,

Code: Select all

love.keyboard.isDown( 'a', 'd' )
IS a substitute for

Code: Select all

love.keyboard.isDown('a') or love.keyboard.isDown('d')
Help us help you: attach a .love.

User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: How to check if 2 keys are down?

Post by Roland_Yonaba » Wed Apr 09, 2014 2:29 pm

As a follow up to the great response of the Great Robin, I would just like to mention this part of PiL about logical operators, which is worth reading. ;)

davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: How to check if 2 keys are down?

Post by davisdude » Thu Apr 10, 2014 12:58 am

My bad, sorry for any confusion! Glad that this is cleared up.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests