harmony 鸿蒙@ohos.multimodalInput.inputMonitor (输入监听)

  • 2022-08-09
  • 浏览 (966)

@ohos.multimodalInput.inputMonitor (输入监听)

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

说明:

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

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

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

导入模块

import inputMonitor from '@ohos.multimodalInput.inputMonitor';

inputMonitor.on(‘touch’)

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

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

需要权限: ohos.permission.INPUT_MONITORING

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

参数:

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

示例:

import { TouchEvent } from '@ohos.multimodalInput.touchEvent';
try {
  inputMonitor.on('touch', (touchEvent: TouchEvent) => {
    console.log(`Monitor on success ${JSON.stringify(touchEvent)}`);
    return false;
  });
} catch (error) {
  console.log(`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> 回调函数,异步上报鼠标输入事件。

示例:

import { MouseEvent } from '@ohos.multimodalInput.mouseEvent';

try {
  inputMonitor.on('mouse', (mouseEvent: MouseEvent) => {
    console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
    return false;
  });
} catch (error) {
  console.log(`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 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

示例:

import { TouchEvent } from '@ohos.multimodalInput.touchEvent';
// 取消监听单个回调函数
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.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
import { TouchEvent } from '@ohos.multimodalInput.touchEvent';
// 取消监听所有回调函数
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.log(`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> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

示例:

import { MouseEvent } from '@ohos.multimodalInput.mouseEvent';
// 取消监听单个回调函数
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.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
import { MouseEvent } from '@ohos.multimodalInput.mouseEvent';
// 取消监听所有回调函数
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.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

TouchEventReceiver

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

需要权限: ohos.permission.INPUT_MONITORING

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

参数:

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

返回值:

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

示例:

import { TouchEvent } from '@ohos.multimodalInput.touchEvent';
try {
  inputMonitor.on('touch', touchEvent => {
    if (touchEvent.touches.length == 3) { // 当前有三个手指按下
      return true;
    }
    return false;
  });
} catch (error) {
    console.log(`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> 回调函数,异步上报捏合输入事件。

示例:

import type { Pinch } from '@ohos.multimodalInput.gestureEvent';
try {
  inputMonitor.on('pinch', (pinchEvent) => {
    console.log(`Monitor on success ${JSON.stringify(pinchEvent)}`);
    return false;
  });
} catch (error) {
  console.log(`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> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

示例:

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

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.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消监听所有回调函数
import { Pinch } from '@ohos.multimodalInput.gestureEvent';

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.log(`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> 回调函数,异步上报三指滑动输入事件。

示例:

try {
  inputMonitor.on('threeFingersSwipe', (threeFingersSwipe) => {
    console.log(`Monitor on success ${JSON.stringify(threeFingersSwipe)}`);
    return false;
  });
} catch (error) {
  console.log(`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> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

示例:

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

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.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消监听所有回调函数
import { ThreeFingersSwipe } from '@ohos.multimodalInput.gestureEvent';

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.log(`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> 回调函数,异步上报四指滑动输入事件。

示例:

try {
  inputMonitor.on('fourFingersSwipe', (fourFingersSwipe) => {
    console.log(`Monitor on success ${JSON.stringify(fourFingersSwipe)}`);
    return false;
  });
} catch (error) {
  console.log(`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> 需要取消监听的回调函数。若不填,则取消当前应用监听的所有回调函数。

示例:

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

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.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
// 取消监听所有回调函数
import { FourFingersSwipe } from '@ohos.multimodalInput.gestureEvent';

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.log(`Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

0  赞