目录

stylusInteraction (Stylus Interaction Function)

Entry class for the stylus interaction function, which currently includes the stylus squeeze and double-tap events.

Since: 5.1.1(19)

Modules to Import

import { stylusInteraction } from '@kit.Penkit';

stylusInteraction.on('squeeze')

on(type: 'squeeze', receiver: Callback<SqueezeEvent>): void

Subscribes to stylus squeeze events. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 5.1.1(19)

Parameters:

Parameter Type Mandatory Description
type string Yes Stylus squeeze event, which has a fixed value of squeeze.
receiver Callback<SqueezeEvent> Yes Callback used to return details about the stylus squeeze event.

Example:

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

stylusInteraction.off('squeeze')

off(type: 'squeeze', receiver?: Callback<SqueezeEvent>): void

Unsubscribes from stylus squeeze events. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 5.1.1(19)

Parameters:

Parameter Type Mandatory Description
type string Yes Stylus squeeze event, which has a fixed value of squeeze.
receiver Callback<SqueezeEvent> No Callback used to unsubscribe from stylus squeeze events. The value must be the same as that of the callback passed upon subscription. If this parameter is not set, all squeeze events will be unsubscribed from.

Example:

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

stylusInteraction.on('doubleTap')

on(type: 'doubleTap', receiver: Callback<DoubleTapEvent>): void

Subscribes to stylus double-tap events. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 5.1.1(19)

Parameters:

Parameter Type Mandatory Description
type string Yes Stylus double-tap event, which has a fixed value of doubleTap.
receiver Callback<DoubleTapEvent> Yes Callback used to return details about the stylus double-tap event.

Example:

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

stylusInteraction.off('doubleTap')

off(type: 'doubleTap', receiver?: Callback<DoubleTapEvent>): void

Unsubscribes from stylus double-tap events. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 5.1.1(19)

Parameters:

Parameter Type Mandatory Description
type string Yes Stylus double-tap event, which has a fixed value of doubleTap.
receiver Callback<DoubleTapEvent> No Callback used to unsubscribe from stylus double-tap events. The value must be the same as that of the callback passed upon subscription. If this parameter is not set, all double-tap events will be unsubscribed from.

Example:

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

SqueezeEvent

Information of stylus squeeze events.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 5.1.1(19)

Name Type Read-Only Optional Description
timestamp number No No Timestamp, which is the time elapsed since the system startup, in ms.

DoubleTapEvent

Information of stylus double-tap events.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 5.1.1(19)

Name Type Read-Only Optional Description
timestamp number No No Timestamp, which is the time elapsed since the system startup, in ms.

stylusInteraction.isSensorSupported

isSensorSupported(): boolean

Queries whether the current device supports the stylus sensor data function.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Returns:

Type Description
boolean

Query result.

- true: The stylus sensor data function is supported.

- false: The stylus sensor data function is not supported.

Example:

try {
  let supported: boolean = stylusInteraction.isSensorSupported();
  console.info(`stylus sensor is supported: ${supported}`);
} catch (error) {
  console.error(`${error.code}: ${error.message}`);
}

stylusInteraction.onAccelerometer

onAccelerometer(receiver: Callback<AccelerometerEvent>): void

Subscribes to stylus accelerometer data. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Parameters:

Parameter Type Mandatory Description
receiver Callback<AccelerometerEvent> Yes Callback used to return stylus accelerometer data.

Example:

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}`);
}

stylusInteraction.offAccelerometer

offAccelerometer(receiver?: Callback<AccelerometerEvent>): void

Unsubscribes from stylus accelerometer data. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Parameters:

Parameter Type Mandatory Description
receiver Callback<AccelerometerEvent> No Callback used to return stylus accelerometer data to be unsubscribed from. The value must be the same as that of the callback passed upon subscription. If this parameter is not set, all stylus accelerometer data callbacks will be unsubscribed from.

Example:

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

AccelerometerData

Stylus accelerometer data.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Name Type Read-Only Optional Description
x number No No X-axis component of the acceleration, in m/s². 4096 indicates a gravity acceleration (g). The value range is [–32768, 32767].
y number No No Y-axis component of the acceleration, in m/s². 4096 indicates a gravity acceleration (g). The value range is [–32768, 32767].
z number No No Z-axis component of the acceleration, in m/s². 4096 indicates a gravity acceleration (g). The value range is [–32768, 32767].

AccelerometerEvent

Stylus accelerometer event data.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Name Type Read-Only Optional Description
accelerometerData AccelerometerData[] No No Stylus acceleration array data.
timestamp number No No Timestamp of the acceleration data, which is the time elapsed since the system startup, in ms.

stylusInteraction.onGyroscope

onGyroscope(receiver: Callback<GyroscopeEvent>): void

Subscribes to stylus gyroscope sensor data. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Parameters:

Parameter Type Mandatory Description
receiver Callback<GyroscopeEvent> Yes Callback used to return stylus gyroscope sensor data.

Example:

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}`);
}

stylusInteraction.offGyroscope

offGyroscope(receiver?: Callback<GyroscopeEvent>): void

Unsubscribes from stylus gyroscope sensor data. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Parameters:

Parameter Type Mandatory Description
receiver Callback<GyroscopeEvent> No Callback used to return stylus gyroscope sensor data to be unsubscribed from. The value must be the same as that of the callback passed upon subscription. If this parameter is not set, all stylus gyroscope sensor data callbacks will be unsubscribed from.

Example:

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

GyroscopeData

Stylus gyroscope sensor data.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Name Type Read-Only Optional Description
x number No No X-axis component of the gyroscope. Unit: 61 mdps/LSB. Value range: [–32768, 32767].
y number No No Y-axis component of the gyroscope. Unit: 61 mdps/LSB. Value range: [–32768, 32767].
z number No No Z-axis component of the gyroscope. Unit: 61 mdps/LSB. Value range: [–32768, 32767].

GyroscopeEvent

Stylus gyroscope event data.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Name Type Read-Only Optional Description
gyroscopeData GyroscopeData[] No No Stylus gyroscope array data.
timestamp number No No Timestamp of the gyroscope data, which is the time elapsed since the system startup, in ms.

stylusInteraction.onSensor

onSensor(receiver: Callback<SensorEvent>): void

Subscribes to stylus accelerometer and gyroscope sensor data. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Parameters:

Parameter Type Mandatory Description
receiver Callback<SensorEvent> Yes Callback used to return stylus accelerometer and gyroscope sensor data.

Example:

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}`);
}

stylusInteraction.offSensor

offSensor(receiver?: Callback<SensorEvent>): void

Unsubscribes from stylus accelerometer and gyroscope sensor data. This API uses an asynchronous callback to return the result.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Parameters:

Parameter Type Mandatory Description
receiver Callback<SensorEvent> No Callback used to return stylus sensor data to be unsubscribed from. The value must be the same as that of the callback passed upon subscription. If this parameter is not set, all sensor data callbacks will be unsubscribed from.

Example:

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

SensorData

Stylus accelerometer and gyroscope sensor data.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Name Type Read-Only Optional Description
accelerometerData AccelerometerData No No Accelerometer data.
gyroscopeData GyroscopeData No No Gyroscope sensor data.

SensorEvent

Stylus accelerometer and gyroscope sensor event data.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.StylusService

Since: 26.0.0

Name Type Read-Only Optional Description
sensorData SensorData[] No No Stylus accelerometer and gyroscope sensor array data.
timestamp number No No Timestamp of the sensor data, which is the time elapsed since the system startup, in ms.