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 »

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: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: How to check if 2 keys are down?

Post 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.
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 »

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: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: How to check if 2 keys are down?

Post 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?
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 »

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 90 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: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: How to check if 2 keys are down?

Post 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'.
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 »

"'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 »

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 »

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 »

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: Bing [Bot], Google [Bot] and 82 guests