サンプルのメイン部分抜粋
#define printf pspDebugScreenPrintf
int main(void)
{
//コントローラーの状態を保存する構造体
SceCtrlData pad;
pspDebugScreenInit();
SetupCallbacks();
sceCtrlSetSamplingCycle(0);
//アナログパットの方つかうよ
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
while(1){
pspDebugScreenSetXY(0, 2);
//押されてるボタン調べる
sceCtrlReadBufferPositive(&pad, 1);
//アナログスティックのx座標
printf("Analog X = %d ", pad.Lx);
//アナログのy座標
printf("Analog Y = %d \n", pad.Ly);
if (pad.Buttons != 0){
if (pad.Buttons & PSP_CTRL_SQUARE){
//四角ボタン押されてるときの処理
printf("Square pressed \n");
}
if (pad.Buttons & PSP_CTRL_TRIANGLE){
//三角ボタン押されてるときの処理 以下同じ
printf("Triangle pressed \n");
}
if (pad.Buttons & PSP_CTRL_CIRCLE){
printf("Cicle pressed \n");
}
if (pad.Buttons & PSP_CTRL_CROSS){
printf("Cross pressed \n");
}
if (pad.Buttons & PSP_CTRL_UP){
printf("Up pressed \n");
}
if (pad.Buttons & PSP_CTRL_DOWN){
printf("Down pressed \n");
}
if (pad.Buttons & PSP_CTRL_LEFT){
printf("Left pressed \n");
}
if (pad.Buttons & PSP_CTRL_RIGHT){
printf("Right pressed \n");
}
if (pad.Buttons & PSP_CTRL_START){
printf("Start pressed \n");
}
if (pad.Buttons & PSP_CTRL_SELECT){
printf("Select pressed \n");
}
if (pad.Buttons & PSP_CTRL_LTRIGGER){
printf("L-trigger pressed \n");
}
if (pad.Buttons & PSP_CTRL_RTRIGGER){
printf("R-trigger pressed \n");
}
}
}
sceKernelExitGame();
return 0;
}
}
typedef struct SceCtrlData {
/** The current read frame. */
unsigned int TimeStamp;
/** Bit mask containing zero or more of ::PspCtrlButtons. */
unsigned int Buttons;
/** Analogue stick, X axis. */
unsigned char Lx;
/** Analogue stick, Y axis. */
unsigned char Ly;
/** Reserved. */
unsigned char Rsrv[6];
} SceCtrlData;
の構造体のメンバbuttonsにボタン押下情報が入っています。
各ビットに押されてるか押されてないかの情報がはいってます。
unsigned intで32bitで32個分のキーの情報が入れられますが、
enum PspCtrlButtonsによると22個分しか使われていないようです。
(実際はわかりません。)
たとえばセレクトボタンだけが押されると、
00000000000000000000000000000001
16進数だと0x00000001
となります。
セレクトとスタートだと
00000000000000000000000000001001
16進数だと0x00000009
となります。
enum PspCtrlButtons
{
/** Select button. */
PSP_CTRL_SELECT = 0x000001,
/** Start button. */
PSP_CTRL_START = 0x000008,
/** Up D-Pad button. */
PSP_CTRL_UP = 0x000010,
/** Right D-Pad button. */
PSP_CTRL_RIGHT = 0x000020,
/** Down D-Pad button. */
PSP_CTRL_DOWN = 0x000040,
/** Left D-Pad button. */
PSP_CTRL_LEFT = 0x000080,
/** Left trigger. */
PSP_CTRL_LTRIGGER = 0x000100,
/** Right trigger. */
PSP_CTRL_RTRIGGER = 0x000200,
/** Triangle button. */
PSP_CTRL_TRIANGLE = 0x001000,
/** Circle button. */
PSP_CTRL_CIRCLE = 0x002000,
/** Cross button. */
PSP_CTRL_CROSS = 0x004000,
/** Square button. */
PSP_CTRL_SQUARE = 0x008000,
/** Home button. */
PSP_CTRL_HOME = 0x010000,
/** Hold button. */
PSP_CTRL_HOLD = 0x020000,
/** Music Note button. */
PSP_CTRL_NOTE = 0x800000,
/** Screen button. */
PSP_CTRL_SCREEN = 0x400000,
/** Volume up button. */
PSP_CTRL_VOLUP = 0x100000,
/** Volume down button. */
PSP_CTRL_VOLDOWN = 0x200000,
/** Wlan switch up. */
PSP_CTRL_WLAN_UP = 0x040000,
/** Remote hold position. */
PSP_CTRL_REMOTE = 0x080000,
/** Disc present. */
PSP_CTRL_DISC = 0x1000000,
/** Memory stick present. */
PSP_CTRL_MS = 0x2000000,
};
で論理積演算で判定。
//buttunsのセレクトキーのbitが1ならば
//この論理積計算は1になるので判定式は真でifの中が実行される
if(pad.Buttuns&PSP_CTRL_SELECT){
...
}
セレクトとスタート
if((pad.Buttuns&(PSP_CTRL_START|PSP_CTRL_SELECT))==(PSP_CTRL_START|PSP_CTRL_SELECT)){
..
}
簡単な説明でした。
間違ってたらだれか直してね。
最終更新:2008年12月12日 21:13