Page 1 of 1

KeysDownRepeat(); Can't use more than once?

Posted: Thu May 04, 2017 8:08 pm
by Templar
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: Select all

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?

Re: KeysDownRepeat(); Can't use more than once?

Posted: Sat Jun 03, 2017 12:21 am
by WinterMute
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: Select all

 if (value & KEY_LEFT) { ... }