Is there a way to make a button able to be hovered over but not pressed?

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

The title says it pretty well, I need to be able to hover over a button in my project but not press it when it’s in this state. Is this possible in godot?

:bust_in_silhouette: Reply From: JimArtificer

This is possible, but it’s harder than it should be. There’s something unexpected (to me) about how the _Input(InputEvent @event) function is called. It appears to be always called, even when it seems like another Control should have handled the event. To work around this I added a mouse cursor position check.

Add a script to your Button that tells Godot that the mouse pressed event has been handled. Example in C#:

public override void _Input(InputEvent @event) {
	if(@event is InputEventMouseButton) {
		InputEventMouseButton mouseEvent = @event as InputEventMouseButton;
		if(mouseEvent.Pressed && GetGlobalRect().HasPoint(mouseEvent.GlobalPosition)) {
			GetViewport().SetInputAsHandled();
		}
	}
}

Thanks for the help! I have 0 familiarity with C# though, aside from gdscript I’ve only ever used python and JS. Could you break down the logic for me, or do you know how much of this transfers to gdscript?

DigitalDrako | 2020-06-25 05:50

:bust_in_silhouette: Reply From: whiteshampoo

Based on the other answer, but a bit simpler:

func _input(event : InputEvent) -> void:
	if is_hovered() and event is InputEventMouseButton:
		get_viewport().set_input_as_handled()

warning:
i still can get focus and pressed via keyboard!