目录

Integrating Stylus Interaction

To enable stylus double-tap, squeeze, and sensor data interactions, your app can integrate the corresponding APIs to subscribe to related events and trigger specified operations within the app through callback mechanisms.

API Description

Class API Description
stylusInteraction on(type: 'squeeze', receiver: Callback<SqueezeEvent>): void Subscribes to stylus squeeze events.
stylusInteraction off(type: 'squeeze', receiver?: Callback<SqueezeEvent>): void Unsubscribes from stylus squeeze events.
stylusInteraction on(type: 'doubleTap', receiver: Callback<DoubleTapEvent>): void Subscribes to stylus double-tap events.
stylusInteraction off(type: 'doubleTap', receiver?: Callback<DoubleTapEvent>): void Unsubscribes from stylus double-tap events.
stylusInteraction isSensorSupported(): boolean Queries whether the device supports the stylus sensor data function.
stylusInteraction onAccelerometer(receiver: Callback<AccelerometerEvent>): void Subscribes to data of the stylus accelerometer.
stylusInteraction offAccelerometer(receiver?: Callback<AccelerometerEvent>): void Unsubscribes from data of the stylus accelerometer.
stylusInteraction onGyroscope(receiver: Callback<GyroscopeEvent>): void Subscribes to data of the stylus gyroscope.
stylusInteraction offGyroscope(receiver?: Callback<GyroscopeEvent>): void Unsubscribes from data of the stylus gyroscope.
stylusInteraction onSensor(receiver: Callback<SensorEvent>): void Subscribes to data of the stylus accelerometer and gyroscope.
stylusInteraction offSensor(receiver?: Callback<SensorEvent>): void Unsubscribes from data of the stylus accelerometer and gyroscope.

Development Procedure

[h2]Stylus Squeeze Events

  1. Import required modules.

    import { stylusInteraction } from '@kit.Penkit';
  2. Subscribe to stylus squeeze events.

    try {
      stylusInteraction.on('squeeze', (event: stylusInteraction.SqueezeEvent) => {
        console.info(`got squeeze event, time: ${event.timestamp}`);
      });
    } catch (error) {
      console.error(`${error.code}: ${error.message}`);
    }
  3. Unsubscribe from stylus squeeze events.

    try {
      stylusInteraction.off('squeeze', (event: stylusInteraction.SqueezeEvent) => {
        console.info(`off squeeze event, time: ${event.timestamp}`);
      });
    } catch (error) {
      console.error(`${error.code}: ${error.message}`);
    }

[h2]Stylus Double-tap Events

  1. Import required modules.

    import { stylusInteraction } from '@kit.Penkit';
  2. Subscribe to stylus double-tap events.

    try {
      stylusInteraction.on('doubleTap', (event: stylusInteraction.DoubleTapEvent) => {
        console.info(`got doubleTap event, time: ${event.timestamp}`);
      });
    } catch (error) {
      console.error(`${error.code}: ${error.message}`);
    }
  3. Unsubscribe from stylus double-tap events.

    try {
      stylusInteraction.off('doubleTap', (event: stylusInteraction.DoubleTapEvent) => {
        console.info(`off doubleTap event, time: ${event.timestamp}`);
      });
    } catch (error) {
      console.error(`${error.code}: ${error.message}`);
    }

[h2]Stylus Sensor Data Function

  1. Import required modules.

    import { stylusInteraction } from '@kit.Penkit';
  2. Query whether the device supports the stylus sensor data function.

     try {
       let supported: boolean = stylusInteraction.isSensorSupported();
       console.info(`stylus sensor is supported: ${supported}`);
     } catch (error) {
       console.error(`${error.code}: ${error.message}`);
     }
  3. Subscribe to data of the stylus accelerometer.

    try {
      stylusInteraction.onAccelerometer((event: stylusInteraction.AccelerometerEvent) => {
        console.info(`got accelerometer event, time: ${event.timestamp}`);
        for (let i = 0; i < event.accelerometerData.length; i++) {
          console.info(`accelerometer data: x=${event.accelerometerData[i].x}, y=${event.accelerometerData[i].y}
          , z=${event.accelerometerData[i].z}`);
        }
      });
    } catch (error) {
      console.error(`${error.code}: ${error.message}`);
    }
  4. Unsubscribe from data of the stylus accelerometer.

    try {
      stylusInteraction.offAccelerometer((event: stylusInteraction.AccelerometerEvent) => {
        console.info(`off accelerometer event, time: ${event.timestamp}`);
      });
    } catch (error) {
      console.error(`${error.code}: ${error.message}`);
    }
  5. Subscribe to data of the stylus gyroscope.

    try {
      stylusInteraction.onGyroscope((event: stylusInteraction.GyroscopeEvent) => {
        console.info(`got gyroscope event, time: ${event.timestamp}`);
        for (let i = 0; i < event.gyroscopeData.length; i++) {
          console.info(`gyroscope data: x=${event.gyroscopeData[i].x}, y=${event.gyroscopeData[i].y}
      , z=${event.gyroscopeData[i].z}`);
        }
      });
    } catch (error) {
      console.error(`${error.code}: ${error.message}`);
    }
  6. Unsubscribe from data of the stylus gyroscope.

    try {
      stylusInteraction.offGyroscope((event: stylusInteraction.GyroscopeEvent) => {
        console.info(`off gyroscope event, time: ${event.timestamp}`);
      });
    } catch (error) {
      console.error(`${error.code}: ${error.message}`);
    }
  7. Subscribe to data of the stylus accelerometer and gyroscope.

    try {
      stylusInteraction.onSensor((event: stylusInteraction.SensorEvent) => {
        console.info(`got sensor event, time: ${event.timestamp}`);
        for (let i = 0; i < event.sensorData.length; i++) {
          let accel = event.sensorData[i].accelerometerData;
          let gyro = event.sensorData[i].gyroscopeData;
          console.info(`sensor data: accel.x=${accel.x}, accel.y=${accel.y}, accel.z=${accel.z}, gyro.x=${gyro.x},
          gyro.y=${gyro.y}, gyro.z=${gyro.z}`);
        }
      });
    } catch (error) {
      console.error(`${error.code}: ${error.message}`);
    }
  8. Unsubscribe from data of the stylus accelerometer and gyroscope.

    try {
      stylusInteraction.offSensor((event: stylusInteraction.SensorEvent) => {
        console.info(`off sensor event, time: ${event.timestamp}`);
      });
    } catch (error) {
      console.error(`${error.code}: ${error.message}`);
    }