Open topic with navigation

# Emulating the Mouse

You can set up the stylus — or any other tracked target — to emulate the mouse. This can be useful if you are porting an existing application to zSpace. Basic setup requires the following steps:

- Enable mouse emulation.
- Define the target to emulate it.
- Map the target's buttons to mouse buttons.

The following partial code example illustrates this. Error handling is omitted for brevity. For the complete example, refer to the Mouse Emulation sample application in the Samples\Device subdirectory.

      // zSpaceContext set during initialization.
      // ZSContext zSpaceContext = ...;
      // Get the stylus target.
      ZSHandle stylusHandle = NULL;
      zsFindTargetByType(zSpaceContext, ZS_TARGET_TYPE_PRIMARY, 0, &stylusHandle);

      // Set mouse emulation enabled.
      zsSetMouseEmulationEnabled(zSpaceContext, true);

      // Assign the target that will emulate the mouse.
      // In this case, we'll use the stylus to drive the mouse.
      zsSetMouseEmulationTarget(zSpaceContext, stylusHandle);

      // Map target buttons to mouse buttons.
      zsSetMouseEmulationButtonMapping(zSpaceContext, 0, ZS_MOUSE_BUTTON_LEFT);
      zsSetMouseEmulationButtonMapping(zSpaceContext, 1, ZS_MOUSE_BUTTON_RIGHT);

You can further refine mouse emulation by defining how far the target can be from the screen surface while emulating a mouse. This is useful if you want the stylus to behave as a stylus most of the time, but emulate a mouse when closer to the screen surface. Use the zsSetMouseEmulationMaxDistance() to specify a distance in meters.

Note:
  If you use the stylus to emulate the mouse only when it is close to the screen, you may want to change the pointer. However, the zSpace SDK does not define the shape of the stylus beam or mouse pointer.

You can also define a movement mode for mouse emulation. The movement mode, ZSMouseMovementMode, defines how the stylus and mouse control the pointer when both devices are used. By default, absolute positions are used, which means that both devices will attempt to control, or fight over, the pointer location. Use zsSetMouseEmulationMovementMode() to change to relative positions. In that case, any movement applied by mouse emulation is relative to the mouse's current position.

Open topic with navigation