gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Detecting key presses with interrupts

#172750 - sylvestersteele - Mon Mar 01, 2010 4:23 am

Hi,

Detecting key presses by polling in the while loop is pretty straightforward, but I want to detect them using interrupts. I did the following:

Code:
irqInit ();
   irqSet(IRQ_VBLANK, Vblank);
   irqEnable (IRQ_KEYS);
   irqSet(IRQ_KEYS, keyPressed);
   consoleDemoInit();


And I found that the keyPressed function is never executed. I was going through the previous posts and came across a post saying that key presses are supposed to be detecting while processing the VBlank interrupt.

Q1: Is this this best way to do it?

Q2: When is the IRQ_KEYS interrupt generated?

Q3: If the answer to Q1 is no- then what is the best way to do it?

Thanks,
Sylvester

#172755 - Dwedit - Mon Mar 01, 2010 12:03 pm

Detecting keypresses with interrupts is just stupid. You can just read the keyboard once per frame, like everyone else does, possibly inside your Vblank handler.

The only real use of the keyboard interrupt was to wake a GBA out of sleep mode, and the DS has a hinge feature for sleep mode instead.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#172756 - SteveH - Mon Mar 01, 2010 2:04 pm

Also NEVER call irqInit() in any of the latest versions of the devkitARM toolchain, currently at r28, as it gets called before main() now.

The more important question I think should be asked here:

Why do you think that using an IRQ handler will be better than just polling the keys in a while() loop?

As WinterMute said in IRC the other night (slighty paraphrased here):

"If your main loop takes more than 1 frame to process, your doing it wrong."