Remove/reset event handlers

Hi!

I have a problem with registering/unregistering event handlers in Cesium, and I can’t seem to disable a previously registered event handler. All event handlers are stored on app initialization. Code:

I have a function which registers an event handler on LEFT_CLICK

const toggleAddListener = async (
	scene: Scene,
	screenSpaceEventHandler: ScreenSpaceEventHandler
): Promise<void> => {
	const viewer = useCesiumViewer();
	removeAllScreenSpaceInputActions(viewer, screenSpaceEventHandler);

	if(shouldBeRegistered)
		screenSpaceEventHandler.setInputAction(async (event: { position: Cartesian2 }) => {
			//Code emitted, actions here
		}, ScreenSpaceEventType.LEFT_CLICK);
	} else {
		screenSpaceEventHandler.setInputAction(
			OriginalCesiumInputActions.LEFT_CLICK,
			ScreenSpaceEventType.LEFT_CLICK
		);
	}
};

This function works just fine and achieves what I want. But we need to be able to disable this event handler outside this function in several cases. So we made a function which removes and resets all handlers that are used.

const removeAllScreenSpaceInputActions = (
	viewer: Viewer,
	screenSpaceEventHandler: ScreenSpaceEventHandler
) => {
	if (!screenSpaceEventHandler) return;

	screenSpaceEventHandler.removeInputAction(
		ScreenSpaceEventType.MOUSE_MOVE,
		KeyboardEventModifier.SHIFT
	);
	screenSpaceEventHandler.removeInputAction(
		ScreenSpaceEventType.LEFT_CLICK,
		KeyboardEventModifier.SHIFT
	);
	screenSpaceEventHandler.removeInputAction(
		ScreenSpaceEventType.RIGHT_CLICK,
		KeyboardEventModifier.SHIFT
	);

	screenSpaceEventHandler.removeInputAction(ScreenSpaceEventType.MOUSE_MOVE);
	screenSpaceEventHandler.removeInputAction(ScreenSpaceEventType.LEFT_CLICK);
	screenSpaceEventHandler.removeInputAction(ScreenSpaceEventType.RIGHT_CLICK);
	screenSpaceEventHandler.removeInputAction(ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
	screenSpaceEventHandler.removeInputAction(ScreenSpaceEventType.LEFT_DOWN);
	screenSpaceEventHandler.removeInputAction(ScreenSpaceEventType.LEFT_UP);

	screenSpaceEventHandler.setInputAction(
		OriginalCesiumInputActions.LEFT_CLICK,
		ScreenSpaceEventType.LEFT_CLICK
	);

	screenSpaceEventHandler.setInputAction(() => ({}), ScreenSpaceEventType.LEFT_DOWN);

	/* screenSpaceEventHandler.setInputAction(
		OriginalCesiumInputActions.LEFT_DOWN,
		ScreenSpaceEventType.LEFT_DOWN
	); */

	if (
		OriginalCesiumInputActions.LEFT_CLICK__SHIFT &&
		OriginalCesiumInputActions.RIGHT_CLICK__SHIFT &&
		OriginalCesiumInputActions.LEFT_CLICK
	) {
		screenSpaceEventHandler.setInputAction(
			OriginalCesiumInputActions.LEFT_CLICK__SHIFT,
			ScreenSpaceEventType.LEFT_CLICK,
			KeyboardEventModifier.SHIFT
		);
		screenSpaceEventHandler.setInputAction(
			OriginalCesiumInputActions.RIGHT_CLICK__SHIFT,
			ScreenSpaceEventType.RIGHT_CLICK,
			KeyboardEventModifier.SHIFT
		);

		screenSpaceEventHandler.setInputAction(
			OriginalCesiumInputActions.LEFT_CLICK,
			ScreenSpaceEventType.LEFT_CLICK
		);
	}

	viewer.scene.screenSpaceCameraController.enableTranslate = true;
	viewer.scene.screenSpaceCameraController.enableLook = true;
};

This does not work, and the event handler in my first snippet still triggers after a full reset. FYI:

  • I tried limiting removeAllScreenSpaceInputActions to only contain LEFT_CLICK to mirror the else-block which works, but still no success.
  • I’ve debugged and logged the events, and everything happens in the correct order.
  • The functions run from the same script, so there is no framework lifecycle interfering.

I’m new here, so tell me if you need more information about the problem :slight_smile:

Hi there,

I think something is going wrong here outside of the CesiumJS code itself.

I can confirm removeInputAction works in this example.

Could it be that you are referencing a different screenSpaceEventHandler?

Hello again,

Sorry for the late reply. The case regarding this bug has been de-prioritized, but I will soon get back to it. Will update this thread with whatever I might find out.

FYI: I can confirm I’m referencing the same screenSpaceEventHandler.