openharmony 鸿蒙 js-apis-inputmonitor-sys

2025-06-12 浏览 (1)

@ohos.multimodalInput.inputMonitor (Input Monitor) (System API)

The inputMonitor module implements listening for events of input devices, including the touchscreen, mouse, touchpad, etc.

NOTE

  • The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • In this document, global indicates the entire touchscreen or touchpad. For example, listening for global touch events means to listen for touch events of the entire touchpad when a user touches at any position on the touchpad.

  • The APIs provided by this module are system APIs.

Modules to Import

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

inputMonitor.on('touch')

on(type: 'touch', receiver: TouchEventReceiver): void

Enables listening for global touch (touchscreen) events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of touch.
receiverTouchEventReceiverYesCallback used to return touch events asynchronously.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

Enables listening for global mouse events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of mouse.
receiverCallback<MouseEvent>YesCallback used to return mouse events asynchronously.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

Enables listening for mouse events. When the mouse pointer moves to the specified rectangular area, a callback is triggered.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of mouse.
rectdisplay.Rect[]YesRectangular area where a callback is triggered. One or two rectangular areas can be specified.
receiverCallback<MouseEvent>YesCallback used to return mouse events asynchronously.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

/**
 * Callback triggered when the mouse pointer moves to the specified rectangular area.
 */
let callback = (mouseEvent : MouseEvent) => {
  this.getUIContext().getPromptAction().showToast({
    message: `Monitor on success: ${JSON.stringify(mouseEvent)}`
  })
  console.log(`Monitor on success ${JSON.stringify(mouseEvent)}`);
  return false;
};

/**
 * Rectangular area where a callback is triggered.
 */
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

Disables listening for global touch (touchscreen) events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of touch.
receiverTouchEventReceiverNoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

// Disable listening for a single callback.
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';

// Cancel listening for all callbacks.
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

Disables listening for global mouse events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of mouse.
receiverCallback<MouseEvent>NoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

// Disable listening for a single callback.
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';

// Cancel listening for all callbacks.
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

Defines the callback for touch (touchscreen) events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
touchEventTouchEventYesTouch event.

Return value

TypeDescription
BooleanResult indicating whether the touch event will be dispatched to the window. The value true indicates that the touch event will be dispatched to the window, and the value false indicates the opposite.

Example

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

try {
  inputMonitor.on('touch', touchEvent => {
    if (touchEvent.touches.length == 3) {// Three fingers are pressed.
      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

Enables listening for global touchpad pinch events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of pinch.
receiverCallback<Pinch>YesCallback used to return pinch events asynchronously.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

Disables listening for global touchpad pinch events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of pinch.
receiverCallback<Pinch>NoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

// Disable listening for a single callback.
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`])}`);
}
// Cancel listening for all callbacks.
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

Enables listening for three-finger swipe events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of threeFingersSwipe.
receiverCallback<ThreeFingersSwipe>YesCallback used to return three-finger swipe events asynchronously.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

Disables listening for three-finger swipe events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of threeFingersSwipe.
receiverCallback<ThreeFingersSwipe>NoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

// Disable listening for a single callback.
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`])}`);
}
// Cancel listening for all callbacks.
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

Enables listening for four-finger swipe events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of fourFingersSwipe.
receiverCallback<FourFingersSwipe>YesCallback used to return four-finger swipe events asynchronously.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

Disables listening for four-finger swipe events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of fourFingersSwipe.
receiverCallback<FourFingersSwipe>NoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

// Disable listening for a single callback.
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`])}`);
}
// Cancel listening for all callbacks.
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

Enables listening for rotation events of the touchpad.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of rotate.
fingersnumberYesNumber of fingers that trigger a rotation. The value must not be greater than 2.
receiverCallback<Rotate>YesCallback used to return rotation events asynchronously.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

Disables listening for rotation events of the touchpad.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of rotate.
fingersnumberYesNumber of fingers that trigger a rotation. The value must not be greater than 2.
receiverCallback<Rotate>NoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

