How do I get input from the mouse wheel?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Aaron Franke

How do I get input from the mouse wheel? For example, for zooming in an out of the map in a strategy game.

The “Wheel Up Button” in Input doesn’t seem to work, unfortunately. My mouse scrolls smoothly with no buttons. I think what I need is an analog input rather than an on/off value.

I’m using Godot 3.0.5 and C# but it should be roughly the same for GDscript.

:bust_in_silhouette: Reply From: DodoIta

I don’t know about C#, but I have this GDScript code for zooming:

if event is InputEventMouseButton:
	if event.is_pressed():
		# zoom in
		if event.button_index == BUTTON_WHEEL_UP:
			zoom_pos = get_global_mouse_position()
			# call the zoom function
		# zoom out
		if event.button_index == BUTTON_WHEEL_DOWN:
			zoom_pos = get_global_mouse_position()
			# call the zoom function
:bust_in_silhouette: Reply From: asaber

This is the way to capture wheel mouse in Godot from C#.

public override void _UnhandledInput(InputEvent @event){
    if (@event is InputEventMouseButton){
        InputEventMouseButton emb = (InputEventMouseButton)@event;
        if (emb.IsPressed()){
            if (emb.ButtonIndex == (int)ButtonList.WheelUp){
                GD.Print(emb.AsText());
            }
            if (emb.ButtonIndex == (int)ButtonList.WheelDown){
                GD.Print(emb.AsText());
            }
        }
    }
}
:bust_in_silhouette: Reply From: Zenpai

Or you could just use:if Input.is_action_just_released(your thing)(gdscript)
The Wheel Up Button only has a just released function.

Thank you very much!

Zero | 2021-03-04 01:28

Thanks. Curious if/where this nugget is buried in the docs.

Stretch | 2021-09-10 19:46

I don’t know if this quirk is mentioned in the docs actually, I think I found it out by trial and error lol.

Zenpai | 2021-09-10 22:01

thanks… unlike InputMouseEvent which able to detect both pressed and not pressed. the InputMap only viable with just_released method. Which is obvious…

untimated | 2022-04-07 00:25

This doesn’t work, because this just makes it zoom forever until it reaches the limit. I want it to only zoom when I’m scrolling.

JasperC | 2022-07-08 22:11

This was the answer I was looking for! Thank you!

PoisonIvy | 2022-11-01 23:40