Part 5 of 10 19 Jan 2026 By Raj Patil 30 min read

Part 5: Event Handling in Freya - Mouse, Keyboard & Scroll

Part 5 of the Freya Rust GUI series. Learn to handle user interactions like mouse clicks, keyboard input, and scrolling events in your application.

Intermediate #rust #freya #events #input-handling #keyboard #mouse #gui
Building Native GUIs with Rust & Freya 5 / 10

Event Handling

Events are how users interact with your application. I’ll explore Freya’s event system.

Click Events

Button::new()
    .on_press(|_| {
        println!("Button clicked!");
    })
    .child(label().text("Click me"))

Keyboard Events

rect()
    .a11y_focusable(true) // Make element focusable to receive key events
    .on_key_down(|e| {
        if e.data.key == Key::Named(NamedKey::Enter) {
            println!("Enter pressed!");
        }
    })
    .child(label().text("Press Enter"))

Mouse Events

rect()
    .width(200.0)
    .height(200.0)
    .background(Color::from_rgb(100, 100, 100))
    .on_pointer_enter(|_| println!("Mouse entered"))
    .on_pointer_leave(|_| println!("Mouse left"))
    .on_pointer_down(|_| println!("Mouse down"))
    .on_pointer_up(|_| println!("Mouse up"))

Event Modifiers

Button::new()
    .on_press(|e| {
        // Note: modifiers might be part of specific event data depending on event type
        // This is a conceptual example for pointer events
        if e.data.modifiers.contains(Modifiers::CONTROL) {
            println!("Ctrl + Click!");
        }
    })
    .child(label().text("Ctrl + Click me"))

Scroll Events

rect()
    .width(Size::fill())
    .height(200.0)
    .overflow_y(Overflow::Scroll) // Enable scrolling
    .on_scroll(|s| {
        // Access scroll data
        println!("Scroll event: {:?}", s);
    })
    // Add enough content to scroll

What’s Next

Next, I’ll explore styling and theming in Freya.

Summary