Mouse Cursor Not Being Captured

So, I was sick of FlyCam, so I created a class that extended FlyByCamera and wanted to use that as the camera. But the problem is that, although the game works, the cursor still shows up until you click the game. Could you please help me fix this problem? I am using Mac OS X 10.7, Java 7, and Eclipse. The relevant parts of my code are below. Thanks.

[java]
public void simpleInitApp() {
initCamera();
viewPort.setBackgroundColor(ColorRGBA.LightGray);
customcamera.setMoveSpeed(15);
customcamera.setZoomSpeed(15);
initInputs();
createCrosshair();
createFloor();

	Block box1 = new Block(ColorRGBA.Orange, new Vector3f(0, 0, 0), assetManager, rootNode);
	box1.attach();
	Block box2 = new Block(ColorRGBA.Blue, new Vector3f(2, 0, 0), assetManager, rootNode);
	box2.attach();
}

private void initInputs() {
inputManager.deleteMapping(SimpleApplication.INPUT_MAPPING_EXIT);
inputManager.deleteMapping(SimpleApplication.INPUT_MAPPING_HIDE_STATS);
inputManager.addMapping(“LeftMouse”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping(“UngrabMouse”, new KeyTrigger(KeyInput.KEY_ESCAPE));
inputManager.addListener(new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if(isPressed) {
if(name.equals(“LeftMouse”)) {
customcamera.setEnabled(true);
inputManager.setCursorVisible(false);
System.out.println(“Pressed”);
} else if(name.equals(“UngrabMouse”)) {
customcamera.setEnabled(false);
inputManager.setCursorVisible(true);
}
}
}
}, “LeftMouse”, “UngrabMouse”);
}

private void initCamera() {
	flyCam.setEnabled(false);
	customcamera = new CustomCam(cam);
	customcamera.registerWithInput(inputManager);
}

[/java]

you’ll need to do

[java]customcamera.setEnabled(true);
inputManager.setCursorVisible(false);[/java]

in your initCamera() method or something…

I did do that. It didn’t work. Strangely, it works when you click the game, and the click ActionListener uses the exact same code.

Hacky

Mouse.setGrabbed(true)

Sadly, that did not work for me either, and I am experiencing this issue both on my Windows and Mac computer. I will post the full source code:

[java]
package craft;

import com.jme3.app.;
import com.jme3.font.
;
import com.jme3.input.;
import com.jme3.input.controls.
;
import com.jme3.material.;
import com.jme3.math.
;
import com.jme3.scene.;
import com.jme3.scene.shape.
;
import com.jme3.system.*;

import java.util.logging.*;

import org.lwjgl.input.*;

public class CircuitCraft extends SimpleApplication {

CustomCam customcamera;

public static void main(String[] args) {
	CircuitCraft craft = new CircuitCraft();
	craft.setShowSettings(false);
	craft.setDisplayFps(false);
	craft.setDisplayStatView(false);
	
	AppSettings settings = new AppSettings(true);
	settings.setWidth(800);
	settings.setHeight(600);
	settings.setTitle("Circuit Craft");
	settings.setSamples(8);
	craft.setSettings(settings);
	Logger.getLogger("").setLevel(Level.SEVERE);
	craft.start();
}

public void simpleInitApp() {
	initCamera();
	viewPort.setBackgroundColor(ColorRGBA.LightGray);
	customcamera.setMoveSpeed(15);
	customcamera.setZoomSpeed(15);
	initInputs();
	createCrosshair();
	createFloor();
	
	Block box1 = new Block(ColorRGBA.Orange, new Vector3f(0, 0, 0), assetManager, rootNode);
	box1.attach();
	Block box2 = new Block(ColorRGBA.Blue, new Vector3f(2, 0, 0), assetManager, rootNode);
	box2.attach();
}

private void createCrosshair() {
	BitmapText crosshair = new BitmapText(assetManager.loadFont("Interface/Fonts/Default.fnt"), false);
	crosshair.setText("+");
	crosshair.setSize(30);
	crosshair.setLocalTranslation(settings.getWidth() / 2 - crosshair.getLineWidth()/2, settings.getHeight() / 2 + crosshair.getLineHeight()/2, 0);
	guiNode.attachChild(crosshair);
}

private void initInputs() {
	inputManager.deleteMapping(SimpleApplication.INPUT_MAPPING_EXIT);
	inputManager.deleteMapping(SimpleApplication.INPUT_MAPPING_HIDE_STATS);
	inputManager.addMapping("LeftMouse", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
	inputManager.addMapping("UngrabMouse", new KeyTrigger(KeyInput.KEY_ESCAPE));
	inputManager.addListener(new ActionListener() {
		public void onAction(String name, boolean isPressed, float tpf) {
			if(isPressed) {
				if(name.equals("LeftMouse")) {
					customcamera.setEnabled(true);
					inputManager.setCursorVisible(false);
					System.out.println("Pressed");
				} else if(name.equals("UngrabMouse")) {
					customcamera.setEnabled(false);
					inputManager.setCursorVisible(true);
				}
			}
		}
	}, "LeftMouse", "UngrabMouse");
}

private void initCamera() {
	flyCam.setEnabled(false);
	inputManager.setCursorVisible(false);
	customcamera = new CustomCam(cam);
	customcamera.setEnabled(true);
	customcamera.registerWithInput(inputManager);
}

private void createFloor() {
	Box floor = new Box(16, 0.01f, 16);
	Geometry geom = new Geometry("Box", floor);
	geom.setLocalTranslation(new Vector3f(0, -1, 0));
	Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
	mat.setColor("Color", ColorRGBA.Black);
	geom.setMaterial(mat);
	rootNode.attachChild(geom);
}

}
[/java]

I just hacked out a solution, where I enabled the custom camera in the simpleUpdate method. And it worked!

1 Like

Well, you never remove the fly cam so it is competing with you as it’s not initialized until after simpleInit(). If you don’t want fly cam then remove it.

…search the forum for how to remove the fly cam as it comes up once a week or so.