Recursive AI

Get the CSS cursor under the mouse in C#

OneRender is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

GetCursor reads the CSS cursor for HTML that OneRender renders, not the operating system’s mouse cursor.

Terminal
dotnet add package RecursiveAI.OneRender.Image

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneRender;
using OneRender.Image;
using OneRender.Interaction;
 
const string html = """
    <button style="cursor:pointer">
      Details
    </button>
    """;
var fonts = FontProviders.CreateDefault();
var page = InteractivePage.Create(
    html, new HtmlPageOptions(fonts));
var cursor = page.GetCursor(12, 12);
Console.WriteLine(cursor.Kind);

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

At (12, 12), the pointer is over the button. The program prints:

Text
PointingHand

OneRender returns a cursor description. Your app still has to select the matching system cursor or draw its own.

Variations

Handle a point outside the viewport. In the sample, add this line to print Default:

C#
Console.WriteLine(page.GetCursor(-1, 12).Kind);

Read a custom cursor hint. Replace the button’s style with this declaration. The sample then prints Crosshair. The file name is only a hint; you don’t need a pen.png file to query it.

CSS
cursor: url('pen.png') 2 3, crosshair;

The short answer

Call InteractivePage.GetCursor(x, y) with the mouse position relative to the HTML viewport and use CursorInfo.Kind to choose the cursor in your host UI. This reads the cursor at a point, not the mouse’s position on the screen. For HTML with cursor: pointer, the C# result is CursorKind.PointingHand.

How do I use the CSS cursor property in an HTML UI?

Set cursor on the element that should suggest the action. Pick pointer for a pointing hand or crosshair when the user needs to aim precisely. MDN’s CSS cursor reference lists the keywords and their meanings. The declaration changes the pointer hint, not what happens on click. A noninteractive box doesn’t become a button because it has cursor: pointer.

OneRender looks up the topmost element hit. CSS can pass the value down from a parent, so in a hand-shaped region you don’t need to set the property on every nested label. When the effective value is auto, GetCursor resolves it to Text over text or editable text controls and Default elsewhere. A button without a cursor rule can therefore return Default even when it’s clickable.

How do I turn mouse coordinates into a CSS cursor?

Use the position inside the rendered viewport, not the position inside the whole window or on the monitor. If your app draws the HTML in a panel offset from the window origin, subtract that offset first. If it scales the rendered area when displaying it, convert the pointer to the layout’s coordinate scale too. Don’t add the document’s scroll offset: GetCursor expects a point in the viewport you’re looking at.

If your host updates :hover through pointer input, read the cursor after it handles the move, so the result reflects any hover-dependent rule. See the pointer shape in the host guide. Also query again when a layout or style change moves a different element under a stationary pointer; a saved cursor describes the hit at the time you asked.

How do I use a custom cursor image with CSS?

Put the image URL first, optionally give its hotspot (the point inside the image that lines up with the mouse), and end with a fallback keyword. OneRender exposes the first image URL and hotspot through CursorInfo; it doesn’t fetch the image or try a list of alternate images. In the variation, the host can use the reported pen.png and hotspot (2, 3), or use the reported Crosshair kind when it can’t display the image. Relative URLs in linked stylesheets resolve against the stylesheet’s address; the inline example reports pen.png as written.

All onerender recipes