|
The Event-driven paradigm
CptS 355 - Programming Language Design Washington State University |
|
EventsSimilar to an exception, an event is a (hardware) condition that is signaled. Unlike an exception, an event is a normal, expected condition. Common events are from input devices, such as
while (TRUE) { // loop forever
while (empty(event.queue)); // wait for an event
event = pop(event.queue);
event.type(event.information);
}
// a mouseDown event handler
public boolean mouseDown(Event event, int int x, int y) {
// what to do on a mouse down
}
// a buttonDown event handler
buttonDown(button b) {
// what to do on a button down
}
Here is a Java example. Notice that the main event loop has
been completely eliminated. The action method controls the
dispatching of an event.
/** Respond to user actions on controls. */
public boolean action(Event e, Object arg) {
if (e.target instanceof TextField) {
setSliderValue(getValue());
controller.convert(this);
return true;
}
if (e.target instanceof Choice) {
controller.convert(this);
return true;
}
return false;
}
/** Respond to the slider. */
public boolean handleEvent(Event e) {
if (e.target instanceof Scrollbar) {
textField.setText(String.valueOf(slider.getValue()));
controller.convert(this);
}
return super.handleEvent(e);
}
Source of InformationThese lecture notes are based on Chapter 14 in "Programming Languages, 6ed" by Robert Sebesta. |
|