Changing the mouse cursor

The mouse cursor can be set to one of the operating system cursors, a custom image, or hidden completely. The change to the cursor will be applicable only to the window you make the change to. To hide the mouse cursor, call Window.set_mouse_visible:

window = pyglet.window.Window()
window.set_mouse_visible(False)

This can be useful if the mouse would obscure text that the user is typing. If you are hiding the mouse cursor for use in a game environment, consider making the mouse exclusive instead; see Mouse exclusivity, below.

Use Window.set_mouse_cursor to change the appearance of the mouse cursor. A mouse cursor is an instance of MouseCursor. You can obtain the operating system-defined cursors with Window.get_system_mouse_cursor:

cursor = window.get_system_mouse_cursor(win.CURSOR_HELP)
window.set_mouse_cursor(cursor)

The cursors that pyglet defines are listed below, along with their typical appearance on Windows and Mac OS X. The pointer image on Linux is dependent on the window manager.

Constant Windows XP Mac OS X
CURSOR_DEFAULT cursor_win_default.png cursor_mac_default.png
CURSOR_CROSSHAIR cursor_win_crosshair.png cursor_mac_crosshair.png
CURSOR_HAND cursor_win_hand.png cursor_mac_hand.png
CURSOR_HELP cursor_win_help.png cursor_mac_default.png
CURSOR_NO cursor_win_no.png cursor_mac_no.png
CURSOR_SIZE cursor_win_size.png cursor_mac_default.png
CURSOR_SIZE_DOWN cursor_win_size_up_down.png cursor_mac_size_down.png
CURSOR_SIZE_DOWN_LEFT cursor_win_size_nesw.png cursor_mac_default.png
CURSOR_SIZE_DOWN_RIGHT cursor_win_size_nwse.png cursor_mac_default.png
CURSOR_SIZE_LEFT cursor_win_size_left_right.png cursor_mac_size_left.png
CURSOR_SIZE_LEFT_RIGHT cursor_win_size_left_right.png cursor_mac_size_left_right.png
CURSOR_SIZE_RIGHT cursor_win_size_left_right.png cursor_mac_size_right.png
CURSOR_SIZE_UP cursor_win_size_up_down.png cursor_mac_size_up.png
CURSOR_SIZE_UP_DOWN cursor_win_size_up_down.png cursor_mac_size_up_down.png
CURSOR_SIZE_UP_LEFT cursor_win_size_nwse.png cursor_mac_default.png
CURSOR_SIZE_UP_RIGHT cursor_win_size_nesw.png cursor_mac_default.png
CURSOR_TEXT cursor_win_text.png cursor_mac_text.png
CURSOR_WAIT cursor_win_wait.png cursor_mac_wait.png
CURSOR_WAIT_ARROW cursor_win_wait_arrow.png cursor_mac_default.png

Alternatively, you can use your own image as the mouse cursor. Use pyglet.image.load to load the image, then create an ImageMouseCursor with the image and "hot-spot" of the cursor. The hot-spot is the point of the image that corresponds to the actual pointer location on screen, for example, the point of the arrow:

image = pyglet.image.load('cursor.png')
cursor = pyglet.window.ImageMouseCursor(image, 16, 8)
window.set_mouse_cursor(cursor)

You can even render a mouse cursor directly with OpenGL. You could draw a 3-dimensional cursor, or a particle trail, for example. To do this, subclass MouseCursor and implement your own draw method. The draw method will be called with the default pyglet window projection, even if you are using another projection in the rest of your application.