Handling Events

The virtual method int Fl_Widget::handle(int event) is called to handle each event passed to the widget. It can:

Events are identified by the integer argument. Other information about the most recent event is stored in static locations and aquired by calling the Fl::event_*() functions. This information remains valid until another event is handled.

Here is a sample handle() method for a widget that acts as a pushbutton and also accepts the keystroke 'x' to cause the callback:

int MyClass::handle(int event) {
  switch(event) {
    case FL_PUSH:
      highlight = 1;
      redraw();
      return 1;
    case FL_DRAG: {
        int t = Fl::event_inside(this);
        if (t != highlight) {
          highlight = t;
	  redraw();
	}
      }
      return 1;
    case FL_RELEASE:
      if (highlight) {
	highlight = 0;
	redraw();
        do_callback();
	// never do anything after a callback, as the callback
	// may delete the widget!
      }
      return 1;
    case FL_SHORTCUT:
      if (Fl::event_key() == 'x') {
        do_callback();
	return 1;
      }
      return 0;
    default:
      return 0;
  }
}

You must return non-zero if your handle() method uses the event. If you return zero it indicates to the parent widget that it can try sending the event to another widget.