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:

gadgetA pointer to the gadget that fired the event
eventXThe x co-ordinate of the event
eventYThe y co-ordinate of the event
keyCodeThe 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: