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 > Multiple held keys issue

#53475 - Cidrick - Thu Sep 08, 2005 5:15 pm

I'm writing a program and I've run into an issue that I can't quite figure how to get around. My code is something like this

Code:
...

if (keysHeld() & KEY_A)
{
   if (((keysHeld() & KEY_DOWN)||(keysDown() & KEY_DOWN)))
   {
      // move down code here
   }
   else if (((keysHeld() & KEY_UP)||(keysDown() & KEY_UP)))
   {
      // move up code here

...


This partially works. If I'm holding down the A key and then press or hold right, it does what I want it to. However, if I'm holding down the A key, then press right and let go, for whatever reason (keysHeld() & KEY_A) becomes false. It seems that if you're holding down two keys and you let go of one, it thinks you're letting go of both of them.

Am I missing something or is this an issue with the devkit?

#53515 - spencer723 - Thu Sep 08, 2005 11:03 pm

No it's just the way you have your code set up, once the program can see that you're not holding the A button it skips that operation therefore not running the "move right" code.

#53523 - cybereality - Thu Sep 08, 2005 11:22 pm

You have to check each key seperately to fix the problem. So each key would have its own if statement (not else if) and inside of that check it would then check for any combinations of keys. Otherwise only the first key press is registered and you cannot check multiple keys accurately.
_________________
// cybereality

#53547 - Cidrick - Fri Sep 09, 2005 2:48 am

Well doesn't that make a guy feel stupid. Thanks.

#53554 - knight0fdragon - Fri Sep 09, 2005 4:15 am

id reccommend using logic, something like if A AND UP do this A AND DOWN do that

#53618 - Cidrick - Fri Sep 09, 2005 8:26 pm

Hahah, oddly enough it wasn't a problem with my code, but rather the emulator I was using to test my code. I tried another emulator and it works just fine. Apparently IdeaS has issues with multiple keypresses.

Thanks for the help anyway =P