// Disable listening for a single callback.
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`])}`);
}
// Cancel listening for all callbacks.
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

Enables listening for global touchpad pinch events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of pinch.
fingersnumberYesNumber of fingers that trigger the pinch. The value must be greater than or equal to 2.
receiverCallback<Pinch>YesCallback used to return pinch events asynchronously.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

Disables listening for global touchpad pinch events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of pinch.
fingersnumberYesNumber of fingers that trigger the pinch. The value must be greater than or equal to 2.
receiverCallback<Pinch>NoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

// Disable listening for a single callback.
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`])}`);
}
// Cancel listening for all callbacks.
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

Enables listening for three-finger tap events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of threeFingersTap.
receiverCallback<ThreeFingersTap>YesCallback used to return three-finger tap events asynchronously.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

Disables listening for three-finger tap events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of threeFingersTap.
receiverCallback<ThreeFingersTap>NoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

// Disable listening for a single callback.
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`])}`);
}
// Cancel listening for all callbacks.
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

Enables listening for touchscreen swipe events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of touchscreenSwipe.
fingersnumberYesNumber of fingers that trigger the swipe. The value range is [3, 5].
receiverCallback<TouchGestureEvent>YesCallback used to return touchscreen swipe events asynchronously.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

Disables listening for touchscreen swipe events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of touchscreenSwipe.
fingersnumberYesNumber of fingers that trigger the swipe. The value range is [3, 5].
receiverCallback<TouchGestureEvent>NoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

// Disable listening for a single callback.
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`])}`);
}
// Cancel listening for all callbacks.
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

Enables listening for touchscreen pinch events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of touchscreenPinch.
fingersnumberYesNumber of fingers that trigger the pinch. The value range is [4, 5].
receiverCallback<TouchGestureEvent>YesCallback used to return touchscreen pinch events asynchronously.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

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

Disables listening for touchscreen pinch events.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This field has a fixed value of touchscreenPinch.
fingersnumberYesNumber of fingers that trigger the pinch. The value range is [4, 5].
receiverCallback<TouchGestureEvent>NoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
201Permission denied.
202SystemAPI permission error.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types; 3. Parameter verification failed.

Example

// Disable listening for a single callback.
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`])}`);
}
// Cancel listening for all callbacks.
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

Listens for the press and release events of the specified key, which can be the META_LEFT, META_RIGHT, power, or volume key.

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This parameter has a fixed value of keyPressed.
keysArray<KeyCode>YesKey code list. The options are KEYCODE_META_LEFT, KEYCODE_META_RIGHT, KEYCODE_POWER, KEYCODE_VOLUME_DOWN, and KEYCODE_VOLUME_UP.
receiverCallback<KeyEvent>YesCallback used to receive reported data.

Error codes

For details about the error codes, see Universal Error Codes and Input Monitor Error Codes.

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

Example

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<KeyEvent>): void

Cancels listening for the press and release events of the specified key, which can be the META_LEFT, META_RIGHT, power, or volume key. This API must be used together with inputMonitor.on ('keyPressed').

Required permissions: ohos.permission.INPUT_MONITORING

System capability: SystemCapability.MultimodalInput.Input.InputMonitor

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. This parameter has a fixed value of keyPressed.
receiverCallback<KeyEvent>NoCallback for which listening is disabled. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.

Error codes

For details about the error codes, see Universal Error Codes.

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

Example

// Disable listening for a single callback.
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`])}`);
}
// Cancel listening for all callbacks.
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_DeviceListener

harmony 鸿蒙Input_InterceptorEventCallback

harmony 鸿蒙Input_AxisEvent

harmony 鸿蒙Input_DeviceInfo

harmony 鸿蒙Input_DeviceListener

harmony 鸿蒙Input_Hotkey

harmony 鸿蒙Input_InterceptorEventCallback

harmony 鸿蒙Input_InterceptorOptions

harmony 鸿蒙Input_KeyEvent

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/AlfuaN