Skip to content

Instantly share code, notes, and snippets.

@nisovin
Last active April 29, 2022 21:50
Show Gist options
  • Save nisovin/a1abbf24449db357c2da0ee2d613808f to your computer and use it in GitHub Desktop.
Save nisovin/a1abbf24449db357c2da0ee2d613808f to your computer and use it in GitHub Desktop.
Godot Screen Mouse Position
extends Node2D
func _process(delta: float) -> void:
$Label.text = var2str(ScreenMouse.mouse_position)
using Godot;
using System.Runtime.InteropServices;
// This script should be added as a Singleton in Godot
public class ScreenMouse : Godot.Node
{
public Vector2 mouse_position {
get {
return GetCursorPosition();
}
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public static implicit operator Vector2(POINT point)
{
return new Vector2(point.X, point.Y);
}
}
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
public static Vector2 GetCursorPosition()
{
POINT lpPoint;
GetCursorPos(out lpPoint);
return lpPoint;
}
// Source: https://stackoverflow.com/questions/1316681/getting-mouse-position-in-c-sharp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment