View unanswered posts | View active topics It is currently Sun Oct 22, 2017 4:39 pm



Reply to topic  [ 2 posts ] 
 KeysDownRepeat(); Can't use more than once? 
Author Message

Joined: Thu May 04, 2017 7:54 pm
Posts: 1
I'm having issues when using this and the other keysDown functions from gba_input.h in that I only one or two buttons can be found at a time. E.g KEY_LEFT and KEY_DOWN can be found, but not KEY_RIGHT or KEY_UP when called. Here's an example:

Code:
include <gba_console.h>
#include <gba_video.h>
#include <gba_interrupt.h>
#include <gba_systemcalls.h>
#include <gba_input.h>
#include <stdio.h>
#include <stdlib.h>

typedef u16 COLOR;

   inline void plot(int xPos, int yPos, COLOR clr)
   {
   ((u16*)(VRAM))[yPos*SCREEN_WIDTH+xPos] = clr;
   }

int main(void) {

irqInit();
irqEnable(IRQ_VBLANK);

u16 keysDownRepeatValue;
   
REG_DISPCNT= MODE_3 | BG2_ON;

while (1) {
   
   scanKeys();

        keysDownRepeatValue   = keysDownRepeat();
   

   //Left and Right
   
   if(keysDownRepeatValue == KEY_LEFT)
   {
   plot (20, 20, RGB5(0, 10, 0));
   }
   
   if (keysDownRepeatValue == KEY_RIGHT)
   {
   plot (10, 10, RGB5(10, 0, 0));
   }

   //Up and Down
   
   if(keysDownRepeatValue == KEY_UP)
   {
   plot (30, 30, RGB5(0, 0, 10));
   }

   if (keysDownRepeatValue == KEY_DOWN)
   {
   plot (40, 40, RGB5(0, 0, 10));
   }

        VBlankIntrWait();
        }
}


And so when I press all four buttons (seperately, at different times), only 1, 2 or 3 out of the 4 plots will show which means the keys aren't showing up. What's the issue?


Thu May 04, 2017 8:08 pm
Profile
Site Admin

Joined: Tue Aug 09, 2005 3:21 am
Posts: 1207
Location: UK
The return value is a button mask - it has a bit set for each button that's pressed. If multiple buttons are pressed then testing for one using "==" will fail.

You should use the & operator to mask & test for individual buttons.

Code:
 if (value & KEY_LEFT) { ... }

_________________
Help keep devkitPro toolchains free, Donate today

devkitPro IRC support
Personal Blog


Sat Jun 03, 2017 12:21 am
Profile ICQ WWW
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
  Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.
Get devkitPro at SourceForge.net. Fast, secure and Free Open Source software downloads