# @ohos.inputMethodEngine (Input Method Service)
The **inputMethodEngine** module is oriented to input method applications (including system and third-party input method applications). With the APIs of this module, input method applications are able to create soft keyboard windows, insert or delete characters, select text, and listen for physical keyboard events.
> **NOTE**
>
>The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import inputMethodEngine from '@ohos.inputMethodEngine';
```
## Constants
Provides the constant values of function keys, edit boxes, and the cursor.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
| Name| Type| Value| Description|
| -------- | -------- | -------- | -------- |
| ENTER_KEY_TYPE_UNSPECIFIED | number | 0 | No function is specified for the Enter key.|
| ENTER_KEY_TYPE_GO | number | 2 | The Enter key takes the user to the target.|
| ENTER_KEY_TYPE_SEARCH | number | 3 | The Enter key takes the user to the results of their searching.|
| ENTER_KEY_TYPE_SEND | number | 4 | The Enter key sends the text to its target.|
| ENTER_KEY_TYPE_NEXT | number | 5 | The Enter key takes the user to the next field.|
| ENTER_KEY_TYPE_DONE | number | 6 | The Enter key takes the user to the next line.|
| ENTER_KEY_TYPE_PREVIOUS | number | 7 | The Enter key takes the user to the previous field.|
| PATTERN_NULL | number | -1 | Any type of edit box.|
| PATTERN_TEXT | number | 0 | Text edit box.|
| PATTERN_NUMBER | number | 2 | Number edit box.|
| PATTERN_PHONE | number | 3 | Phone number edit box.|
| PATTERN_DATETIME | number | 4 | Date edit box.|
| PATTERN_EMAIL | number | 5 | Email edit box.|
| PATTERN_URI | number | 6 | URI edit box.|
| PATTERN_PASSWORD | number | 7 | Password edit box.|
| OPTION_ASCII | number | 20 | ASCII values are allowed.|
| OPTION_NONE | number | 0 | No input attribute is specified.|
| OPTION_AUTO_CAP_CHARACTERS | number | 2 | Characters are allowed.|
| OPTION_AUTO_CAP_SENTENCES | number | 8 | Sentences are allowed.|
| OPTION_AUTO_WORDS | number | 4 | Words are allowed.|
| OPTION_MULTI_LINE | number | 1 | Multiple lines are allowed.|
| OPTION_NO_FULLSCREEN | number | 10 | Half-screen style.|
| FLAG_SELECTING | number | 2 | The edit box is being selected.|
| FLAG_SINGLE_LINE | number | 1 | The edit box allows only single-line input.|
| DISPLAY_MODE_PART | number | 0 | The edit box is displayed in half-screen mode.|
| DISPLAY_MODE_FULL | number | 1 | The edit box is displayed in full screen.|
| CURSOR_UP9+ | number | 1 | The caret moves upward.|
| CURSOR_DOWN9+ | number | 2 | The caret moves downward.|
| CURSOR_LEFT9+ | number | 3 | The caret moves leftward.|
| CURSOR_RIGHT9+ | number | 4 | The caret moves rightward.|
| WINDOW_TYPE_INPUT_METHOD_FLOAT9+ | number | 2105 | The input method is displayed in a floating window.|
## inputMethodEngine.getInputMethodAbility9+
getInputMethodAbility(): InputMethodAbility
Obtains an [InputMethodAbility](#inputmethodability) instance for the input method.
The input method can use the obtained instance to subscribe to a soft keyboard display/hide request event.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Return value**
| Type | Description |
| ----------------------------------------- | ------------------ |
| [InputMethodAbility](#inputmethodability) | **InputMethodAbility** instance.|
**Example**
```js
let InputMethodAbility = inputMethodEngine.getInputMethodAbility();
```
## inputMethodEngine.getKeyboardDelegate9+
getKeyboardDelegate(): KeyboardDelegate
Obtains a [KeyboardDelegate](#keyboarddelegate) instance for the input method. The input method can use the obtained instance to subscribe to a physical keyboard event, text selection change event, and more.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Return value**
| Type | Description |
| ------------------------------------- | ------------------------ |
| [KeyboardDelegate](#keyboarddelegate) | **KeyboardDelegate** instance.|
**Example**
```js
let KeyboardDelegate = inputMethodEngine.getKeyboardDelegate();
```
## inputMethodEngine.getInputMethodEngine(deprecated)
getInputMethodEngine(): InputMethodEngine
Obtains an [InputMethodEngine](#inputmethodengine) instance for the input method. The input method can use the obtained instance to subscribe to a soft keyboard display/hide request event.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getInputMethodAbility()](#inputmethodenginegetinputmethodability9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Return value**
| Type | Description |
| ----------------------------------------- | ------------------ |
| [InputMethodEngine](#inputmethodengine) | **InputMethodAbility** instance.|
**Example**
```js
let InputMethodEngine = inputMethodEngine.getInputMethodEngine();
```
## inputMethodEngine.createKeyboardDelegate(deprecated)
createKeyboardDelegate(): KeyboardDelegate
Obtains a [KeyboardDelegate](#keyboarddelegate) instance for the input method. The input method can use the obtained instance to subscribe to a physical keyboard event, text selection change event, and more.
> **NOTE**
>
>This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getKeyboardDelegate()](#inputmethodenginegetkeyboarddelegate9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Return value**
| Type | Description |
| ------------------------------------- | ------------------------ |
| [KeyboardDelegate](#keyboarddelegate) | **KeyboardDelegate** instance.|
**Example**
```js
let keyboardDelegate = inputMethodEngine.createKeyboardDelegate();
```
## InputMethodEngine
In the following API examples, you must first use **[getInputMethodEngine](#inputmethodenginegetinputmethodenginedeprecated)** to obtain an **InputMethodEngine** instance, and then call the APIs using the obtained instance.
### on('inputStart')
on(type: 'inputStart', callback: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void
Enables listening for the input method binding event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
The value **'inputStart'** indicates the input method binding event.|
| callback | (kbController: [KeyboardController](#keyboardcontroller), textInputClient: [TextInputClient](#textinputclientdeprecated)) => void | Yes| Callback used to return the **KeyboardController** and **TextInputClient** instances.|
**Example**
```js
inputMethodEngine.getInputMethodEngine().on('inputStart', (kbController, textClient) => {
let keyboardController = kbController;
let textInputClient = textClient;
});
```
### off('inputStart')
off(type: 'inputStart', callback?: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void
Cancels listening for the input method binding event.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------- | ---- | ------------------------ |
| type | string | Yes | Listening type.
The value **'inputStart'** indicates the input method binding event.|
| callback | (kbController: [KeyboardController](#keyboardcontroller), textInputClient: [TextInputClient](#textinputclientdeprecated)) => void | No| Callback used to return the **KeyboardController** and **TextInputClient** instances.|
**Example**
```js
inputMethodEngine.getInputMethodEngine().off('inputStart', (kbController, textInputClient) => {
console.log('delete inputStart notification.');
});
```
### on('keyboardShow'|'keyboardHide')
on(type: 'keyboardShow'|'keyboardHide', callback: () => void): void
Enables listening for a keyboard visibility event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
- The value **'keyboardShow'** indicates the keyboard display event.
- The value **'keyboardHide'** indicates the keyboard hiding event.|
| callback | () => void | Yes | Callback used to return the result. |
**Example**
```js
inputMethodEngine.getInputMethodEngine().on('keyboardShow', () => {
console.log('inputMethodEngine keyboardShow.');
});
inputMethodEngine.getInputMethodEngine().on('keyboardHide', () => {
console.log('inputMethodEngine keyboardHide.');
});
```
### off('keyboardShow'|'keyboardHide')
off(type: 'keyboardShow'|'keyboardHide', callback?: () => void): void
Disables listening for a keyboard visibility event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
- The value **'keyboardShow'** indicates the keyboard display event.
- The value **'keyboardHide'** indicates the keyboard hiding event.|
| callback | () => void | No | Callback used to return the result.|
**Example**
```js
inputMethodEngine.getInputMethodEngine().off('keyboardShow');
inputMethodEngine.getInputMethodEngine().off('keyboardHide');
```
## InputMethodAbility
In the following API examples, you must first use **[getInputMethodAbility](#inputmethodenginegetinputmethodability9)** to obtain an **InputMethodAbility** instance, and then call the APIs using the obtained instance.
### on('inputStart')9+
on(type: 'inputStart', callback: (kbController: KeyboardController, inputClient: InputClient) => void): void
Enables listening for the input method binding event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
The value **'inputStart'** indicates the input method binding event.|
| callback | (kbController: [KeyboardController](#keyboardcontroller), inputClient: [InputClient](#inputclient9)) => void | Yes| Callback used to return the result.|
**Example**
```js
inputMethodEngine.getInputMethodAbility().on('inputStart', (kbController, client) => {
let keyboardController = kbController;
let inputClient = client;
});
```
### off('inputStart')9+
off(type: 'inputStart', callback?: (kbController: KeyboardController, inputClient: InputClient) => void): void
Cancels listening for the input method binding event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------- | ---- | ------------------------ |
| type | string | Yes | Listening type.
The value **'inputStart'** indicates the input method binding event.|
| callback | (kbController: [KeyboardController](#keyboardcontroller), inputClient: [InputClient](#inputclient9)) => void | No| Callback used to return the result.|
**Example**
```js
inputMethodEngine.getInputMethodAbility().off('inputStart');
```
### on('inputStop')9+
on(type: 'inputStop', callback: () => void): void
Enables listening for the input method unbinding event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
The value **'inputStop'** indicates the input method unbinding event.|
| callback | () => void | Yes | Callback used to return the result. |
**Example**
```js
inputMethodEngine.getInputMethodAbility().on('inputStop', () => {
console.log('inputMethodAbility inputStop');
});
```
### off('inputStop')9+
off(type: 'inputStop', callback: () => void): void
Cancels listening for the input method stop event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
The value **'inputStop'** indicates the input method unbinding event.|
| callback | () => void | Yes | Callback used to return the result. |
**Example**
```js
inputMethodEngine.getInputMethodAbility().off('inputStop', () => {
console.log('inputMethodAbility delete inputStop notification.');
});
```
### on('setCallingWindow')9+
on(type: 'setCallingWindow', callback: (wid: number) => void): void
Enables listening for the window invocation setting event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
The value **'setCallingWindow'** indicates the window invocation setting event.|
| callback | (wid: number) => void | Yes | Callback used to return the window ID of the caller. |
**Example**
```js
inputMethodEngine.getInputMethodAbility().on('setCallingWindow', (wid) => {
console.log('inputMethodAbility setCallingWindow');
});
```
### off('setCallingWindow')9+
off(type: 'setCallingWindow', callback: (wid:number) => void): void
Disables listening for the window invocation setting event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
The value **'setCallingWindow'** indicates the window invocation setting event.|
| callback | (wid:number) => void | Yes | Callback used to return the window ID of the caller. |
**Example**
```js
inputMethodEngine.getInputMethodAbility().off('setCallingWindow', () => {
console.log('inputMethodAbility delete setCallingWindow notification.');
});
```
### on('keyboardShow'|'keyboardHide')9+
on(type: 'keyboardShow'|'keyboardHide', callback: () => void): void
Enables listening for a keyboard visibility event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
- The value **'keyboardShow'** indicates the keyboard display event.
- The value **'keyboardHide'** indicates the keyboard hiding event.|
| callback | () => void | Yes | Callback used to return the result. |
**Example**
```js
inputMethodEngine.getInputMethodAbility().on('keyboardShow', () => {
console.log('InputMethodAbility keyboardShow.');
});
inputMethodEngine.getInputMethodAbility().on('keyboardHide', () => {
console.log('InputMethodAbility keyboardHide.');
});
```
### off('keyboardShow'|'keyboardHide')9+
off(type: 'keyboardShow'|'keyboardHide', callback?: () => void): void
Disables listening for a keyboard visibility event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
- The value **'keyboardShow'** indicates the keyboard display event.
- The value **'keyboardHide'** indicates the keyboard hiding event.|
| callback | () => void | No | Callback used to return the result. |
**Example**
```js
inputMethodEngine.getInputMethodAbility().off('keyboardShow', () => {
console.log('InputMethodAbility delete keyboardShow notification.');
});
inputMethodEngine.getInputMethodAbility().off('keyboardHide', () => {
console.log('InputMethodAbility delete keyboardHide notification.');
});
```
### on('setSubtype')9+
on(type: 'setSubtype', callback: (inputMethodSubtype: InputMethodSubtype) => void): void
Enables listening for the input method subtype setting event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type| Mandatory | Description|
| -------- | --- | ---- | --- |
| type | string | Yes | Listening type.
The value **'setSubtype'** indicates the input method subtype setting event.|
| callback | (inputMethodSubtype: [InputMethodSubtype](js-apis-inputmethod-subtype.md)) => void | Yes | Callback used to return the input method subtype. |
**Example**
```js
inputMethodEngine.getInputMethodAbility().on('setSubtype', (inputMethodSubtype) => {
console.log('InputMethodAbility setSubtype.');
});
```
### off('setSubtype')9+
off(type: 'setSubtype', callback?: (inputMethodSubtype: InputMethodSubtype) => void): void
Disables listening for the input method subtype setting event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| ------- | ----- | ---- | ---- |
| type | string | Yes | Listening type.
The value **'setSubtype'** indicates the input method subtype setting event.|
| callback | (inputMethodSubtype: [InputMethodSubtype](js-apis-inputmethod-subtype.md)) => void | No | Callback used to return the input method subtype. |
**Example**
```js
inputMethodEngine.getInputMethodAbility().off('setSubtype', () => {
console.log('InputMethodAbility delete setSubtype notification.');
});
```
## KeyboardDelegate
In the following API examples, you must first use **[getKeyboardDelegate](#inputmethodenginegetkeyboarddelegate9)** to obtain a **KeyboardDelegate** instance, and then call the APIs using the obtained instance.
### on('keyDown'|'keyUp')
on(type: 'keyDown'|'keyUp', callback: (event: KeyEvent) => boolean): void
Enables listening for a keyboard event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
The value **'keyDown'** indicates the keydown event.
The value **'keyUp'** indicates the keyup event.|
| callback | (event: [KeyEvent](#keyevent)) => boolean | Yes| Callback used to return the key information.|
**Example**
```js
inputMethodEngine.getKeyboardDelegate().on('keyUp', (keyEvent) => {
console.info('inputMethodEngine keyCode.(keyUp):' + JSON.stringify(keyEvent.keyCode));
console.info('inputMethodEngine keyAction.(keyUp):' + JSON.stringify(keyEvent.keyAction));
return true;
});
inputMethodEngine.getKeyboardDelegate().on('keyDown', (keyEvent) => {
console.info('inputMethodEngine keyCode.(keyDown):' + JSON.stringify(keyEvent.keyCode));
console.info('inputMethodEngine keyAction.(keyDown):' + JSON.stringify(keyEvent.keyAction));
return true;
});
```
### off('keyDown'|'keyUp')
off(type: 'keyDown'|'keyUp', callback?: (event: KeyEvent) => boolean): void
Disables listening for a keyboard event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------- | ---- | ----- |
| type | string | Yes | Listening type.
The value **'keyDown'** indicates the keydown event.
The value **'keyUp'** indicates the keyup event.|
| callback | (event: [KeyEvent](#keyevent)) => boolean | No | Callback used to return the key information. |
**Example**
```js
inputMethodEngine.getKeyboardDelegate().off('keyUp', (keyEvent) => {
console.log('delete keyUp notification.');
return true;
});
inputMethodEngine.getKeyboardDelegate().off('keyDown', (keyEvent) => {
console.log('delete keyDown notification.');
return true;
});
```
### on('cursorContextChange')
on(type: 'cursorContextChange', callback: (x: number, y:number, height:number) => void): void
Enables listening for the cursor change event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---- | ---- | ----- |
| type | string | Yes | Listening type.
The value **'cursorContextChange'** indicates the cursor change event.|
| callback | (x: number, y: number, height: number) => void | Yes | Callback used to return the cursor information.
- **x**: x coordinate of the top of the cursor.
- **y**: x coordinate of the bottom of the cursor.
- **height**: height of the cursor.|
**Example**
```js
inputMethodEngine.getKeyboardDelegate().on('cursorContextChange', (x, y, height) => {
console.log('inputMethodEngine cursorContextChange x:' + x);
console.log('inputMethodEngine cursorContextChange y:' + y);
console.log('inputMethodEngine cursorContextChange height:' + height);
});
```
### off('cursorContextChange')
off(type: 'cursorContextChange', callback?: (x: number, y: number, height: number) => void): void
Cancels listening for cursor context changes. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---- | ---- | ------ |
| type | string | Yes | Listening type.
The value **'cursorContextChange'** indicates the cursor change event.|
| callback | (x: number, y:number, height:number) => void | No | Callback used to return the cursor information.
- **x**: x coordinate of the top of the cursor.
- **y**: x coordinate of the bottom of the cursor.
- **height**: height of the cursor.
|
**Example**
```js
inputMethodEngine.getKeyboardDelegate().off('cursorContextChange', (x, y, height) => {
console.log('delete cursorContextChange notification.');
});
```
### on('selectionChange')
on(type: 'selectionChange', callback: (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void): void
Enables listening for the text selection change event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ----- | ---- | ---- |
| type | string | Yes | Listening type.
The value **'selectionChange'** indicates the text selection change event.|
| callback | (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void | Yes | Callback used to return the text selection information.
- **oldBegin**: start of the selected text before the change.
- **oldEnd**: end of the selected text before the change.
- **newBegin**: start of the selected text after the change.
- **newEnd**: end of the selected text after the change.|
**Example**
```js
inputMethodEngine.getKeyboardDelegate().on('selectionChange', (oldBegin, oldEnd, newBegin, newEnd) => {
console.log('inputMethodEngine beforeEach selectionChange oldBegin:' + oldBegin);
console.log('inputMethodEngine beforeEach selectionChange oldEnd:' + oldEnd);
console.log('inputMethodEngine beforeEach selectionChange newBegin:' + newBegin);
console.log('inputMethodEngine beforeEach selectionChange newEnd:' + newEnd);
});
```
### off('selectionChange')
off(type: 'selectionChange', callback?: (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void): void
Cancels listening for text selection changes. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------- | ---- | ------- |
| type | string | Yes | Listening type.
The value **'selectionChange'** indicates the text selection change event.|
| callback | (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void | No | Callback used to return the text selection information.
- **oldBegin**: start of the selected text before the change.
- **oldEnd**: end of the selected text before the change.
- **newBegin**: start of the selected text after the change.
- **newEnd**: end of the selected text after the change.
|
**Example**
```js
inputMethodEngine.getKeyboardDelegate().off('selectionChange', (oldBegin, oldEnd, newBegin, newEnd) => {
console.log('delete selectionChange notification.');
});
```
### on('textChange')
on(type: 'textChange', callback: (text: string) => void): void
Enables listening for the text change event. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
The value **'textChange'** indicates the text change event.|
| callback | (text: string) => void | Yes | Callback used to return the text content.|
**Example**
```js
inputMethodEngine.getKeyboardDelegate().on('textChange', (text) => {
console.log('inputMethodEngine textChange. text:' + text);
});
```
### off('textChange')
off(type: 'textChange', callback?: (text: string) => void): void
Cancels listening for text changes. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type | string | Yes | Listening type.
The value **'textChange'** indicates the text change event.|
| callback | (text: string) => void | No | Callback used to return the text content.|
**Example**
```js
inputMethodEngine.getKeyboardDelegate().off('textChange', (text) => {
console.log('delete textChange notification. text:' + text);
});
```
## KeyboardController
In the following API examples, you must first use **[on('inputStart')](#oninputstart9)** to obtain a **KeyboardController** instance, and then call the APIs using the obtained instance.
### hide9+
hide(callback: AsyncCallback<void>): void
Hides the keyboard. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ---------------------- | ---- | -------- |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800003 | input method client error. |
**Example**
```js
keyboardController.hide((err) => {
if (err !== undefined) {
console.error('Failed to hide keyboard: ' + JSON.stringify(err));
return;
}
console.log('Succeeded in hiding keyboard.');
});
```
### hide9+
hide(): Promise<void>
Hides the keyboard. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Return value**
| Type | Description |
| ---------------- | ------------------------- |
| Promise<void> | Promise that returns no value.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800003 | input method client error. |
**Example**
```js
keyboardController.hide().then(() => {
console.info('Succeeded in hiding keyboard.');
}).catch((err) => {
console.info('Failed to hide keyboard: ' + JSON.stringify(err));
});
```
### hideKeyboard(deprecated)
hideKeyboard(callback: AsyncCallback<void>): void
Hides the keyboard. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [hide](#hide9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ---------------------- | ---- | -------- |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.|
**Example**
```js
keyboardController.hideKeyboard((err) => {
if (err !== undefined) {
console.error('Failed to hide Keyboard: ' + JSON.stringify(err));
return;
}
console.log('Succeeded in hiding keyboard.');
});
```
### hideKeyboard(deprecated)
hideKeyboard(): Promise<void>
Hides the keyboard. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [hide](#hide9-1) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Return value**
| Type | Description |
| ---------------- | ------------------------- |
| Promise<void> | Promise that returns no value.|
**Example**
```js
keyboardController.hideKeyboard().then(() => {
console.info('Succeeded in hiding keyboard.');
}).catch((err) => {
console.info('Failed to hide Keyboard: ' + JSON.stringify(err));
});
```
## InputClient9+
In the following API examples, you must first use **[on('inputStart')](#oninputstart9)** to obtain an **InputClient** instance, and then call the APIs using the obtained instance.
### sendKeyFunction9+
sendKeyFunction(action:number, callback: AsyncCallback<boolean>): void
Sends the function key. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| action | number | Yes| Action of the function key.
- **0**: invalid key.
- **1**: confirm key (Enter key).|
| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is **true**. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800003 | input method client error. |
**Example**
```js
let action = 1;
try {
inputClient.sendKeyFunction(action, (err, result) => {
if (err !== undefined) {
console.error('Failed to sendKeyFunction: ' + JSON.stringify(err));
return;
}
if (result) {
console.info('Succeeded in sending key function. ');
} else {
console.error('Failed to sendKeyFunction. ');
}
});
} catch (err) {
console.error('sendKeyFunction err: ' + JSON.stringify(err));
}
```
### sendKeyFunction9+
sendKeyFunction(action: number): Promise<boolean>
Sends the function key. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| action | number | Yes| Action of the function key.
- **0**: invalid key.
- **1**: confirm key (Enter key).|
**Return value**
| Type | Description |
| ------------------------------- | ------------------------------------------------------------ |
| Promise<boolean> | Promise used to return the result. The value **true** means that the operation is successful, and **false** means the opposite.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800003 | input method client error. |
**Example**
```js
let action = 1;
try {
inputClient.sendKeyFunction(action).then((result) => {
if (result) {
console.info('Succeeded in sending key function. ');
} else {
console.error('Failed to sendKeyFunction. ');
}
}).catch((err) => {
console.error('Failed to sendKeyFunction:' + JSON.stringify(err));
});
} catch (err) {
console.error('Failed to sendKeyFunction: ' + JSON.stringify(err));
}
```
### getForward9+
getForward(length:number, callback: AsyncCallback<string>): void
Obtains the specific-length text before the cursor. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
| callback | AsyncCallback<string> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the obtained text. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | ------------------------------ |
| 12800003 | input method client error. |
| 12800006 | Input method controller error. |
**Example**
```js
let length = 1;
try {
inputClient.getForward(length, (err, text) => {
if (err !== undefined) {
console.error('Failed to getForward: ' + JSON.stringify(err));
return;
}
console.log('Succeeded in getting forward, text: ' + text);
});
} catch (err) {
console.error('Failed to getForward: ' + JSON.stringify(err));
}
```
### getForward9+
getForward(length:number): Promise<string>
Obtains the specific-length text before the cursor. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
**Return value**
| Type | Description |
| ------------------------------- | ------------------------------------------------------------ |
| Promise<string> | Promise used to return the specific-length text before the cursor. |
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | ------------------------------ |
| 12800003 | input method client error. |
| 12800006 | Input method controller error. |
**Example**
```js
let length = 1;
try {
inputClient.getForward(length).then((text) => {
console.info('Succeeded in getting forward, text: ' + text);
}).catch((err) => {
console.error('Failed to getForward: ' + JSON.stringify(err));
});
} catch (err) {
console.error('Failed to getForward: ' + JSON.stringify(err));
}
```
### getBackward9+
getBackward(length:number, callback: AsyncCallback<string>): void
Obtains the specific-length text after the cursor. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
| callback | AsyncCallback<string> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the obtained text. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | ------------------------------ |
| 12800003 | input method client error. |
| 12800006 | Input method controller error. |
**Example**
```js
let length = 1;
try {
inputClient.getBackward(length, (err, text) => {
if (err !== undefined) {
console.error('Failed to getForward: ' + JSON.stringify(err));
return;
}
console.log('Succeeded in getting backward, text: ' + text);
});
} catch (err) {
console.error('Failed to getForward: ' + JSON.stringify(err));
}
```
### getBackward9+
getBackward(length:number): Promise<string>
Obtains the specific-length text after the cursor. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
**Return value**
| Type | Description |
| ------------------------------- | ------------------------------------------------------------ |
| Promise<string> | Promise used to return the specific-length text after the cursor. |
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | ------------------------------ |
| 12800003 | input method client error. |
| 12800006 | Input method controller error. |
**Example**
```js
let length = 1;
try {
inputClient.getBackward(length).then((text) => {
console.info('Succeeded in getting backward, text: ' + text);
}).catch((err) => {
console.error('Failed to getForward: ' + JSON.stringify(err));
});
} catch (err) {
console.error(`Failed to getBackward: ${JSON.stringify(err)}`);
}
```
### deleteForward9+
deleteForward(length:number, callback: AsyncCallback<boolean>): void
Deletes the fixed-length text before the cursor. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is **true**. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800002 | input method engine error. |
| 12800003 | input method client error. |
**Example**
```js
let length = 1;
try {
inputClient.deleteForward(length, (err, result) => {
if (err !== undefined) {
console.error('Failed to delete forward: ' + JSON.stringify(err));
return;
}
if (result) {
console.info('Succeeded in deleting forward. ');
} else {
console.error('Failed to delete forward: ' + JSON.stringify(err));
}
});
} catch (err) {
console.error('Failed to delete forward: ' + JSON.stringify(err));
}
```
### deleteForward9+
deleteForward(length:number): Promise<boolean>
Deletes the fixed-length text before the cursor. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ---------- |
| length | number | Yes | Text length.|
**Return value**
| Type | Description |
| ---------------------- | -------------- |
| Promise<boolean> | Promise used to return the result. The value **true** means that the operation is successful, and **false** means the opposite.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800002 | Input method engine error. |
| 12800003 | input method client error. |
**Example**
```js
let length = 1;
try {
inputClient.deleteForward(length).then((result) => {
if (result) {
console.info('Succeeded in deleting forward. ');
} else {
console.error('Failed to delete Forward. ');
}
}).catch((err) => {
console.error('Failed to delete Forward: ' + JSON.stringify(err));
});
} catch (err) {
console.error(`Failed to deleteForward: ${JSON.stringify(err)}`);
}
```
### deleteBackward9+
deleteBackward(length:number, callback: AsyncCallback<boolean>): void
Deletes the fixed-length text after the cursor. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ---------------------------- | ---- | -------------- |
| length | number | Yes | Text length. |
| callback | AsyncCallback<boolean> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is **true**. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800002 | Input method engine error. |
| 12800003 | input method client error. |
**Example**
```js
let length = 1;
try {
inputClient.deleteBackward(length, (err, result) => {
if (err !== undefined) {
console.error('Failed to delete Backward: ' + JSON.stringify(err));
return;
}
if (result) {
console.info('Succeeded in deleting backward. ');
} else {
console.error('Failed to delete Backward: ' + JSON.stringify(err));
}
});
} catch (err) {
console.error('deleteBackward err: ' + JSON.stringify(err));
}
```
### deleteBackward9+
deleteBackward(length:number): Promise<boolean>
Deletes the fixed-length text after the cursor. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
**Return value**
| Type | Description |
| ------------------------------- | ------------------------------------------------------------ |
| Promise<boolean> | Promise used to return the result. The value **true** means that the deletion is successful, and **false** means the opposite.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800002 | Input method engine error. |
| 12800003 | input method client error. |
**Example**
```js
let length = 1;
inputClient.deleteBackward(length).then((result) => {
if (result) {
console.info('Succeeded in deleting backward. ');
} else {
console.error('Failed to deleteBackward. ');
}
}).catch((err) => {
console.error('Failed to deleteBackward: ' + JSON.stringify(err));
});
```
### insertText9+
insertText(text:string, callback: AsyncCallback<boolean>): void
Inserts text. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| Text to insert.|
| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is **true**. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800002 | Input method engine error. |
| 12800003 | input method client error. |
**Example**
```js
inputClient.insertText('test', (err, result) => {
if (err !== undefined) {
console.error('Failed to insertText: ' + JSON.stringify(err));
return;
}
if (result) {
console.info('Succeeded in inserting text. ');
} else {
console.error('Failed to insertText. ');
}
});
```
### insertText9+
insertText(text:string): Promise<boolean>
Inserts text. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| Text to insert.|
**Return value**
| Type | Description |
| ------------------------------- | ------------------------------------------------------------ |
| Promise<boolean> | Promise used to return the result. The value **true** means that the insertion is successful, and **false** means the opposite. |
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800002 | Input method engine error. |
| 12800003 | input method client error. |
**Example**
```js
try {
inputClient.insertText('test').then((result) => {
if (result) {
console.info('Succeeded in inserting text. ');
} else {
console.error('Failed to insertText. ');
}
}).catch((err) => {
console.error('Failed to insertText: ' + JSON.stringify(err));
});
} catch (err) {
console.error('Failed to insertText: ' + JSON.stringify(err));
}
```
### getEditorAttribute9+
getEditorAttribute(callback: AsyncCallback<EditorAttribute>): void
Obtains the attribute of the edit box. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory | Description |
| ------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
| callback | AsyncCallback<[EditorAttribute](#editorattribute)> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the attribute of the edit box. Otherwise, **err** is an error object.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800003 | input method client error. |
**Example**
```js
inputClient.getEditorAttribute((err, editorAttribute) => {
if (err !== undefined) {
console.error('Failed to getEditorAttribute: ' + JSON.stringify(err));
return;
}
console.log('editorAttribute.inputPattern: ' + JSON.stringify(editorAttribute.inputPattern));
console.log('editorAttribute.enterKeyType: ' + JSON.stringify(editorAttribute.enterKeyType));
});
```
### getEditorAttribute9+
getEditorAttribute(): Promise<EditorAttribute>
Obtains the attribute of the edit box. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Return value**
| Type | Description |
| ------------------------------- | ------------------------------------------------------------ |
| Promise<[EditorAttribute](#editorattribute)> | Promise used to return the attribute of the edit box. |
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800003 | input method client error. |
**Example**
```js
inputClient.getEditorAttribute().then((editorAttribute) => {
console.info('editorAttribute.inputPattern: ' + JSON.stringify(editorAttribute.inputPattern));
console.info('editorAttribute.enterKeyType: ' + JSON.stringify(editorAttribute.enterKeyType));
}).catch((err) => {
console.error('Failed to getEditorAttribute: ' + JSON.stringify(err));
});
```
### moveCursor9+
moveCursor(direction: number, callback: AsyncCallback<void>): void
Moves the cursor. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| --------- | ------------------------- | ---- | -------------- |
| direction | number | Yes | Direction in which the cursor moves.|
| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. |
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800003 | input method client error. |
**Example**
```js
try {
inputClient.moveCursor(inputMethodEngine.CURSOR_UP, (err) => {
if (err !== undefined) {
console.error('Failed to moveCursor: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in moving cursor.');
});
} catch (err) {
console.error('Failed to moveCursor: ' + JSON.stringify(err));
}
```
### moveCursor9+
moveCursor(direction: number): Promise<void>
Moves the cursor. This API uses a promise to return the result.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| --------- | ------ | ---- | -------------- |
| direction | number | Yes | Direction in which the cursor moves.|
**Return value**
| Type | Description |
| ------------------- | ------------------------- |
| Promise<void> | Promise that returns no value.|
**Error codes**
For details about the error codes, see [Input Method Framework Error Codes](../errorcodes/errorcode-inputmethod-framework.md).
| Error Code ID| Error Message |
| -------- | -------------------------- |
| 12800003 | input method client error. |
**Example**
```js
try {
inputClient.moveCursor(inputMethodEngine.CURSOR_UP).then(() => {
console.log('Succeeded in moving cursor.');
}).catch((err) => {
console.error('Failed to moveCursor: ' + JSON.stringify(err));
});
} catch (err) {
console.log('Failed to moveCursor: ' + JSON.stringify(err));
}
```
## EditorAttribute
Attribute of the edit box.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
| Name | Type| Readable| Writable| Description |
| ------------ | -------- | ---- | ---- | ------------------ |
| enterKeyType | number | Yes | No | Function attribute of the edit box.|
| inputPattern | number | Yes | No | Text attribute of the edit box.|
## KeyEvent
Describes the attribute of a key.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
| Name | Type| Readable| Writable| Description |
| --------- | -------- | ---- | ---- | ------------ |
| keyCode | number | Yes | No | Key value. For detail, see [KeyCode](js-apis-keycode.md#keycode).|
| keyAction | number | Yes | No | Key event type.
- **2**: keydown event.
- **3**: keyup event.|
## TextInputClient(deprecated)
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [InputClient](#inputclient9) instead.
In the following API examples, you must first use **[on('inputStart')](#oninputstart)** to obtain a **TextInputClient** instance, and then call the APIs using the obtained instance.
### getForward(deprecated)
getForward(length:number, callback: AsyncCallback<string>): void
Obtains the specific-length text before the cursor. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getForward](#getforward9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
| callback | AsyncCallback<string> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the obtained text. Otherwise, **err** is an error object.|
**Example**
```js
let length = 1;
textInputClient.getForward(length, (err, text) => {
if (err !== undefined) {
console.error('Failed to getForward: ' + JSON.stringify(err));
return;
}
console.log('Succeeded in getting forward, text: ' + text);
});
```
### getForward(deprecated)
getForward(length:number): Promise<string>
Obtains the specific-length text before the cursor. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getForward](#getforward9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
**Return value**
| Type | Description |
| ------------------------------- | ------------------------------------------------------------ |
| Promise<string> | Promise used to return the specific-length text before the cursor. |
**Example**
```js
let length = 1;
textInputClient.getForward(length).then((text) => {
console.info('Succeeded in getting forward, text: ' + text);
}).catch((err) => {
console.error('Failed to getForward: ' + JSON.stringify(err));
});
```
### getBackward(deprecated)
getBackward(length:number, callback: AsyncCallback<string>): void
Obtains the specific-length text after the cursor. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getBackward](#getbackward9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
| callback | AsyncCallback<string> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the obtained text. Otherwise, **err** is an error object.|
**Example**
```js
let length = 1;
textInputClient.getBackward(length, (err, text) => {
if (err !== undefined) {
console.error('Failed to getBackward: ' + JSON.stringify(err));
return;
}
console.log('Succeeded in getting borward, text: ' + text);
});
```
### getBackward(deprecated)
getBackward(length:number): Promise<string>
Obtains the specific-length text after the cursor. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getBackward](#getbackward9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
**Return value**
| Type | Description |
| ------------------------------- | ------------------------------------------------------------ |
| Promise<string> | Promise used to return the specific-length text after the cursor. |
**Example**
```js
let length = 1;
textInputClient.getBackward(length).then((text) => {
console.info('Succeeded in getting backward: ' + JSON.stringify(text));
}).catch((err) => {
console.error('Failed to getBackward: ' + JSON.stringify(err));
});
```
### deleteForward(deprecated)
deleteForward(length:number, callback: AsyncCallback<boolean>): void
Deletes the fixed-length text before the cursor. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [deleteForward](#deleteforward9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is **true**. Otherwise, **err** is an error object.|
**Example**
```js
let length = 1;
textInputClient.deleteForward(length, (err, result) => {
if (err !== undefined) {
console.error('Failed to deleteForward: ' + JSON.stringify(err));
return;
}
if (result) {
console.info('Succeeded in deleting forward. ');
} else {
console.error('Failed to deleteForward. ');
}
});
```
### deleteForward(deprecated)
deleteForward(length:number): Promise<boolean>
Deletes the fixed-length text before the cursor. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [deleteForward](#deleteforward9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ---------- |
| length | number | Yes | Text length.|
**Return value**
| Type | Description |
| ---------------------- | -------------- |
| Promise<boolean> | Promise used to return the result. The value **true** means that the deletion is successful, and **false** means the opposite.|
**Example**
```js
let length = 1;
textInputClient.deleteForward(length).then((result) => {
if (result) {
console.info('Succeeded in deleting forward. ');
} else {
console.error('Failed to delete forward. ');
}
}).catch((err) => {
console.error('Failed to delete forward: ' + JSON.stringify(err));
});
```
### deleteBackward(deprecated)
deleteBackward(length:number, callback: AsyncCallback<boolean>): void
Deletes the fixed-length text after the cursor. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [deleteBackward](#deletebackward9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ---------------------------- | ---- | -------------- |
| length | number | Yes | Text length. |
| callback | AsyncCallback<boolean> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is **true**. Otherwise, **err** is an error object.|
**Example**
```js
let length = 1;
textInputClient.deleteBackward(length, (err, result) => {
if (err !== undefined) {
console.error('Failed to delete backward: ' + JSON.stringify(err));
return;
}
if (result) {
console.info('Succeeded in deleting backward. ');
} else {
console.error('Failed to deleteBackward. ');
}
});
```
### deleteBackward(deprecated)
deleteBackward(length:number): Promise<boolean>
Deletes the fixed-length text after the cursor. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [deleteBackward](#deletebackward9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
**Return value**
| Type | Description |
| ------------------------------- | ------------------------------------------------------------ |
| Promise<boolean> | Promise used to return the result. The value **true** means that the deletion is successful, and **false** means the opposite.|
**Example**
```js
let length = 1;
textInputClient.deleteBackward(length).then((result) => {
if (result) {
console.info('Succeeded in deleting backward. ');
} else {
console.error('Failed to deleteBackward. ');
}
}).catch((err) => {
console.error('Failed to deleteBackward: ' + JSON.stringify(err));
});
```
### sendKeyFunction(deprecated)
sendKeyFunction(action: number, callback: AsyncCallback<boolean>): void
Sends the function key. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [sendKeyFunction](#sendkeyfunction9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| action | number | Yes| Action of the function key.
- **0**: invalid key.
- **1**: confirm key (Enter key).|
| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is **true**. Otherwise, **err** is an error object.|
**Example**
```js
let action = 1;
textInputClient.sendKeyFunction(action, (err, result) => {
if (err !== undefined) {
console.error('Failed to sendKeyFunction: ' + JSON.stringify(err));
return;
}
if (result) {
console.info('Succeeded in sending key function. ');
} else {
console.error('Failed to sendKeyFunction. ');
}
});
```
### sendKeyFunction(deprecated)
sendKeyFunction(action: number): Promise<boolean>
Sends the function key. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [sendKeyFunction](#sendkeyfunction9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| action | number | Yes| Action of the function key.
- **0**: invalid key.
- **1**: confirm key (Enter key).|
**Return value**
| Type | Description |
| ------------------------------- | ------------------------------------------------------------ |
| Promise<boolean> | Promise used to return the result. The value **true** means that the setting is successful, and **false** means the opposite.|
**Example**
```js
let action = 1;
textInputClient.sendKeyFunction(action).then((result) => {
if (result) {
console.info('Succeeded in sending key function. ');
} else {
console.error('Failed to sendKeyFunction. ');
}
}).catch((err) => {
console.error('Failed to sendKeyFunction:' + JSON.stringify(err));
});
```
### insertText(deprecated)
insertText(text:string, callback: AsyncCallback<boolean>): void
Inserts text. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [insertText](#inserttext9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| Text to insert.|
| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is **true**. Otherwise, **err** is an error object.|
**Example**
```js
textInputClient.insertText('test', (err, result) => {
if (err !== undefined) {
console.error('Failed to insertText: ' + JSON.stringify(err));
return;
}
if (result) {
console.info('Succeeded in inserting text. ');
} else {
console.error('Failed to insertText. ');
}
});
```
### insertText(deprecated)
insertText(text:string): Promise<boolean>
Inserts text. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [insertText](#inserttext9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| Text to insert.|
**Return value**
| Type | Description |
| ------------------------------- | ------------------------------------------------------------ |
| Promise<boolean> | Promise used to return the result. The value **true** means that the insertion is successful, and **false** means the opposite.|
**Example**
```js
textInputClient.insertText('test').then((result) => {
if (result) {
console.info('Succeeded in inserting text. ');
} else {
console.error('Failed to insertText. ');
}
}).catch((err) => {
console.error('Failed to insertText: ' + JSON.stringify(err));
});
```
### getEditorAttribute(deprecated)
getEditorAttribute(callback: AsyncCallback<EditorAttribute>): void
Obtains the attribute of the edit box. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getEditorAttribute](#geteditorattribute9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ----- | ----- | ----- |
| callback | AsyncCallback<[EditorAttribute](#editorattribute)> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the attribute of the edit box. Otherwise, **err** is an error object.|
**Example**
```js
textInputClient.getEditorAttribute((err, editorAttribute) => {
if (err !== undefined) {
console.error('Failed to getEditorAttribute: ' + JSON.stringify(err));
return;
}
console.log('editorAttribute.inputPattern: ' + JSON.stringify(editorAttribute.inputPattern));
console.log('editorAttribute.enterKeyType: ' + JSON.stringify(editorAttribute.enterKeyType));
});
```
### getEditorAttribute(deprecated)
getEditorAttribute(): Promise<EditorAttribute>
Obtains the attribute of the edit box. This API uses a promise to return the result.
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getEditorAttribute](#geteditorattribute9) instead.
**System capability**: SystemCapability.MiscServices.InputMethodFramework
**Return value**
| Type | Description |
| ------------------------------- | ------------------------------------------------------------ |
| Promise<[EditorAttribute](#editorattribute)> | Promise used to return the attribute of the edit box. |
**Example**
```js
textInputClient.getEditorAttribute().then((editorAttribute) => {
console.info('editorAttribute.inputPattern: ' + JSON.stringify(editorAttribute.inputPattern));
console.info('editorAttribute.enterKeyType: ' + JSON.stringify(editorAttribute.enterKeyType));
}).catch((err) => {
console.error('Failed to getEditorAttribute: ' + JSON.stringify(err));
});
```