How do I detect that the mouse is hovering a control AND the mouse button is already pressed?

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

Hi,

Detecting if a mouse has clicked ON a control is easy. However, I’m having trouble detecting if a mouse entering a control is already clicked.

What I’m trying to do is a row of buttons, but you only need to click one of them, then drag the mouse and any “neighbor” buttons will toggle to match the first clicked button, a little bit like Photoshop’s layer visibility buttons.

But the obvious “store the mouse click in variable, then check for mouse entering” is not working, i.e.:

func _ready():
	set_process_input(true)

func _input(event):	
	if event is InputEventMouseButton:
		if event.is_pressed():
			mouse_pressed = true
		else:
			mouse_pressed = false

func _on_mouse_entered():
	if mouse_pressed:
		print("button is drag-pressed!")

Any suggestions? Thanks!

:bust_in_silhouette: Reply From: njamster

Your code works fine for me. I created an HBoxContainer with a “Separation”-value of 50, then added three Button-nodes and attached your script liket his:

extends Button

var mouse_pressed = false

func _input(event):
	if event is InputEventMouseButton:
		if event.is_pressed():
			mouse_pressed = true
		else:
			mouse_pressed = false

func _on_mouse_entered():
	if mouse_pressed:
		print("button is drag-pressed!")

Lastly I connected each button’s mouse_entered-signal to the _on_mouse_entered-callback and ran the project. Everything worked as you described it.

Thanks! But it still doesn’t work consistently here… it actually works if I move the mouse out of the window while dragging, then come back without letting go off the button…

Here’s my test scene:
Test Scene Hierarchy

And here’s the script on each button:

extends Button

var mouse_pressed = false

func _input(event):
	if event is InputEventMouseButton:
		if event.is_pressed():
			mouse_pressed = true
		else:
			mouse_pressed = false

func _on_mouse_entered():
	if mouse_pressed:
		print("button is drag-pressed!")


func _on_Button_button_down():
	print("button is pressed!")

I’m using Godot 3.2.1, on MacOS

DoctorWhoof | 2020-04-15 19:20

The image (I assume) of your test scene hierarchy doesn’t work. If you sure the callbacks in your script are connected correctly, you might consider posting a bug report on github. It definitely works for me (using Godot 3.2.1 under Arch Linux).

njamster | 2020-04-15 22:36

Yes, the image show just an HBoxContainer with 5 buttons under it. The signals are connected correctly, since when I comment out the “if mouse_pressed:” line I get the message printed every time the mouse enters.

The only problem is the _input function, it only seems to run when the mouse leaves the window and comes back…

DoctorWhoof | 2020-04-16 06:36