Skip to content

Cursor visibility & catching

raeleus edited this page Sep 14, 2019 · 9 revisions

For some games like first person shooters, it is often necessary to catch the cursor so it stays in the center of the screen, and only use position deltas to rotate the camera. Other times we might want to position the cursor manually. Both things can be done as follows:

Gdx.input.setCursorCatched(true);
Gdx.input.setCursorPosition(x, y);

Note that catching the cursor only works reliably in the LWJGL/LWJGL3 back-ends.

Cursor catching and positioning is only available on the desktop.

Similarly, changing the cursor image is available on the desktop and on gwt if the browser supports the "cursor:url()" syntax and also supports the png format as cursor. It can be done as follows:

Note: You Should dispose the cursor if you don't need it anymore

Cursor customCursor = Gdx.graphics.newCursor(new Pixmap(Gdx.files.internal("cursor.png")), hotspotX, hotspotY);
Gdx.graphics.setCursor(customCursor);

You can also change the cursor to a system cursor, this only works in the LWJGL3 backend and in the GWT backend. It can be done as follows:

Gdx.graphics.setSystemCursor(SystemCursor.Crosshair);

Supported system cursors are the

  • Arrow Image of an arrow cursor
  • Ibeam Image of an i-beam cursor
  • Crosshair Image of a crosshair cursor
  • Hand Image of a pointer cursor
  • HorizontalResize Image of a horizontal-resize cursor
  • VerticalResize Image of a vertical-resize cursor

cursors.

Prev | Next

Table of Contents

Wiki Style Guide

Developer's Guide

The Application Framework
A Simple Game

File Handling

Networking

Preferences

Input Handling

Memory Management

Audio
Graphics

    2D Graphics

    3D Graphics

Managing Your Assets

Internationalization and Localization

Utilities
Math Utilities
Tools
Extensions
Using libGDX With Other JVM Languages
Third Party Services

Articles

Clone this wiki locally
You can’t perform that action at this time.