How to change the type of mouse cursor?
Asked Answered
M

6

0

Hi, I found the set_default_cursor_shape function in the Input docs, but it doesn't seem to do anything:

Input.SetDefaultCursorShape(Input.CursorShape.Drag);
GD.Print(Input.GetCurrentCursorShape().ToString());

Output is "Arrow", or whatever the last cursor shape was, but never "Drag". I tried putting it in both in _Ready and in _Process, at the root of the scene.

The only example I found online is this: https://mcmap.net/q/21929/-change-mouse-cursor-when-entering-exiting-area2d which looks the same as what I did

Marcellemarcellina answered 8/11, 2023 at 20:14 Comment(0)
F
0

Seems to work (mostly):

using Godot;
using System;

public partial class node_2d : Node2D
{
	public override void _Ready()
	{
		//Input.SetDefaultCursorShape(Input.CursorShape.Cross);
		Input.SetDefaultCursorShape(Input.CursorShape.Drag);
		GD.Print(Input.GetCurrentCursorShape());
	}

	public override void _Process(double delta)
	{
		GD.Print(Input.GetCurrentCursorShape());
	}
}

Changing the cursor works. However, in ready it prints "arrow" and in process it prints "drag".

In case you are trying to change the mouse cursor on a control node, you have to do it on the control itself:

using Godot;
using System;

public partial class control : Control
{
	public override void _Ready()
	{
		MouseDefaultCursorShape = CursorShape.Drag;
		GD.Print(MouseDefaultCursorShape);
	}

	public override void _Process(double delta)
	{
		GD.Print(MouseDefaultCursorShape);
	}
}
Faddist answered 8/11, 2023 at 21:12 Comment(0)
F
0

In Godot 4 it works for me like this:


func _ready() -> void:
	Input.set_default_cursor_shape(Input.CURSOR_DRAG)

EDIT: Sorry, just saw the C# tag ๐Ÿ˜ฎ

Faddist answered 8/11, 2023 at 20:40 Comment(0)
F
0

Seems to work (mostly):

using Godot;
using System;

public partial class node_2d : Node2D
{
	public override void _Ready()
	{
		//Input.SetDefaultCursorShape(Input.CursorShape.Cross);
		Input.SetDefaultCursorShape(Input.CursorShape.Drag);
		GD.Print(Input.GetCurrentCursorShape());
	}

	public override void _Process(double delta)
	{
		GD.Print(Input.GetCurrentCursorShape());
	}
}

Changing the cursor works. However, in ready it prints "arrow" and in process it prints "drag".

In case you are trying to change the mouse cursor on a control node, you have to do it on the control itself:

using Godot;
using System;

public partial class control : Control
{
	public override void _Ready()
	{
		MouseDefaultCursorShape = CursorShape.Drag;
		GD.Print(MouseDefaultCursorShape);
	}

	public override void _Process(double delta)
	{
		GD.Print(MouseDefaultCursorShape);
	}
}
Faddist answered 8/11, 2023 at 21:12 Comment(0)
M
0

OK, I made a blank scene and suddenly it works. Clearly something is interfering with it, but I have almost nothing in this project except some areas and labels... time to investigate.

Marcellemarcellina answered 9/11, 2023 at 17:30 Comment(0)
M
0

So, it was caused by TexturRect's mouse filter setting. I'm using some TextureRects for the background, and I'm guessing this:

caused any cursor change to immediately reset. Setting it to "Ignore" fixed the issue. Very annoying considering that "Pass" is the default setting and I'll probably have to do this for every TextureRect in the game, but at least I know where the problem is now.

Marcellemarcellina answered 9/11, 2023 at 17:50 Comment(0)
M
0

Faddist I can't set my own answer as the solution, so you can have it for the effort ๐Ÿ˜„

Marcellemarcellina answered 9/11, 2023 at 17:51 Comment(0)
F
0

Thanks! Glad you figured it out. ๐Ÿ˜ƒ

Faddist answered 9/11, 2023 at 19:52 Comment(0)

© 2022 - 2025 โ€” McMap. All rights reserved.