Use this file to discover all available pages before exploring further.
FLTK’s event system handles all user interactions including mouse clicks, keyboard input, window changes, and drag-and-drop operations. Events are delivered to widgets through the handle() method and can be queried using the Fl::event_*() functions.
Events are handled by implementing the handle() method (FL/Fl_Widget.H:289):
class My_Button : public Fl_Button {public: My_Button(int X, int Y, int W, int H, const char *L = 0) : Fl_Button(X, Y, W, H, L) {} int handle(int event) override { switch(event) { case FL_PUSH: printf("Button pushed at %d,%d\n", Fl::event_x(), Fl::event_y()); return 1; // Event handled case FL_RELEASE: printf("Button released\n"); return 1; case FL_ENTER: printf("Mouse entered\n"); return 1; case FL_LEAVE: printf("Mouse left\n"); return 1; default: // Pass unhandled events to base class return Fl_Button::handle(event); } }};
Return 1 if you handled the event, 0 if you didn’t. If you return 0, FLTK may send the event to other widgets.
From FL/core/events.H:99-212, FLTK provides functions to query mouse event details:
Position
Buttons
Clicks
Wheel
int handle(int event) { if (event == FL_PUSH) { // Position relative to window int x = Fl::event_x(); int y = Fl::event_y(); // Absolute screen position int screen_x = Fl::event_x_root(); int screen_y = Fl::event_y_root(); printf("Click at (%d,%d)\n", x, y); } return 0;}
int handle(int event) { if (event == FL_PUSH) { // Which button was pressed int btn = Fl::event_button(); if (btn == FL_LEFT_MOUSE) { printf("Left click\n"); } else if (btn == FL_RIGHT_MOUSE) { printf("Right click\n"); } else if (btn == FL_MIDDLE_MOUSE) { printf("Middle click\n"); } } return 0;}
int handle(int event) { if (event == FL_PUSH) { // Number of clicks (0=single, 1=double, etc.) int clicks = Fl::event_clicks(); if (clicks > 0) { printf("Double-click detected\n"); } // Check if still within click distance if (Fl::event_is_click()) { printf("Mouse hasn't moved far\n"); } } return 0;}
int handle(int event) { if (event == FL_MOUSEWHEEL) { // Vertical scroll (positive = down) int dy = Fl::event_dy(); // Horizontal scroll (positive = right) int dx = Fl::event_dx(); printf("Scroll: dx=%d, dy=%d\n", dx, dy); return 1; } return 0;}
// Get widget under mouseFl_Widget *under = Fl::belowmouse();// Set which widget tracks mouseFl::belowmouse(my_widget);// Get widget being draggedFl_Widget *dragged = Fl::pushed();// Set widget that receives drag eventsFl::pushed(my_widget);
int handle(int event) { switch(event) { case FL_DND_ENTER: // Mouse dragging into widget return 1; // Accept drag case FL_DND_DRAG: // Mouse moving while dragging return 1; case FL_DND_LEAVE: // Mouse left widget while dragging return 1; case FL_DND_RELEASE: // User released mouse button return 1; // Accept drop default: return Fl_Widget::handle(event); }}
2
Receive Dropped Data
int handle(int event) { if (event == FL_PASTE) { // Get type of data const char *type = Fl::event_clipboard_type(); if (type == Fl::clipboard_plain_text) { // Text data const char *text = Fl::event_text(); int len = Fl::event_length(); printf("Dropped text: %.*s\n", len, text); } else if (type == Fl::clipboard_image) { // Image data Fl_RGB_Image *img = (Fl_RGB_Image*)Fl::event_clipboard(); printf("Dropped image: %dx%d\n", img->w(), img->h()); } return 1; } return 0;}