harmony 鸿蒙@ohos.multimodalInput.inputMonitor (输入监听)(系统接口)

  • 2025-06-16
  • 浏览 (2)

@ohos.multimodalInput.inputMonitor (输入监听)(系统接口)

输入监听模块,提供了监听输入设备事件的能力。输入设备事件当前包括触摸(触屏)事件、鼠标输入事件和触控板输入事件。

说明:

  • 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

  • 文档中“全局”表示整个触控屏或触控板。如监听全局触摸事件,表示触摸触控板任何位置时,整个触控板的触摸事件均被监听。

  • 本模块接口均为系统接口。

导入模块

import { inputMonitor } from '@kit.InputKit';

inputMonitor.on(‘touch’)

on(type: ‘touch’, receiver: TouchEventReceiver): void

监听全局触摸(触屏)事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’touch’。
receiver TouchEventReceiver 回调函数,异步上报触摸屏输入事件。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { TouchEvent } from '@kit.InputKit';

try {
  inputMonitor.on('touch', (touchEvent: TouchEvent) => {
    console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
    return false;
  });
} catch (error) {
  console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.on(‘mouse’)9+

on(type: ‘mouse’, receiver: Callback<MouseEvent>): void

监听全局鼠标事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’mouse’。
receiver Callback<MouseEvent> 回调函数,异步上报鼠标输入事件。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { MouseEvent } from '@kit.InputKit';

try {
  inputMonitor.on('mouse', (mouseEvent: MouseEvent) => {
    console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
    return false;
  });
} catch (error) {
  console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.on(‘mouse’)11+

on(type: ‘mouse’, rect: display.Rect[], receiver: Callback<MouseEvent>): void

监听鼠标事件,当鼠标移动至指定矩形区域内时,触发回调任务。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’mouse’。
rect display.Rect[] 可以触发回调任务的矩形区域,可传入1至2个。
receiver Callback<MouseEvent> 回调函数,异步上报鼠标输入事件。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { MouseEvent } from '@kit.InputKit';
import { display } from '@kit.ArkUI';

/**
 * 鼠标在矩形区域内时,触发的回调任务。
 */
let callback = (mouseEvent : MouseEvent) => {
  this.getUIContext().getPromptAction().showToast({
    message: `监听成功:${JSON.stringify(mouseEvent)}`
  })
  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
  return false;
};

/**
 * 触发回调事件矩形区域。
 */
let rect: display.Rect[] = [{
  left: 100,
  top: 100,
  width: 100,
  height: 100
}, {
  left: 600,
  top: 100,
  width: 100,
  height: 100
}];

try {
  inputMonitor.on('mouse', rect, callback);
} catch (error) {
  console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.off(‘touch’)

off(type: ‘touch’, receiver?: TouchEventReceiver): void

取消监听全局触摸(触屏)事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’touch’。
receiver TouchEventReceiver 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { TouchEvent } from '@kit.InputKit';

// 取消监听单个回调函数
let callback = (touchEvent: TouchEvent) => {
  console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
  return false;
};
try {
  inputMonitor.on('touch', callback);
  inputMonitor.off('touch', callback);
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
import { TouchEvent } from '@kit.InputKit';

// 取消监听所有回调函数
let callback = (touchEvent: TouchEvent) => {
  console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
  return false;
};
try {
  inputMonitor.on('touch', callback);
  inputMonitor.off('touch');
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.off(‘mouse’)9+

off(type: ‘mouse’, receiver?: Callback<MouseEvent>): void

取消监听全局鼠标事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’mouse’。
receiver Callback<MouseEvent> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { MouseEvent } from '@kit.InputKit';

// 取消监听单个回调函数
let callback = (mouseEvent: MouseEvent) => {
  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
  return false;
};
try {
  inputMonitor.on('mouse', callback);
  inputMonitor.off('mouse', callback);
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
import { MouseEvent } from '@kit.InputKit';

// 取消监听所有回调函数
let callback = (mouseEvent: MouseEvent) => {
  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
  return false;
};
try {
  inputMonitor.on('mouse', callback);
  inputMonitor.off('mouse');
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

TouchEventReceiver

(touchEvent: TouchEvent): Boolean

触摸(触屏)输入事件的回调函数。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
touchEvent TouchEvent 触摸输入事件。

返回值:

类型 说明
Boolean 若返回true,本次触摸后续产生的事件不再分发到窗口;若返回false,本次触摸后续产生的事件还会分发到窗口。

示例:

import { TouchEvent } from '@kit.InputKit';

try {
  inputMonitor.on('touch', touchEvent => {
    if (touchEvent.touches.length == 3) { // 当前有三个手指按下
      return true;
    }
    return false;
  });
} catch (error) {
    console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.on(‘pinch’)10+

on(type: ‘pinch’, receiver: Callback<Pinch>): void

监听全局触控板的捏合事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’pinch’。
receiver Callback<Pinch> 回调函数,异步上报捏合输入事件。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Pinch } from '@kit.InputKit';

try {
  inputMonitor.on('pinch', (pinchEvent) => {
    console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
    return false;
  });
} catch (error) {
  console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.off(‘pinch’)10+

off(type: ‘pinch’, receiver?: Callback<Pinch>): void

取消监听全局触控板的捏合事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’pinch’。
receiver Callback<Pinch> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

// 取消监听单个回调函数
import { Pinch } from '@kit.InputKit';

let callback = (pinchEvent: Pinch) => {
  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
  return false;
};
try {
  inputMonitor.on('pinch', callback);
  inputMonitor.off('pinch', callback);
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消监听所有回调函数
import { Pinch } from '@kit.InputKit';

let callback = (pinchEvent: Pinch) => {
  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
  return false;
};
try {
  inputMonitor.on('pinch', callback);
  inputMonitor.off('pinch');
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.on(‘threeFingersSwipe’)10+

on(type: ‘threeFingersSwipe’, receiver: Callback<ThreeFingersSwipe>): void

监听全局触控板的三指滑动事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’threeFingersSwipe’。
receiver Callback<ThreeFingersSwipe> 回调函数,异步上报三指滑动输入事件。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

try {
  inputMonitor.on('threeFingersSwipe', (threeFingersSwipe) => {
    console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
    return false;
  });
} catch (error) {
  console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.off(‘threeFingersSwipe’)10+

off(type: ‘threeFingersSwipe’, receiver?: Callback<ThreeFingersSwipe>): void

取消监听全局触控板的三指滑动事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’threeFingersSwipe’。
receiver Callback<ThreeFingersSwipe> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

// 取消监听单个回调函数
import { ThreeFingersSwipe } from '@kit.InputKit';

let callback = (threeFingersSwipe: ThreeFingersSwipe) => {
  console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
  return false;
};
try {
  inputMonitor.on('threeFingersSwipe', callback);
  inputMonitor.off("threeFingersSwipe", callback);
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消监听所有回调函数
import { ThreeFingersSwipe } from '@kit.InputKit';

let callback = (threeFingersSwipe: ThreeFingersSwipe) => {
  console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
  return false;
};
try {
  inputMonitor.on("threeFingersSwipe", callback);
  inputMonitor.off("threeFingersSwipe");
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.on(‘fourFingersSwipe’)10+

on(type: ‘fourFingersSwipe’, receiver: Callback<FourFingersSwipe>): void

监听全局触控板的四指滑动事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’fourFingersSwipe’。
receiver Callback<FourFingersSwipe> 回调函数,异步上报四指滑动输入事件。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

try {
  inputMonitor.on('fourFingersSwipe', (fourFingersSwipe) => {
    console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
    return false;
  });
} catch (error) {
  console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.off(‘fourFingersSwipe’)10+

off(type: ‘fourFingersSwipe’, receiver?: Callback<FourFingersSwipe>): void

取消监听全局触控板的四指滑动事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’fourFingersSwipe’。
receiver Callback<FourFingersSwipe> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

// 取消监听单个回调函数
import { FourFingersSwipe } from '@kit.InputKit';

let callback = (fourFingersSwipe: FourFingersSwipe) => {
  console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
  return false;
};
try {
  inputMonitor.on('fourFingersSwipe', callback);
  inputMonitor.off('fourFingersSwipe', callback);
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消监听所有回调函数
import { FourFingersSwipe } from '@kit.InputKit';

let callback = (fourFingersSwipe: FourFingersSwipe) => {
  console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
  return false;
};
try {
  inputMonitor.on('fourFingersSwipe', callback);
  inputMonitor.off('fourFingersSwipe');
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.on(‘rotate’)11+

on(type: ‘rotate’, fingers: number, receiver: Callback<Rotate>): void

监听全局触控板的旋转事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’rotate’。
fingers number 旋转的手指数,目前支持监听手指数是2。
receiver Callback<Rotate> 回调函数,异步上报旋转输入事件。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Rotate } from '@kit.InputKit';

try {
  inputMonitor.on('rotate', 2, (rotateEvent: Rotate) => {
    console.log(`Monitor on success ${JSON.stringify(rotateEvent)}`);
    return false;
  });
} catch (error) {
  console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.off(‘rotate’)11+

off(type: ‘rotate’, fingers: number, receiver?: Callback<Rotate>): void

取消监听全局触控板的旋转事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’rotate’。
fingers number 旋转的手指数,目前支持监听手指数是2。
receiver Callback<Rotate> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

// 取消监听单个回调函数
import { Rotate } from '@kit.InputKit';

let callback = (rotateEvent: Rotate) => {
  console.log(`Monitor on success ${JSON.stringify(rotateEvent)}`);
  return false;
};
try {
  inputMonitor.on('rotate', 2, callback);
  inputMonitor.off('rotate', 2, callback);
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消监听所有回调函数
import { Rotate } from '@kit.InputKit';

let callback = (rotateEvent: Rotate) => {
  console.log(`Monitor on success ${JSON.stringify(rotateEvent)}`);
  return false;
};
try {
  inputMonitor.on('rotate', 2, callback);
  inputMonitor.off('rotate', 2);
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.on(‘pinch’)11+

on(type: ‘pinch’, fingers: number, receiver: Callback<Pinch>): void

监听全局触控板的捏合事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’pinch’。
fingers number 捏合的手指数,取值范围:大于等于2。
receiver Callback<Pinch> 回调函数,异步上报捏合输入事件。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Pinch } from '@kit.InputKit';

try {
  inputMonitor.on('pinch', 2, (pinchEvent: Pinch) => {
    console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
    return false;
  });
} catch (error) {
  console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.off(‘pinch’)11+

off(type: ‘pinch’, fingers: number, receiver?: Callback<Pinch>): void

取消监听全局触控板的捏合事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’pinch’。
fingers number 捏合的手指数,取值范围:大于等于2。
receiver Callback<Pinch> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

// 取消监听单个回调函数
import { Pinch } from '@kit.InputKit';

let callback = (pinchEvent: Pinch) => {
  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
  return false;
};
try {
  inputMonitor.on('pinch', 2, callback);
  inputMonitor.off('pinch', 2, callback);
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消监听所有回调函数
import { Pinch } from '@kit.InputKit';

let callback = (pinchEvent: Pinch) => {
  console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
  return false;
};
try {
  inputMonitor.on('pinch', 2, callback);
  inputMonitor.off('pinch', 2);
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.on(‘threeFingersTap’)11+

on(type: ‘threeFingersTap’, receiver: Callback<ThreeFingersTap>): void

监听全局触控板的三指轻点事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’threeFingersTap’。
receiver Callback<ThreeFingersTap> 回调函数,异步上报三指轻点输入事件。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

try {
  inputMonitor.on('threeFingersTap', (threeFingersTap) => {
    console.log(`Monitor on success ${JSON.stringify(threeFingersTap)}`);
    return false;
  });
} catch (error) {
  console.error(`Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.off(‘threeFingersTap’)11+

off(type: ‘threeFingersTap’, receiver?: Callback<ThreeFingersTap>): void

取消监听全局触控板的三指轻点事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’threeFingersTap’。
receiver Callback<ThreeFingersTap> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

// 取消监听单个回调函数
import { ThreeFingersTap } from '@kit.InputKit';

let callback = (threeFingersTap: ThreeFingersTap) => {
  console.log(`Monitor on success ${JSON.stringify(threeFingersTap)}`);
  return false;
};
try {
  inputMonitor.on('threeFingersTap', callback);
  inputMonitor.off("threeFingersTap", callback);
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消监听所有回调函数
import { ThreeFingersTap } from '@kit.InputKit';

let callback = (threeFingersTap: ThreeFingersTap) => {
  console.log(`Monitor on success ${JSON.stringify(threeFingersTap)}`);
  return false;
};
try {
  inputMonitor.on('threeFingersTap', callback);
  inputMonitor.off("threeFingersTap");
  console.log(`Monitor off success`);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.on(‘touchscreenSwipe’)18+

on(type: ‘touchscreenSwipe’, fingers: number, receiver: Callback<TouchGestureEvent>): void

监听触摸屏滑动手势事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’touchscreenSwipe’。
fingers number 滑动手势的手指数,取值范围:[3,5]。
receiver Callback<TouchGestureEvent> 回调函数,异步上报触摸屏滑动手势事件。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent';

let fingers: number = 4;
try {
  inputMonitor.on('touchscreenSwipe', fingers, (event: TouchGestureEvent) => {
    console.log(`Monitor on success ${JSON.stringify(event)}`);
  });
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.off(‘touchscreenSwipe’)18+

off(type: ‘touchscreenSwipe’, fingers: number, receiver?: Callback<TouchGestureEvent>): void

取消监听触摸屏滑动手势事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’touchscreenSwipe’。
fingers number 滑动手势的手指数,取值范围:[3,5]。
receiver Callback<TouchGestureEvent> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

// 取消监听单个回调函数
import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent';

let callback = (event: TouchGestureEvent) => {
  console.log(`Monitor on success ${JSON.stringify(event)}`);
};
let fingers: number = 4;
try {
  inputMonitor.on('touchscreenSwipe', fingers, callback);
  inputMonitor.off('touchscreenSwipe', fingers, callback);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消监听所有回调函数
import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent';

let fingers: number = 4;
try {
  inputMonitor.on('touchscreenSwipe', fingers, (event: TouchGestureEvent) => {
    console.log(`Monitor on success ${JSON.stringify(event)}`);
  });
  inputMonitor.off('touchscreenSwipe', fingers);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.on(‘touchscreenPinch’)18+

on(type: ‘touchscreenPinch’, fingers: number, receiver: Callback<TouchGestureEvent>): void

监听触摸屏捏合手势事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’touchscreenPinch’。
fingers number 捏合手势的手指数,取值范围:[4,5]。
receiver Callback<TouchGestureEvent> 回调函数,异步上报触摸屏捏合手势事件。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent';

let fingers: number = 4;
try {
  inputMonitor.on('touchscreenPinch', fingers, (event: TouchGestureEvent) => {
    console.log(`Monitor on success ${JSON.stringify(event)}`);
  });
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.off(‘touchscreenPinch’)18+

off(type: ‘touchscreenPinch’, fingers: number, receiver?: Callback<TouchGestureEvent>): void

取消监听触摸屏捏合手势事件。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 输入设备事件类型,取值’touchscreenPinch’。
fingers number 捏合手势的手指数,取值范围:[4,5]。
receiver Callback<TouchGestureEvent> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 SystemAPI permission error.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

// 取消监听单个回调函数
import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent';

let callback = (event: TouchGestureEvent) => {
  console.log(`Monitor on success ${JSON.stringify(event)}`);
};
let fingers: number = 4;
try {
  inputMonitor.on('touchscreenPinch', fingers, callback);
  inputMonitor.off("touchscreenPinch", fingers, callback);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消监听所有回调函数
import { TouchGestureEvent } from '@ohos.multimodalInput.gestureEvent';

let fingers: number = 4;
try {
  inputMonitor.on('touchscreenPinch', fingers, (event: TouchGestureEvent) => {
    console.log(`Monitor on success ${JSON.stringify(event)}`);
  });
  inputMonitor.off("touchscreenPinch", fingers);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.on(‘keyPressed’)15+

on(type: ‘keyPressed’, keys: Array<KeyCode>, receiver: Callback<KeyEvent>): void

监听指定按键的按下抬起事件,支持监听META_LEFT键、META_RIGHT键、电源键、音量键。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 按键事件类型,取唯一值’keyPressed’。
keys Array<KeyCode> 按键码列表,支持如下取值:KEYCODE_META_LEFT、KEYCODE_META_RIGHT、KEYCODE_POWER、KEYCODE_VOLUME_DOWN、KEYCODE_VOLUME_UP。
receiver Callback&lt;KeyEvent&gt; 用于接收上报数据的回调函数。

错误码

以下错误码的详细介绍请参见通用错误码按键监听错误码

错误码ID 错误信息
201 Permission denied.
202 Permission denied, non-system app called system api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.
4100001 Event listening not supported for the key.

示例:

import { inputMonitor, KeyEvent, KeyCode } from '@kit.InputKit';

try {
  let keys: Array<KeyCode> = [KeyCode.KEYCODE_VOLUME_UP];
  inputMonitor.on('keyPressed', keys, (event: KeyEvent ) => {
    console.log(`Monitor on success ${JSON.stringify(event)}`);
  });
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

inputMonitor.off(‘keyPressed’)15+

off(type: ‘keyPressed’, receiver?: Callback&lt;KeyEvent&gt;): void

取消监听按键按下抬起事件。支持取消监听META_LEFT键、META_RIGHT键、电源键、音量键。需和inputMonitor.on(‘keyPressed’)配套使用。

需要权限: ohos.permission.INPUT_MONITORING

系统能力: SystemCapability.MultimodalInput.Input.InputMonitor

参数:

参数名 类型 必填 说明
type string 按键事件类型,取唯一值’keyPressed’。
receiver Callback&lt;KeyEvent&gt; 需要取消监听的回调函数。若不填,取消应用所有按键监听的回调函数。

错误码

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
201 Permission denied.
202 Permission denied, non-system app called system api.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

示例:

// 取消监听单个回调函数
import { inputMonitor, KeyEvent, KeyCode } from '@kit.InputKit';

try {
  let callback = (event: KeyEvent) => {
    console.log(`Monitor on success ${JSON.stringify(event)}`);
  };
  let keys: Array<KeyCode> = [KeyCode.KEYCODE_VOLUME_UP];
  inputMonitor.on('keyPressed', keys, callback);
  inputMonitor.off("keyPressed", callback);
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消监听所有回调函数
import { inputMonitor, KeyEvent, KeyCode } from '@kit.InputKit';

try {
  let keys: Array<KeyCode> = [KeyCode.KEYCODE_VOLUME_UP];
  inputMonitor.on('keyPressed', keys, (event: KeyEvent) => {
    console.log(`Monitor on success ${JSON.stringify(event)}`);
  });
  inputMonitor.off("keyPressed");
} catch (error) {
  console.error(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Input Kit(多模输入服务)

harmony 鸿蒙Input_AxisEvent

harmony 鸿蒙Input_DeviceInfo

harmony 鸿蒙Input_DeviceListener

harmony 鸿蒙Input_Hotkey

harmony 鸿蒙Input_InterceptorEventCallback

harmony 鸿蒙Input_InterceptorOptions

harmony 鸿蒙Input_KeyEvent

harmony 鸿蒙Input_KeyState

harmony 鸿蒙Input_MouseEvent

0  赞