Developer Guide - Event Arguments
It us generally useful to know more information about the event than just that it has occurred. We know that a button has been clicked, but where has it been clicked? If a window has been dragged, where has it been dragged to? Which key has been pressed? The "EventArgs" parameter in the EventHandler's functions gives us this information.
The EventArgs parameter is a struct that contains all of the information we need to know about an event. It contains:
gadget | A pointer to the gadget that fired the event |
eventX | The x co-ordinate of the event |
eventY | The y co-ordinate of the event |
keyCode | The button that fired the event |
Not all of these properties are filled for every event. The key press event does not send the co-ordinates, for example, and the click event does not send a keycode. The VBL event does not populate the struct with any information.
The "keyCode" value refers to an enum in the "EventHandler" class. Each button on the DS has its own unique number. If we want to check for a keycode, the easiest way is to use a switch() statement in the key press handler:
void handleKeyPress(EventArgs e) {
switch (e.keyCode) {
case KEY_CODE_A:
// A is pressed
break;
case KEY_CODE_B:
// B is pressed
break;
default:
// Etc
break;
}
}
The co-ordinates are relative to the screen, rather than to the gadget. They can be converted to "gadget space" quite easily:
s16 gadgetEventX = e.eventX - getX();
s16 gadgetEventY = e.eventY - getY();
This information lets us handle events more intelligently. We can:
- Use the same event handler for multiple buttons, and use a switch() statement on the e.gadget property (where "e" is the instance of the EventArgs struct) to determine which gadget sent the event;
- Perform different actions depending on where a gadget was clicked;
- Use a switch statement to run different code depending on which button was pressed.