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.
| 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. |
Import required modules.
import { stylusInteraction } from '@kit.Penkit'; 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}`);
} 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}`);
} Import required modules.
import { stylusInteraction } from '@kit.Penkit'; 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}`);
} 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}`);
} Import required modules.
import { stylusInteraction } from '@kit.Penkit'; 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}`);
} 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}`);
} 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}`);
} 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}`);
} 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}`);
} 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}`);
} 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}`);
}