Simultaneously press two buttons

Topics related to the API, programming discussions & questions, coding tips, bugs, etc. should go here.
Post Reply
User avatar
Kerby
Posts: 90
Joined: Wed Feb 06, 2013 6:02 pm

Simultaneously press two buttons

Post by Kerby »

Hi!
I have one small question. How to write the condition simultaneously pressing two buttons "A" and "left"? Maybe my question is stupid, but I tried different options ... simple is not working is not one!

Code: Select all

if (ReadJoypad(0)==BTN_A)
  {
   if (ReadJoypad(0)==BTN_LEFT)
    {..........

Code: Select all

if ((ReadJoypad(0)==BTN_A)&&(ReadJoypad(0)==BTN_LEFT))
  {.........

Code: Select all

if (ReadJoypad(0)==BTN_A&&BTN_LEFT)
 {.........
How easy to implement?
User avatar
uze6666
Site Admin
Posts: 4814
Joined: Tue Aug 12, 2008 9:13 pm
Location: Montreal, Canada
Contact:

Re: Simultaneously press two buttons

Post by uze6666 »

You were close... It's :

Code: Select all

if (ReadJoypad(0)==BTN_A | BTN_LEFT)
{.........} 
You must use a binary OR since the joy pad value is a bit field where each bit match a controller button.
User avatar
Kerby
Posts: 90
Joined: Wed Feb 06, 2013 6:02 pm

Re: Simultaneously press two buttons

Post by Kerby »

Oooh ... Thank you, so of course everything is working as it should! Only needed to add parentheses.

Code: Select all

if (ReadJoypad(0)==(BTN_A | BTN_LEFT))
{....
User avatar
D3thAdd3r
Posts: 3293
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: Simultaneously press two buttons

Post by D3thAdd3r »

Code: Select all

uint16_t oldpadstate,padstate;

//...in some input func
	oldpadstate = padstate;
	padstate = ReadJoypad(0);
	
	if(padstate & (BTN_A|BTN_B))//A and B pressed this frame?
		TriggerFx(10,255,true);
		
	if((padstate & BTN_X) && !(oldpadstate & BTN_X))//pushed this frame but not last?
		Triggerx(2,255,true);

I think most people are doing something like this too; using a variable to keep track of the buttons from last frame and the current joypad state of this frame instead of calling the function every time you want to check a button
User avatar
Janka
Posts: 214
Joined: Fri Sep 21, 2012 10:46 pm
Location: inside Out

Re: Simultaneously press two buttons

Post by Janka »

Maybe someone will count it as a shameless ad, but there's a simple and complete solution for evaluating the controllers:

Code: Select all

previous = current;
current = ReadJoypad(0);

held = current & previous;
pressed = current & (current ^ previous);
released = previous & (current ^ previous);
It sorts out pressed, held, released events so one can simply check for combinations afterwards.

Code: Select all

if ((BTN_A & pressed) && (BTN_LEFT & held)) { // Do what's to be done when A is pressed while Left is held }
User avatar
uze6666
Site Admin
Posts: 4814
Joined: Tue Aug 12, 2008 9:13 pm
Location: Montreal, Canada
Contact:

Re: Simultaneously press two buttons

Post by uze6666 »

Janka wrote:Maybe someone will count it as a shameless ad, but there's a simple and complete solution for evaluating the controllers:

Code: Select all

previous = current;
current = ReadJoypad(0);

held = current & previous;
pressed = current & (current ^ previous);
released = previous & (current ^ previous);
It sorts out pressed, held, released events so one can simply check for combinations afterwards.

Code: Select all

if ((BTN_A & pressed) && (BTN_LEFT & held)) { // Do what's to be done when A is pressed while Left is held }
That's the way, good refresher. I forgot about how simple and elegant that method is. For those interested, the approach is documented in the wiki.
User avatar
D3thAdd3r
Posts: 3293
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: Simultaneously press two buttons

Post by D3thAdd3r »

A bit cleaner for a bit more memory, seems to be the classic tradeoff in this hobby
User avatar
Kerby
Posts: 90
Joined: Wed Feb 06, 2013 6:02 pm

Re: Simultaneously press two buttons

Post by Kerby »

Thank you all for answering my silly question! Newbie ...
Post Reply