As I said in the previous post: use Mouse.setNativeCursor. It will change the mouse image when the mouse cursor is inside the lwjgl window. If you set it with a transparent image the mouse cursor will be invisible when it is inside the lwjgl window. When you move it outside it will be visible, showing the cursor the normal way by the os.
Hell, I’ll even give you the some source:
import java.nio.*;
import org.lwjgl.input.Mouse;
import org.lwjgl.input.Cursor;
/**
* Mouse cursor utility class.
*/
public class MouseCursor {
/**
* Makes the native cursor transparent. Does not grab the mouse.
*/
public static void hideNativeCursor() {
int minSize = Cursor.getMinCursorSize();
Cursor cursor = createCursor(new int[minSize*minSize], minSize, minSize, 0, 0);
enableNativeCursor(cursor);
}
/**
* Sets the native curosr to the image specified by the parameters. The
* MouseCursor position is set to the middle of the window.
*/
public static void enableNativeCursor(Cursor cursor) {
try {
if (cursor != null && (Cursor.getCapabilities() & Cursor.CURSOR_ONE_BIT_TRANSPARENCY ) != 0) {
Mouse.setNativeCursor(cursor);
} else {
System.out.println("No HW cursor support!");
Mouse.setNativeCursor(null);
}
} catch (Exception e) {
System.out.println("Exception setting hardware cursor:"+e.getMessage());
}
}
/**
* Creates a cursor.
* @return the created cursor or null if creating failed.
*/
public static Cursor createCursor(int data[], int w, int h, int x, int y) {
try {
if ((Cursor.getCapabilities() & Cursor.CURSOR_ONE_BIT_TRANSPARENCY ) != 0) {
ByteBuffer scratch = ByteBuffer.allocateDirect(4 * data.length);
for (int i=0; i<data.length; i++) {
if ((data[i]>>>24) > 0) {
scratch.put((byte) 0xff);
scratch.put((byte) ((data[i]>>16) & 0xff));
scratch.put((byte) ((data[i]>>8) & 0xff));
scratch.put((byte) ((data[i]) & 0xff));
} else {
scratch.putInt(0);
}
}
scratch.rewind();
return new Cursor(w,h, x, y, 1, scratch.asIntBuffer(), null);
}
} catch (Exception e) {
System.out.println("Exception creating cursor:"+e.getMessage());
}
return null;
}
}
To use it you:
- Do NOT grab the mouse
- Call MouseCursor.hideNativeCursor()
I think the code uses Lwjgl 0.95. If you use another version of lwjgl it might not compile because they keep moving stuff around. Then you have to search the javadocs for it.