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.

Beginners > Input Problem: Event triggered on KeyUp

#33437 - simc - Mon Jan 03, 2005 10:52 am

My GBA app has some code that polls buttons looking for a Keydown, on which it returns the buttons pressed. When the button is held for too long, it also returns when you release the button, but this only occurs on the GBA but doesn't seem to occur in VBA. I'm not sure if it is a problem with my code (most likely) or my GBA.

Here is the function that should return keys pressed since the last poll.
Code:

// in auxfunctions.c
u16 getinput()
{
    OldInputFromKeyInterrupt = InputFromKeyInterrupt;
    InputFromKeyInterrupt = ~KEYS;

    // we only want keydowns
    OldInputFromKeyInterrupt = OldInputFromKeyInterrupt & InputFromKeyInterrupt;
       
    return (InputFromKeyInterrupt & ~OldInputFromKeyInterrupt);
}


This is for a flashcard app for learning chinese using Practical Chinese Reader. Its should be ready for release after this is fixed. Tell me if it interests you at all :-)

The binary is available at http://www.baud-bandit.com/simon/pcr_flash_vol1.zip. I'm going to post the source to this page shortly http://www.baud-bandit.com/simon/stuff.html.

#33444 - identitycrisisuk - Mon Jan 03, 2005 2:25 pm

You don't really describe what this function is used for so I can't test holding down a key on this ROM you've put up.

Generally speaking there's nothing that could go wrong with your GBA, that's the format you want to make sure it works on. No emulation can be 100% perfect all the time I don't think, when I last upgraded VBA it removed some strange bugs that happened with the emulator so try that.
_________________
Code:
CanIKickIt(YES_YOU_CAN);

#33561 - simc - Wed Jan 05, 2005 2:14 pm

Quote:

You don't really describe what this function is used for so I can't test holding down a key on this ROM you've put up.


The function reads the state of the keys and compares it to the last time the input was polled by the function using the set difference operator, as shown in the FAQ. Bellow is a slight revision of the code with a redundant line removed which was that when the key is first pressed (but with InputFromKey & ~OldInputFromKey it should only return a 1 for that key in the bit field if the button was pressed since the last time it was polled)

The problem is that if the button is held for, say, a second, when it is released then the program acts as though the button has been pressed once more.

Code:

// these are globals
u16 InputFromKey=0;
u16 OldInputFromKey=~0;

// ...
u16 getinput()
{
    OldInputFromKey = InputFromKey;
    InputFromKey = ~KEYS;

    return (InputFromKey & ~OldInputFromKey);
}


This is the code that uses the function to poll the buttons:
Code:

 while (1)
    {

        answer(nextcard);
        while (!(getinput() & KEY_B))
        {
        }
        flipBuffersOnVblank();

        nextcard = findNextCard();
        question(nextcard);
        while (!(getinput() & KEY_B))
        {
        }
        flipBuffersOnVblank();
    }


Quote:

Generally speaking there's nothing that could go wrong with your GBA, that's the format you want to make sure it works on. No emulation can be 100% perfect all the time I don't think, when I last upgraded VBA it removed some strange bugs that happened with the emulator so try that.

Well, perhaps i've put too much faith in VBA since I did most of my development on it before my flashcard arrived :-). I have tried the latest VBA beta as well as BA, neither can recreate the problem.

#33568 - tepples - Wed Jan 05, 2005 4:33 pm

simc wrote:
My GBA app has some code that polls buttons looking for a Keydown, on which it returns the buttons pressed. When the button is held for too long, it also returns when you release the button, but this only occurs on the GBA but doesn't seem to occur in VBA.
Code:
InputFromKeyInterrupt

If you're using the key interrupt, you may be falling victim to different implementations of the key interrupt on the GBA vs. VBA. When you press a button, the GBA generates anywhere from one to ten interrupts based on contact bounce and then stops generating interrupts until you release and press the button again (or unless the contact bounces), but VBA generates one interrupt per vblank. Does the problem go away on hardware if you hold the button down harder?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#33582 - sajiimori - Wed Jan 05, 2005 8:17 pm

If you don't have a specific need for the interrupt, just read the key register. There are fewer ways things can go wrong.

#33586 - tepples - Wed Jan 05, 2005 8:30 pm

Right. I see the key interrupt as useful for only four main things:
  1. to wake the system from sleep mode,
  2. to wake the CPU from low-power mode in simple applications such as PogoShell or a book reader,
  3. to obtain a more precise key-down time in a fighting game or a music game, or
  4. to obtain a random seed from vcount (which may not work on the Game Boy Player).
In a drill and practice app, the closest reason I can see is reason 2.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#33594 - poslundc - Wed Jan 05, 2005 10:20 pm

I'd also consider using it to provide a soft-reset for the game (usually A+B+Start+Select).

Dan.