Detecting multiple button presses

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
omlette
Posts: 5
Joined: Wed Mar 16, 2005 7:58 am

Detecting multiple button presses

Post by omlette »

What's the correct way to detect if a user is pressing more than one button? I've been using the following method, which only seems to work if one button is pressed:

Code: Select all

sceCtrlPeekBufferPositive(&paddata, 1);
if(paddata.Buttons & PSP_CTRL_blah & PSP_CTRL_blah2)
{
...
}
Thanks
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

You have a couple of options, all really down to preference really. I use the following in one of my projects:

Code: Select all

if (pad.Buttons & PSP_CTRL_UP && pad.Buttons & PSP_CTRL_TRIANGLE)
omlette
Posts: 5
Joined: Wed Mar 16, 2005 7:58 am

Post by omlette »

Thank you :)
Aion
Posts: 40
Joined: Mon Jul 24, 2006 10:58 pm
Location: Montreal

Post by Aion »

Code: Select all

u32 uWantedKeys;
uWantedKeys = PSP_CTRL_UP | PSP_CTRL_TRIANGLE; //As many other as you want

if( (pad.Buttons & uWantedKeys)  == uWantedKeys )
   // Do whatever
Or

Code: Select all

u32 uWantedKeys;
uWantedKeys = PSP_CTRL_UP;
uWantedKeys |= PSP_CTRL_TRIANGLE; 
//... As many other as you want

if( (pad.Buttons & uWantedKeys)  == uWantedKeys )
   // Do whatever
Dremth
Posts: 14
Joined: Fri Jul 15, 2005 9:41 am
Location: Round Rock, TX

Post by Dremth »

Neither of those worked for me. I think it may have something to do with the: "u32". What is u32?
Aion wrote:

Code: Select all

u32 uWantedKeys;
uWantedKeys = PSP_CTRL_UP | PSP_CTRL_TRIANGLE; //As many other as you want

if( (pad.Buttons & uWantedKeys)  == uWantedKeys )
   // Do whatever
Or

Code: Select all

u32 uWantedKeys;
uWantedKeys = PSP_CTRL_UP;
uWantedKeys |= PSP_CTRL_TRIANGLE; 
//... As many other as you want

if( (pad.Buttons & uWantedKeys)  == uWantedKeys )
   // Do whatever
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

Code: Select all

#define BOTH_UP_AND_TRIANGLE_PRESSED (PSP_CTRL_UP|PSP_CTRL_TRIANGLE)

if ((pad.Buttons & BOTH_UP_AND_TRIANGLE_PRESSED) == BOTH_UP_AND_TRIANGLE_PRESSED)
{
  // do whatever you want when both UP and TRIANGLE are pressed
}
Aion's examples are not bad, but to create a variable to keep a constant is not something I would expect for such trivial thing, especially if you don't use before or after u32 a "const" directive.

usually u32 is declared that way :

Code: Select all

typedef unsigned int u32;
Dremth
Posts: 14
Joined: Fri Jul 15, 2005 9:41 am
Location: Round Rock, TX

Post by Dremth »

Ok. Thanks a bunch!
Aion
Posts: 40
Joined: Mon Jul 24, 2006 10:58 pm
Location: Montreal

Post by Aion »

I wouldn't usually store it in a variable, but I wanted the code to be as clear as possible for the example.
Post Reply