How to access scroll wheel delta in winit Event?

621 Views Asked by At

I am currently learning Rust by writing a small graphical program that uses winit for window handling. My goal is to implement a zoom functionality using the scroll wheel as input.

My problem is that I do not know how to access the "delta" or how much the wheel moved (or at least in which direction). My event loop looks like this:

event_loop.run(move |event, _, control_flow| {
    match event {
        Event::WindowEvent {
            ref event,
            window_id,
        } if window_id == window.id() => {
            if !state.input(event) {
                match event {
                    // match events
                }
            }
        }
    }
});

In the "match events" section I have all the logic related to handling user input events such as keyboard strokes. For example to match a key I would do:

match event {
    WindowEvent::KeyboardInput {
        input:
            KeyboardInput {
                state: ElementState::Pressed,
                virtual_keycode: Some(VirtualKeyCode::Space),
                ..
            },
        ..
    }
    // continue matching

To match mouse events I first tried with WindowEvent::MouseInput but this seems to be intended to use with the actual buttons such as left, right click etc. so I tried using WindowEvent::MouseWheel. This event triggers when I move my wheel but I don't know how to access the delta to know in which direction the wheel moved and by how much.

I don't think matching as I do with the keys works, since this is a continuous value. My guess is that I need to access "delta" from the scope with the comment in the code below but I don't know how to do it.

match event {
    WindowEvent::MouseWheel {
        delta,
        ..
    } => {
        // how to access "delta" using "event"?
    }
1

There are 1 best solutions below

0
On

As @jmb commented you can just access the "delta" from the scope after the match. I am not sure what I was doing that I didn't see it.

Since "delta" itself is an enum you also need to match it, such as:

match event {
    WindowEvent::MouseWheel { delta, .. } => {
        match delta {
            MouseScrollDelta::LineDelta(x, y) => {
                //
            }
            _ => {}
        }
    }