Page 1 of 1

How to check if 2 keys are down?

Posted: Tue Apr 08, 2014 11:33 pm
by IAsep-TrixI
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?

Re: How to check if 2 keys are down?

Posted: Tue Apr 08, 2014 11:36 pm
by DaedalusYoung

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.

Re: How to check if 2 keys are down?

Posted: Wed Apr 09, 2014 12:27 am
by davisdude
You can also do

Code: Select all

love.keyboard.isDown( 'a' and 'd' )

Re: How to check if 2 keys are down?

Posted: Wed Apr 09, 2014 12:33 am
by DaedalusYoung
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?

Re: How to check if 2 keys are down?

Posted: Wed Apr 09, 2014 12:51 am
by davisdude
It works. Of course, you would have to do if love.keyboard.isDown( 'a' and 'd' ) then, but yes, it does work.

Re: How to check if 2 keys are down?

Posted: Wed Apr 09, 2014 1:06 am
by DaedalusYoung
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'.

Re: How to check if 2 keys are down?

Posted: Wed Apr 09, 2014 6:57 am
by bartbes
"'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.

Re: How to check if 2 keys are down?

Posted: Wed Apr 09, 2014 10:10 am
by Robin
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')

Re: How to check if 2 keys are down?

Posted: Wed Apr 09, 2014 2:29 pm
by Roland_Yonaba
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. ;)

Re: How to check if 2 keys are down?

Posted: Thu Apr 10, 2014 12:58 am
by davisdude
My bad, sorry for any confusion! Glad that this is cleared up.