Select to view content in your preferred language

How to get cursor position outside of a Map Tool

314
5
Jump to solution
03-18-2025 05:01 AM
BarbaraSchneider2
Frequent Contributor

I can get the current cursor position inside a Map Tool as follows:

protected override async Task HandleMouseDownAsync(MapViewMouseButtonEventArgs e)
{
	// Get cursor point
	MapPoint mapPoint =  await QueuedTask.Run(() =>
	{
		return ActiveMapView.ClientToMap(e.ClientPoint);
	});
}

 However, I would like to get the cursor position outside of a MapTool. Does anyone know how to handle this? I've already posted another question: https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-catch-event-when-z-changes/m-p/1339114...

Thanks,
Barbara

0 Kudos
2 Solutions

Accepted Solutions
SelimDissem
Esri Contributor

Hi Barbara,

As illustrated by your current implementation, the ArcGIS Pro SDK provides you with the MapTool to give you access to the necessary context / information to create a MapPoint based on the position of the mouse within the MapView.

Beyond the scope of the Pro API, you could use the Windows API but will have to manage all the scenarios and conversions yourself.

 

View solution in original post

0 Kudos
BarbaraSchneider2
Frequent Contributor

I found a solution to the problem:

I declare an internal class member variable (CursorPoint) inside my MapTool which I can access from outside:

internal class ToolAddRasterPoint : MapTool
{
    internal MapPoint CursorPoint{ get; set; }

    public ToolAddRasterPoint()
    {
        IsSketchTool = true;
        UseSnapping = false;
        SketchType = SketchGeometryType.Point;

        MyModule.ToolAddRasterPoint = this; // to access from outside
    }
        
    protected override async void OnToolMouseMove(MapViewMouseEventArgs e)
    {
        await QueuedTask.Run(() =>
        {
            CursorPoint = MapView.Active.ClientToMap(e.ClientPoint);
        });
    }
}

 

View solution in original post

0 Kudos
5 Replies
SelimDissem
Esri Contributor

@BarbaraSchneider2 ,

could you elaborate some more on what you're trying to achieve and what context you would be in?

0 Kudos
BarbaraSchneider2
Frequent Contributor

Hi Selim,

I want to do the following:

1. The user clicks on a keyboard shortcut

2. This keyboard shortcut is linked to a command, in which I programmatically get the current cursor position and create a MapPoint

Within a map tool, this looks as follows:

1. The user clicks on a keyboard shortcut

2. This keyboard shortcut is linked to a command, in which I programmatically activate a MapTool

3. The user has to click again on the Stereo Map to get the cursor position

--> This is one click more. The users of my applications perform these steps about 500 times a day, so these are 500 clicks more...

0 Kudos
SelimDissem
Esri Contributor

Hi Barbara,

As illustrated by your current implementation, the ArcGIS Pro SDK provides you with the MapTool to give you access to the necessary context / information to create a MapPoint based on the position of the mouse within the MapView.

Beyond the scope of the Pro API, you could use the Windows API but will have to manage all the scenarios and conversions yourself.

 

0 Kudos
BarbaraSchneider2
Frequent Contributor

This is the code to get the cursor position (x, y, and z) using the Windows API:

System.Drawing.Point cursorPoint = System.Windows.Forms.Cursor.Position;
System.Windows.Point screenPoint = new System.Windows.Point(cursorPoint.X, cursorPoint.Y);

var mapPoint = await QueuedTask.Run(() =>
{
	// Convert to map position
	return MapView.Active.ScreenToMap(screenPoint);
});
0 Kudos
BarbaraSchneider2
Frequent Contributor

I found a solution to the problem:

I declare an internal class member variable (CursorPoint) inside my MapTool which I can access from outside:

internal class ToolAddRasterPoint : MapTool
{
    internal MapPoint CursorPoint{ get; set; }

    public ToolAddRasterPoint()
    {
        IsSketchTool = true;
        UseSnapping = false;
        SketchType = SketchGeometryType.Point;

        MyModule.ToolAddRasterPoint = this; // to access from outside
    }
        
    protected override async void OnToolMouseMove(MapViewMouseEventArgs e)
    {
        await QueuedTask.Run(() =>
        {
            CursorPoint = MapView.Active.ClientToMap(e.ClientPoint);
        });
    }
}

 

0 Kudos