js-apis-inputmethodengine.md 42.7 KB
Newer Older
E
ester.zhou 已提交
1 2
# Input Method Engine

3 4 5
The **inputMethodEngine** module streamlines the interaction between applications and input methods. By calling APIs of this module, applications can accept text input through the input methods, be bound to input method services, request the keyboard to display or hide, listen for the input method status, and much more.

> **NOTE**
E
ester.zhou 已提交
6
>
7
> 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.
E
ester.zhou 已提交
8 9 10 11

## Modules to Import

```
12
import inputMethodEngine from '@ohos.inputmethodengine';
E
ester.zhou 已提交
13 14 15 16
```

## inputMethodEngine

17
Provides the constants.
E
ester.zhou 已提交
18

19
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| ENTER_KEY_TYPE_UNSPECIFIED | number | Yes| No| No function is specified for the Enter key.|
| ENTER_KEY_TYPE_GO | number | Yes| No| The Enter key takes the user to the target.|
| ENTER_KEY_TYPE_SEARCH | number | Yes| No| The Enter key takes the user to the results of their searching.|
| ENTER_KEY_TYPE_SEND | number | Yes| No| The Enter key sends the text to its target.|
| ENTER_KEY_TYPE_NEXT | number | Yes| No| The Enter key takes the user to the next field.|
| ENTER_KEY_TYPE_DONE | number | Yes| No| The Enter key takes the user to the next line.|
| ENTER_KEY_TYPE_PREVIOUS | number | Yes| No| The Enter key takes the user to the previous field.|
| PATTERN_NULL | number | Yes| No| Any type of edit box.|
| PATTERN_TEXT | number | Yes| No| Text edit box.|
| PATTERN_NUMBER | number | Yes| No| Number edit box.|
| PATTERN_PHONE | number | Yes| No| Phone number edit box.|
| PATTERN_DATETIME | number | Yes| No| Date edit box.|
| PATTERN_EMAIL | number | Yes| No| Email edit box.|
| PATTERN_URI | number | Yes| No| URI edit box.|
| PATTERN_PASSWORD | number | Yes| No| Password edit box.|
| OPTION_ASCII | number | Yes| No| ASCII values are allowed.|
| OPTION_NONE | number | Yes| No| No input attribute is specified.|
| OPTION_AUTO_CAP_CHARACTERS | number | Yes| No| Characters are allowed.|
| OPTION_AUTO_CAP_SENTENCES | number | Yes| No| Sentences are allowed.|
| OPTION_AUTO_WORDS | number | Yes| No| Words are allowed.|
| OPTION_MULTI_LINE | number | Yes| No| Multiple lines are allowed.|
| OPTION_NO_FULLSCREEN | number | Yes| No| Half-screen style.|
| FLAG_SELECTING | number | Yes| No| The edit box is being selected.|
| FLAG_SINGLE_LINE | number | Yes| No| The edit box allows only single-line input.|
| DISPLAY_MODE_PART | number | Yes| No| The edit box is displayed in half-screen mode.|
| DISPLAY_MODE_FULL | number | Yes| No| The edit box is displayed in full screen.|
E
ester.zhou 已提交
49 50 51 52 53
| CURSOR_UP<sup>9+</sup> | number | Yes| No| The caret moves upward.|
| CURSOR_DOWN<sup>9+</sup> | number | Yes| No| The caret moves downward.|
| CURSOR_LEFT<sup>9+</sup> | number | Yes| No| The caret moves leftward.|
| CURSOR_RIGHT<sup>9+</sup> | number | Yes| No| The caret moves rightward.|
| WINDOW_TYPE_INPUT_METHOD_FLOAT<sup>9+</sup> | number | Yes| No| The input method is displayed in a floating window.|
E
ester.zhou 已提交
54 55 56 57 58 59 60

## inputMethodEngine.getInputMethodEngine<a name="getInputMethodEngine"></a>

getInputMethodEngine(): InputMethodEngine

Obtains an **InputMethodEngine** instance.

61
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
62

E
ester.zhou 已提交
63
**Return value**
E
ester.zhou 已提交
64

E
ester.zhou 已提交
65 66 67
| Type                                   | Description        |
| --------------------------------------- | ------------ |
| [InputMethodEngine](#InputMethodEngine) | **InputMethodEngine** instance obtained.|
E
ester.zhou 已提交
68

E
ester.zhou 已提交
69
**Example**
E
ester.zhou 已提交
70

71 72 73
  ```js
  var InputMethodEngine = inputMethodEngine.getInputMethodEngine();
  ```
E
ester.zhou 已提交
74 75 76 77 78 79 80

## inputMethodEngine.createKeyboardDelegate<a name="createKeyboardDelegate"></a>

createKeyboardDelegate(): KeyboardDelegate

Obtains a **KeyboardDelegate** instance.

81
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
82

E
ester.zhou 已提交
83
**Return value**
E
ester.zhou 已提交
84

E
ester.zhou 已提交
85 86 87
| Type                                 | Description            |
| ------------------------------------- | ---------------- |
| [KeyboardDelegate](#KeyboardDelegate) | **KeyboardDelegate** instance obtained.|
E
ester.zhou 已提交
88

E
ester.zhou 已提交
89
**Example**
E
ester.zhou 已提交
90

91 92 93
  ```js
  var KeyboardDelegate = inputMethodEngine.createKeyboardDelegate();
  ```
E
ester.zhou 已提交
94 95 96 97 98 99 100 101 102

## InputMethodEngine<a name="InputMethodEngine"></a>

In the following API examples, you must first use [getInputMethodEngine](#getInputMethodEngine) to obtain an **InputMethodEngine** instance, and then call the APIs using the obtained instance.

### on('inputStart')<a name="inputStart"></a>

on(type: 'inputStart', callback: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void

E
ester.zhou 已提交
103
Listens for the input method binding event. This API uses a callback to return the result. This API requires two parameters, the first one being napi_string and the second one being napi_function. If either of these parameters is not passed in, an exception is thrown.
E
ester.zhou 已提交
104

105
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
106

E
ester.zhou 已提交
107
**Parameters**
E
ester.zhou 已提交
108 109 110 111 112 113

| Name  | Type                           | Mandatory| Description                                                        |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                        | Yes  | Listening type.<br>Set it to **'inputStart'**, which indicates listening for the input method binding event.|
| callback | [KeyboardController](#KeyboardController), [TextInputClient](#TextInputClient) | Yes| Callback used to return the result.|

E
ester.zhou 已提交
114
**Example**
E
ester.zhou 已提交
115

116 117 118 119 120 121
  ```js
  InputMethodEngine.on('inputStart', (kbController, textInputClient) => {
      KeyboardController = kbController;
      TextInputClient = textInputClient;
  });
  ```
E
ester.zhou 已提交
122 123 124 125 126

### off('inputStart')

off(type: 'inputStart', callback?: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void

E
ester.zhou 已提交
127
Cancels listening for the input method binding event. An exception is thrown in the following cases: (1) No parameter is passed; (2) Only one parameter is passed in, and it is not napi_string; (2) Two parameters are passed in, and the first parameter is not napi_string or the second parameter is not napi_function. If only one parameter is passed in, all listeners of the specified type will be canceled. If two parameters are passed in, the current listener of the specified type will be canceled.
E
ester.zhou 已提交
128

129
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
130

E
ester.zhou 已提交
131
**Parameters**
E
ester.zhou 已提交
132

E
ester.zhou 已提交
133 134 135 136
| Name  | Type                | Mandatory| Description                    |
| -------- | -------------------- | ---- | ------------------------ |
| type | string                                                       | Yes  | Listening type.<br>Set it to **'inputStart'**, which indicates listening for the input method binding event.|
| callback | [KeyboardController](#KeyboardController), [TextInputClient](#TextInputClient) | No| Callback used to return the result.|
E
ester.zhou 已提交
137

138 139


E
ester.zhou 已提交
140
**Example**
E
ester.zhou 已提交
141

142
  ```js
E
ester.zhou 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  InputMethodEngine.off('inputStart', (kbController, textInputClient) => {
      console.log('delete inputStart notification.');
  });
  ```

### on('inputStop')<sup>9+</sup>

on(type: 'inputStop', callback: () => void): void

Listens for the input method stop event. This API uses a callback to return the result.

**System capability**: SystemCapability.MiscServices.InputMethodFramework

**Parameters**

| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type     | string | Yes  | Listening type.<br>Set it to **'inputStop'**, which indicates listening for the input method stop event.|
| callback | void   | Yes  | Callback used to return the result.                                                  |

**Example**

  ```js
InputMethodEngine.getInputMethodEngine().on('inputStop', () => {
    console.log('inputMethodEngine inputStop');
});
  ```

### off('inputStop')<sup>9+</sup>

off(type: 'inputStop', callback: () => void): void

Cancels listening for the input method stop event. This API uses a callback to return the result.

**System capability**: SystemCapability.MiscServices.InputMethodFramework

**Parameters**

| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type     | string | Yes  | Listening type.<br>Set it to **'inputStop'**, which indicates listening for the input method stop event.|
| callback | void   | Yes  | Callback used to return the result.                                                  |

**Example**

  ```js
InputMethodEngine.getInputMethodEngine().off('inputStop', () => {
    console.log('inputMethodEngine delete inputStop notification.');
});
  ```

### on('setCallingWindow')<sup>9+</sup>

on(type: 'setCallingWindow', callback: (wid:number) => void): void

Listens for the window invocation setting event. This API uses a callback to return the result.

**System capability**: SystemCapability.MiscServices.InputMethodFramework

**Parameters**

| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type     | string | Yes  | Listening type.<br>Set it to **'setCallingWindow'**, which indicates listening for the window invocation setting event.|
| callback | number | Yes  | Window ID of the caller.                                           |

**Example**

  ```js
InputMethodEngine.getInputMethodEngine().on('setCallingWindow', (wid) => {
    console.log('inputMethodEngine setCallingWindow');
});
  ```

### off('setCallingWindow')<sup>9+</sup>

off(type: 'setCallingWindow', callback: (wid:number) => void): void

Cancels listening for the window invocation setting event. This API uses a callback to return the result.

**System capability**: SystemCapability.MiscServices.InputMethodFramework

**Parameters**

| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type     | string | Yes  | Listening type.<br>Set it to **'setCallingWindow'**, which indicates listening for the window invocation setting event.|
| callback | number | Yes  | Window ID of the caller.                                |

**Example**

  ```js
InputMethodEngine.getInputMethodEngine().off('setCallingWindow', () => {
    console.log('inputMethodEngine delete setCallingWindow notification.');
});
238
  ```
E
ester.zhou 已提交
239 240 241 242 243

### on('keyboardShow'|'keyboardHide')

on(type: 'keyboardShow'|'keyboardHide', callback: () => void): void

E
ester.zhou 已提交
244
Listens for an input method event. This API requires two parameters, the first one being napi_string and the second one being napi_function. If either of these parameters is not passed in, an exception is thrown.
E
ester.zhou 已提交
245

246
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
247

E
ester.zhou 已提交
248
**Parameters**
E
ester.zhou 已提交
249

E
ester.zhou 已提交
250 251
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
252
| type     | string | Yes  | Listening type.<br>- The value **'keyboardShow'** means to listen for displaying of the keyboard.<br>- The value **'keyboardHide'** means to listen for hiding of the keyboard. |
E
ester.zhou 已提交
253
| callback | void   | No  | Callback used to return the result.                                                  |
E
ester.zhou 已提交
254

E
ester.zhou 已提交
255
**Example**
E
ester.zhou 已提交
256

257
  ```js
E
ester.zhou 已提交
258 259 260 261 262
  InputMethodEngine.on('keyboardShow', () => {
      console.log('inputMethodEngine keyboardShow.');
  });
  InputMethodEngine.on('keyboardHide', () => {
      console.log('inputMethodEngine keyboardHide.');
263 264
  });
  ```
E
ester.zhou 已提交
265 266 267 268 269

### off('keyboardShow'|'keyboardHide')

off(type: 'keyboardShow'|'keyboardHide', callback?: () => void): void

E
ester.zhou 已提交
270
Cancels listening for an input method event. An exception is thrown in the following cases: (1) No parameter is passed; (2) Only one parameter is passed in, and it is not napi_string; (2) Two parameters are passed in, and the first parameter is not napi_string or the second parameter is not napi_function. If only one parameter is passed in, all listeners of the specified type will be canceled. If two parameters are passed in, the current listener of the specified type will be canceled.
E
ester.zhou 已提交
271

272
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
273

E
ester.zhou 已提交
274
**Parameters**
E
ester.zhou 已提交
275

E
ester.zhou 已提交
276 277
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
278
| type     | string | Yes  | Listening type.<br>- The value **'keyboardShow'** means to listen for displaying of the keyboard.<br>- The value **'keyboardHide'** means to listen for hiding of the keyboard.|
E
ester.zhou 已提交
279
| callback | void   | No  | Callback used to return the result.                                                  |
E
ester.zhou 已提交
280

E
ester.zhou 已提交
281
**Example**
E
ester.zhou 已提交
282

283
  ```js
E
ester.zhou 已提交
284 285 286 287 288 289
  InputMethodEngine.off('keyboardShow', () => {
      console.log('inputMethodEngine delete keyboardShow notification.');
  });
  InputMethodEngine.off('keyboardHide', () => {
      console.log('inputMethodEngine delete keyboardHide notification.');
  });
290 291
  ```

E
ester.zhou 已提交
292 293 294 295 296 297 298 299 300

## KeyboardDelegate<a name="KeyboardDelegate"></a>

In the following API examples, you must first use [createKeyboardDelegate](#createKeyboardDelegate) 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

E
ester.zhou 已提交
301
Listens for a hard keyboard even. This API uses a callback to return the key information. This API requires two parameters, the first one being napi_string and the second one being napi_function. If either of these parameters is not passed in, an exception is thrown.
E
ester.zhou 已提交
302

303
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
304

E
ester.zhou 已提交
305
**Parameters**
E
ester.zhou 已提交
306

E
ester.zhou 已提交
307 308
| Name  | Type                           | Mandatory| Description                                                        |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
309
| type   | string         | Yes  | Listening type.<br>- The value **'keyDown'** means to listen for pressing of a key.<br>- The value **'keyUp'** means to listen for releasing of a key.|
E
ester.zhou 已提交
310
| callback | [KeyEvent](#KeyEvent) | Yes| Callback used to return the key information.|
E
ester.zhou 已提交
311

312 313


E
ester.zhou 已提交
314
**Example**
E
ester.zhou 已提交
315

316
  ```js
E
ester.zhou 已提交
317 318 319 320 321 322 323 324 325
  KeyboardDelegate.on('keyUp', (keyEvent) => {
      console.info('inputMethodEngine keyCode.(keyUp):' + JSON.stringify(keyEvent.keyCode));
      console.info('inputMethodEngine keyAction.(keyUp):' + JSON.stringify(keyEvent.keyAction));
      return true;
  });
  KeyboardDelegate.on('keyDown', (keyEvent) => {
      console.info('inputMethodEngine keyCode.(keyDown):' + JSON.stringify(keyEvent.keyCode));
      console.info('inputMethodEngine keyAction.(keyDown):' + JSON.stringify(keyEvent.keyAction));
      return true;
326 327
  });
  ```
E
ester.zhou 已提交
328 329 330 331 332

### off('keyDown'|'keyUp')

off(type: 'keyDown'|'keyUp', callback?: (event: KeyEvent) => boolean): void

E
ester.zhou 已提交
333
Cancels listening for a hard keyboard even. An exception is thrown in the following cases: (1) No parameter is passed; (2) Only one parameter is passed in, and it is not napi_string; (2) Two parameters are passed in, and the first parameter is not napi_string or the second parameter is not napi_function. If only one parameter is passed in, all listeners of the specified type will be canceled. If two parameters are passed in, the current listener of the specified type will be canceled.
E
ester.zhou 已提交
334

335
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
336

E
ester.zhou 已提交
337
**Parameters**
E
ester.zhou 已提交
338

E
ester.zhou 已提交
339 340
| Name  | Type                 | Mandatory| Description                                                        |
| -------- | --------------------- | ---- | ------------------------------------------------------------ |
E
ester.zhou 已提交
341
| type     | string                | Yes  | Listening type.<br>- The value **'keyDown'** means to listen for pressing of a key.<br>- The value **'keyUp'** means to listen for releasing of a key.|
E
ester.zhou 已提交
342
| callback | [KeyEvent](#KeyEvent) | No  | Callback used to return the key information.                                          |
E
ester.zhou 已提交
343

E
ester.zhou 已提交
344
**Example**
E
ester.zhou 已提交
345

346
  ```js
E
ester.zhou 已提交
347 348 349 350 351 352 353 354
  KeyboardDelegate.off('keyUp', (keyEvent) => {
      console.log('delete keyUp notification.');
      return true;
  });
  KeyboardDelegate.off('keyDown', (keyEvent) => {
      console.log('delete keyDown notification.');
      return true;
  });
355
  ```
E
ester.zhou 已提交
356 357 358 359 360

### on('cursorContextChange')

on(type: 'cursorContextChange', callback: (x: number, y:number, height:number) => void): void

E
ester.zhou 已提交
361
Listens for cursor context changes. This API uses a callback to return the cursor information. This API requires two parameters, the first one being napi_string and the second one being napi_function. If either of these parameters is not passed in, an exception is thrown.
E
ester.zhou 已提交
362

363
  **System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
364

365
  **Parameters**
E
ester.zhou 已提交
366

E
ester.zhou 已提交
367 368 369 370
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type     | string | Yes  | Listening type.<br>Set it to **'cursorContextChange'**, which indicates listening for cursor context changes.|
| callback | number | Yes  | Callback used to return the cursor information.                                          |
E
ester.zhou 已提交
371

372 373 374


  **Example**
E
ester.zhou 已提交
375

E
ester.zhou 已提交
376 377
```js
KeyboardDelegate.on('cursorContextChange', (x, y, height) => {
E
ester.zhou 已提交
378 379 380
    console.log('inputMethodEngine cursorContextChange x:' + x);
    console.log('inputMethodEngine cursorContextChange y:' + y);
    console.log('inputMethodEngine cursorContextChange height:' + height);
E
ester.zhou 已提交
381 382
});
```
E
ester.zhou 已提交
383 384 385 386 387

### off('cursorContextChange')

off(type: 'cursorContextChange', callback?: (x: number, y:number, height:number) => void): void

E
ester.zhou 已提交
388
Cancels listening for cursor context changes. An exception is thrown in the following cases: (1) No parameter is passed; (2) Only one parameter is passed in, and it is not napi_string; (2) Two parameters are passed in, and the first parameter is not napi_string or the second parameter is not napi_function. If only one parameter is passed in, all listeners of the specified type will be canceled. If two parameters are passed in, the current listener of the specified type will be canceled.
E
ester.zhou 已提交
389

390
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
391

392
  **Parameters**
E
ester.zhou 已提交
393

394 395 396 397
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type     | string | Yes  | Listening type.<br>Set it to **'cursorContextChange'**, which indicates listening for cursor context changes.|
| callback | number | No  | Callback used to return the cursor information.                                          |
E
ester.zhou 已提交
398

399 400

  **Example**
E
ester.zhou 已提交
401

E
ester.zhou 已提交
402
```js
E
ester.zhou 已提交
403 404 405
KeyboardDelegate.off('cursorContextChange', (x, y, height) => {
    console.log('delete cursorContextChange notification.');
});
406
```
E
ester.zhou 已提交
407 408 409 410
### on('selectionChange')

on(type: 'selectionChange', callback: (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void): void

E
ester.zhou 已提交
411
Listens for text selection changes. This API uses a callback to return the text selection information. This API requires two parameters, the first one being napi_string and the second one being napi_function. If either of these parameters is not passed in, an exception is thrown.
E
ester.zhou 已提交
412

413
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
414

415
  **Parameters**
E
ester.zhou 已提交
416 417 418 419 420

| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type     | string | Yes  | Listening type.<br>Set it to **'selectionChange'**, which indicates listening for text selection changes.|
| callback | number | Yes  | Callback used to return the text selection information.                                      |
E
ester.zhou 已提交
421

422
  **Example**
E
ester.zhou 已提交
423

E
ester.zhou 已提交
424 425
```js
KeyboardDelegate.on('selectionChange', (oldBegin, oldEnd, newBegin, newEnd) => {
E
ester.zhou 已提交
426 427 428 429
    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);
E
ester.zhou 已提交
430 431
});
```
E
ester.zhou 已提交
432 433 434 435 436

### off('selectionChange')

off(type: 'selectionChange', callback?: (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void): void

E
ester.zhou 已提交
437
Cancels listening for text selection changes. An exception is thrown in the following cases: (1) No parameter is passed; (2) Only one parameter is passed in, and it is not napi_string; (2) Two parameters are passed in, and the first parameter is not napi_string or the second parameter is not napi_function. If only one parameter is passed in, all listeners of the specified type will be canceled. If two parameters are passed in, the current listener of the specified type will be canceled.
E
ester.zhou 已提交
438

439
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
440

441
  **Parameters**
E
ester.zhou 已提交
442

443 444 445 446
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type     | string | Yes  | Listening type.<br>Set it to **'selectionChange'**, which indicates listening for text selection changes.|
| callback | number | No  | Callback used to return the text selection information.                                      |
E
ester.zhou 已提交
447

448
  **Example**
E
ester.zhou 已提交
449

E
ester.zhou 已提交
450
```js
E
ester.zhou 已提交
451 452 453
KeyboardDelegate.off('selectionChange', (oldBegin, oldEnd, newBegin, newEnd) => {
  console.log('delete selectionChange notification.');
});
E
ester.zhou 已提交
454
```
E
ester.zhou 已提交
455 456 457 458 459 460


### on('textChange')

on(type: 'textChange', callback: (text: string) => void): void

E
ester.zhou 已提交
461
Listens for text changes. This API uses a callback to return the current text content. This API requires two parameters, the first one being napi_string and the second one being napi_function. If either of these parameters is not passed in, an exception is thrown.
E
ester.zhou 已提交
462

463
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
464

465
  **Parameters**
E
ester.zhou 已提交
466

467 468 469 470
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type     | string | Yes  | Listening type.<br>Set it to **'textChange'**, which indicates listening for text changes.|
| callback | string | Yes  | Callback used to return the current text content.                                      |
E
ester.zhou 已提交
471

472
  **Example**
E
ester.zhou 已提交
473

E
ester.zhou 已提交
474 475
```js
KeyboardDelegate.on('textChange', (text) => {
E
ester.zhou 已提交
476
    console.log('inputMethodEngine textChange. text:' + text);
E
ester.zhou 已提交
477 478
});
```
E
ester.zhou 已提交
479 480 481 482 483

### off('textChange')

off(type: 'textChange', callback?: (text: string) => void): void

E
ester.zhou 已提交
484
Cancels listening for text changes. An exception is thrown in the following cases: (1) No parameter is passed; (2) Only one parameter is passed in, and it is not napi_string; (2) Two parameters are passed in, and the first parameter is not napi_string or the second parameter is not napi_function. If only one parameter is passed in, all listeners of the specified type will be canceled. If two parameters are passed in, the current listener of the specified type will be canceled.
E
ester.zhou 已提交
485

486
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
487

488
  **Parameters**
E
ester.zhou 已提交
489

490 491 492 493
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| type     | string | Yes  | Listening type.<br>Set it to **'textChange'**, which indicates listening for text changes.|
| callback | string | No  | Callback used to return the current text content.                                      |
E
ester.zhou 已提交
494

495
  **Example**
E
ester.zhou 已提交
496

E
ester.zhou 已提交
497
```js
E
ester.zhou 已提交
498 499 500
keyboardDelegate.off('textChange', (text) => {
    console.log('delete textChange notification. text:' + text);
});
E
ester.zhou 已提交
501
```
E
ester.zhou 已提交
502 503 504 505 506 507 508 509 510

## KeyboardController<a name="KeyboardController"></a>

In the following API examples, you must first use [inputStart](#inputStart) to obtain a **KeyboardController** instance, and then call the APIs using the obtained instance.

### hideKeyboard

hideKeyboard(callback: AsyncCallback&lt;void&gt;): void

E
ester.zhou 已提交
511
Hides the keyboard. This API uses an asynchronous callback to return the result. If the required parameter is not passed in, an exception is thrown.
E
ester.zhou 已提交
512

513
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
514

E
ester.zhou 已提交
515
**Parameters**
E
ester.zhou 已提交
516

E
ester.zhou 已提交
517 518 519
| Name  | Type                  | Mandatory| Description    |
| -------- | ---------------------- | ---- | -------- |
| callback | AsyncCallback&lt;void> | No  | Callback used to return the result.|
E
ester.zhou 已提交
520

E
ester.zhou 已提交
521
**Example**
E
ester.zhou 已提交
522 523


E
ester.zhou 已提交
524
```js
E
ester.zhou 已提交
525 526 527 528 529 530 531
KeyboardController.hideKeyboard((err) => {
    if (err === undefined) {
        console.error('hideKeyboard callback result---err: ' + err.msg);
        return;
    }
    console.log('hideKeyboard callback.');
});
E
ester.zhou 已提交
532 533 534 535 536 537
```

### hideKeyboard

hideKeyboard(): Promise&lt;void&gt;

E
ester.zhou 已提交
538
Hides the keyboard. This API uses a promise to return the result. If any parameter is passed in, an exception is thrown.
E
ester.zhou 已提交
539

540
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
541

E
ester.zhou 已提交
542
**Return value**
E
ester.zhou 已提交
543

E
ester.zhou 已提交
544 545 546
| Type            | Description                     |
| ---------------- | ------------------------- |
| Promise&lt;void> | Promise that returns no value.|
E
ester.zhou 已提交
547

E
ester.zhou 已提交
548
**Example**
E
ester.zhou 已提交
549

E
ester.zhou 已提交
550
```js
E
ester.zhou 已提交
551 552 553 554 555 556 557
async function InputMethodEngine() {
    await KeyboardController.hideKeyboard().then(() => {
        console.info('hideKeyboard promise.');
    }).catch((err) => {
        console.info('hideKeyboard promise err: ' + err.msg);
    });
}
E
ester.zhou 已提交
558 559 560 561 562 563 564 565 566 567
```

## TextInputClient<a name="TextInputClient"></a>

In the following API examples, you must first use [inputStart](#inputStart) to obtain a **TextInputClient** instance, and then call the APIs using the obtained instance.

### getForward

getForward(length:number, callback: AsyncCallback&lt;string&gt;): void

E
ester.zhou 已提交
568
Obtains the specific-length text before the cursor. This API uses an asynchronous callback to return the result. If the required two parameters are not passed in, an exception is thrown.
E
ester.zhou 已提交
569

570
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
571

E
ester.zhou 已提交
572 573 574 575 576
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
577
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the text.|
E
ester.zhou 已提交
578

E
ester.zhou 已提交
579
**Example**
E
ester.zhou 已提交
580

581
  ```js
E
ester.zhou 已提交
582 583 584 585 586 587 588 589
  var length = 1;
  TextInputClient.getForward(length, (err, text) => {
      if (err === undefined) {
          console.error('getForward callback result---err: ' + err.msg);
          return;
      }
      console.log('getForward callback result---text: ' + text);
  });
590
  ```
E
ester.zhou 已提交
591 592 593 594 595

### getForward

getForward(length:number): Promise&lt;string&gt;

E
ester.zhou 已提交
596
Obtains the specific-length text before the cursor. This API uses a promise to return the result. If the required parameter is not passed in, an exception is thrown.
E
ester.zhou 已提交
597

598
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
599

E
ester.zhou 已提交
600
**Parameters**
E
ester.zhou 已提交
601

E
ester.zhou 已提交
602 603 604
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
E
ester.zhou 已提交
605

E
ester.zhou 已提交
606
**Return value**
E
ester.zhou 已提交
607

E
ester.zhou 已提交
608 609
| Type                           | Description                                                        |
| ------------------------------- | ------------------------------------------------------------ |
610
| Promise&lt;string&gt; |  Promise used to return the text.               |
E
ester.zhou 已提交
611 612 613

**Example**

614
  ```js
E
ester.zhou 已提交
615 616 617 618 619 620 621 622
  async function InputMethodEngine() {
      var length = 1;
      await TextInputClient.getForward(length).then((text) => {
          console.info('getForward promise result---res: ' + text);
      }).catch((err) => {
          console.error('getForward promise err: ' + err.msg);
      });
  }
623
  ```
E
ester.zhou 已提交
624 625 626 627 628

### getBackward

getBackward(length:number, callback: AsyncCallback&lt;string&gt;): void

E
ester.zhou 已提交
629
Obtains the specific-length text after the cursor. This API uses an asynchronous callback to return the result. If the required two parameters are not passed in, an exception is thrown.
E
ester.zhou 已提交
630

631
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
632

E
ester.zhou 已提交
633
**Parameters**
E
ester.zhou 已提交
634

E
ester.zhou 已提交
635 636 637
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
638
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the text.|
E
ester.zhou 已提交
639

E
ester.zhou 已提交
640 641
**Example**

642
  ```js
E
ester.zhou 已提交
643 644 645 646 647 648 649
  var length = 1;
  TextInputClient.getBackward(length, (err, text) => {
      if (err === undefined) {
          console.error('getBackward callback result---err: ' + err.msg);
          return;
      }
      console.log('getBackward callback result---text: ' + text);
650 651
  });
  ```
E
ester.zhou 已提交
652 653 654 655 656

### getBackward

getBackward(length:number): Promise&lt;string&gt;

E
ester.zhou 已提交
657
Obtains the specific-length text after the cursor. This API uses a promise to return the result. If the required parameter is not passed in, an exception is thrown.
E
ester.zhou 已提交
658

659
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
660

E
ester.zhou 已提交
661
**Parameters**
E
ester.zhou 已提交
662

E
ester.zhou 已提交
663 664 665
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
E
ester.zhou 已提交
666

E
ester.zhou 已提交
667
**Return value**
E
ester.zhou 已提交
668

E
ester.zhou 已提交
669 670
| Type                           | Description                                                        |
| ------------------------------- | ------------------------------------------------------------ |
671
| Promise&lt;string&gt; |  Promise used to return the text.               |
E
ester.zhou 已提交
672 673 674

**Example**

675
  ```js
E
ester.zhou 已提交
676 677 678 679 680 681 682 683
  async function InputMethodEngine() {
      var length = 1;
      await TextInputClient.getBackward(length).then((text) => {
          console.info('getBackward promise result---res: ' + text);
      }).catch((err) => {
          console.error('getBackward promise err: ' + err.msg);
      });
  }
684
  ```
E
ester.zhou 已提交
685 686 687 688 689

### deleteForward

deleteForward(length:number, callback: AsyncCallback&lt;boolean&gt;): void

E
ester.zhou 已提交
690
Deletes the fixed-length text before the cursor. This API uses an asynchronous callback to return the result. If the required two parameters are not passed in, an exception is thrown.
E
ester.zhou 已提交
691

692
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
693

E
ester.zhou 已提交
694
**Parameters**
E
ester.zhou 已提交
695

E
ester.zhou 已提交
696 697 698
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
699
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the result.|
E
ester.zhou 已提交
700 701 702

**Example**

703
  ```js
E
ester.zhou 已提交
704 705 706 707 708 709 710 711 712 713 714
  var length = 1;
  TextInputClient.deleteForward(length, (err, result) => {
      if (err === undefined) {
          console.error('deleteForward callback result---err: ' + err.msg);
          return;
      }
      if (result) {
          console.info('Success to deleteForward.(callback) ');
      } else {
          console.error('Failed to deleteForward.(callback) ');
      }
715 716
  });
  ```
E
ester.zhou 已提交
717 718 719 720
### deleteForward

deleteForward(length:number): Promise&lt;boolean&gt;

E
ester.zhou 已提交
721
Deletes the fixed-length text before the cursor. This API uses a promise to return the result. If the required parameter is not passed in, an exception is thrown.
E
ester.zhou 已提交
722

723
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
724

E
ester.zhou 已提交
725 726
**Parameters**

727 728 729
| Name| Type  | Mandatory| Description      |
| ------ | ------ | ---- | ---------- |
| length | number | Yes  | Text length.|
E
ester.zhou 已提交
730 731 732

**Return value** 

733 734 735
| Type                  | Description          |
| ---------------------- | -------------- |
| Promise&lt;boolean&gt; | Promise used to return the result.|
E
ester.zhou 已提交
736 737 738 739

**Example**

```js
E
ester.zhou 已提交
740 741 742 743 744 745 746 747 748 749 750 751
async function InputMethodEngine() {
    var length = 1;
    await TextInputClient.deleteForward(length).then((result) => {
        if (result) {
            console.info('Success to deleteForward.(promise) ');
        } else {
            console.error('Failed to deleteForward.(promise) ');
        }
    }).catch((err) => {
        console.error('deleteForward promise err: ' + err.msg);
    });
}
E
ester.zhou 已提交
752 753
```

E
ester.zhou 已提交
754 755 756 757
### deleteBackward

deleteBackward(length:number, callback: AsyncCallback&lt;boolean&gt;): void

E
ester.zhou 已提交
758
Deletes the fixed-length text after the cursor. This API uses an asynchronous callback to return the result. If the required two parameters are not passed in, an exception is thrown.
E
ester.zhou 已提交
759

760
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
761

762
  **Parameters**
E
ester.zhou 已提交
763

764 765 766 767
| Name  | Type                        | Mandatory| Description          |
| -------- | ---------------------------- | ---- | -------------- |
| length   | number                       | Yes  | Text length.    |
| callback | AsyncCallback&lt;boolean&gt; | Yes  | Callback used to return the result.|
E
ester.zhou 已提交
768

769
  **Example**
E
ester.zhou 已提交
770 771

```js
E
ester.zhou 已提交
772 773 774 775 776 777 778 779 780 781 782
var length = 1;
TextInputClient.deleteBackward(length, (err, result) => {
    if (err === undefined) {
        console.error('deleteBackward callback result---err: ' + err.msg);
        return;
    }
    if (result) {
        console.info('Success to deleteBackward.(callback) ');
    } else {
        console.error('Failed to deleteBackward.(callback) ');
    }
E
ester.zhou 已提交
783 784
});
```
E
ester.zhou 已提交
785 786 787 788 789

### deleteBackward

deleteBackward(length:number): Promise&lt;boolean&gt;

E
ester.zhou 已提交
790
Deletes the fixed-length text after the cursor. This API uses an asynchronous callback to return the result. If the required two parameters are not passed in, an exception is thrown.
E
ester.zhou 已提交
791

792
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
793

E
ester.zhou 已提交
794
**Parameters**
E
ester.zhou 已提交
795

E
ester.zhou 已提交
796 797 798
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| length | number | Yes| Text length.|
E
ester.zhou 已提交
799

E
ester.zhou 已提交
800
**Return value**
E
ester.zhou 已提交
801

E
ester.zhou 已提交
802 803
| Type                           | Description                                                        |
| ------------------------------- | ------------------------------------------------------------ |
804
| Promise&lt;boolean&gt; |  Promise used to return the result.               |
E
ester.zhou 已提交
805 806 807 808

**Example**

```js
E
ester.zhou 已提交
809 810 811 812 813 814 815 816 817 818 819 820
async function InputMethodEngine() {
    var length = 1;
    await TextInputClient.deleteBackward(length).then((result) => {
        if (result) {
            console.info('Success to deleteBackward.(promise) ');
        } else {
            console.error('Failed to deleteBackward.(promise) ');
        }
    }).catch((err) => {
        console.error('deleteBackward promise err: ' + err.msg);
    });
}
821
```
E
ester.zhou 已提交
822 823 824 825
### sendKeyFunction

sendKeyFunction(action:number, callback: AsyncCallback&lt;boolean&gt;): void

E
ester.zhou 已提交
826
Sets the Enter key to send the text to its target. This API uses an asynchronous callback to return the result. If the required two parameters are not passed in, an exception is thrown.
E
ester.zhou 已提交
827

828
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
829

830
  **Parameters**
E
ester.zhou 已提交
831

E
ester.zhou 已提交
832 833 834
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| action | number | Yes| Edit box attribute.|
835
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the result.|
E
ester.zhou 已提交
836

837
  **Example**
E
ester.zhou 已提交
838 839

```js
E
ester.zhou 已提交
840 841 842 843 844 845 846 847 848 849
TextInputClient.sendKeyFunction(keyFunction, (err, result) => {
    if (err === undefined) {
        console.error('sendKeyFunction callback result---err: ' + err.msg);
        return;
    }
    if (result) {
        console.info('Success to sendKeyFunction.(callback) ');
    } else {
        console.error('Failed to sendKeyFunction.(callback) ');
    }
E
ester.zhou 已提交
850 851
});
```
E
ester.zhou 已提交
852 853 854 855 856

### sendKeyFunction

sendKeyFunction(action:number): Promise&lt;boolean&gt;

E
ester.zhou 已提交
857
Sets the Enter key to send the text to its target. This API uses a promise to return the result. If the required parameter is not passed in, an exception is thrown.
E
ester.zhou 已提交
858

859
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
860

E
ester.zhou 已提交
861
**Parameters**
E
ester.zhou 已提交
862

E
ester.zhou 已提交
863 864 865
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| action | number | Yes| Edit box attribute.|
E
ester.zhou 已提交
866

E
ester.zhou 已提交
867 868 869 870
**Return value**

| Type                           | Description                                                        |
| ------------------------------- | ------------------------------------------------------------ |
871
| Promise&lt;boolean&gt; |  Promise used to return the result.               |
E
ester.zhou 已提交
872 873 874

**Example**

875
  ```js
E
ester.zhou 已提交
876 877 878 879 880 881 882 883 884 885 886
  async function InputMethodEngine() {
      await client.sendKeyFunction(keyFunction).then((result) => {
          if (result) {
              console.info('Success to sendKeyFunction.(promise) ');
          } else {
              console.error('Failed to sendKeyFunction.(promise) ');
          }
      }).catch((err) => {
          console.error('sendKeyFunction promise err:' + err.msg);
      });
  }
887
  ```
E
ester.zhou 已提交
888 889 890 891 892

### insertText

insertText(text:string, callback: AsyncCallback&lt;boolean&gt;): void

E
ester.zhou 已提交
893
Inserts text. This API uses an asynchronous callback to return the result. If the required two parameters are not passed in, an exception is thrown.
E
ester.zhou 已提交
894

895
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
896

E
ester.zhou 已提交
897
**Parameters**
E
ester.zhou 已提交
898

E
ester.zhou 已提交
899 900 901
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| Text to insert.|
902
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the result.|
E
ester.zhou 已提交
903

E
ester.zhou 已提交
904 905 906
**Example**

```js
E
ester.zhou 已提交
907 908 909 910 911 912 913 914 915 916
TextInputClient.insertText('test', (err, result) => {
    if (err === undefined) {
        console.error('insertText callback result---err: ' + err.msg);
        return;
    }
    if (result) {
        console.info('Success to insertText.(callback) ');
    } else {
        console.error('Failed to insertText.(callback) ');
    }
E
ester.zhou 已提交
917 918
});
```
E
ester.zhou 已提交
919 920 921 922 923

### insertText

insertText(text:string): Promise&lt;boolean&gt;

E
ester.zhou 已提交
924
Inserts text. This API uses a promise to return the result. If the required parameter is not passed in, an exception is thrown.
E
ester.zhou 已提交
925

926
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
927

E
ester.zhou 已提交
928
**Parameters**
E
ester.zhou 已提交
929

E
ester.zhou 已提交
930 931 932
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| text | string | Yes| Text to insert.|
E
ester.zhou 已提交
933

934
**Return value** 
E
ester.zhou 已提交
935 936 937

| Type                           | Description                                                        |
| ------------------------------- | ------------------------------------------------------------ |
938
| Promise&lt;boolean&gt; |  Promise used to return the result.               |
E
ester.zhou 已提交
939 940 941

**Example**

942
  ```js
E
ester.zhou 已提交
943 944 945 946 947 948 949 950 951 952 953
  async function InputMethodEngine() {
      await TextInputClient.insertText('test').then((result) => {
          if (result) {
              console.info('Success to insertText.(promise) ');
          } else {
              console.error('Failed to insertText.(promise) ');
          }
      }).catch((err) => {
          console.error('insertText promise err: ' + err.msg);
      });
  }
954
  ```
E
ester.zhou 已提交
955 956 957 958 959

### getEditorAttribute

getEditorAttribute(callback: AsyncCallback&lt;EditorAttribute&gt;): void

E
ester.zhou 已提交
960
Obtains the attribute of the edit box. This API uses an asynchronous callback to return the result. If the required parameter is not passed in, an exception is thrown.
E
ester.zhou 已提交
961

962
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
963

E
ester.zhou 已提交
964 965 966 967 968 969 970
**Parameters**

| Name                        | Type                         | Mandatory                           | Description                                                        |
| ------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
| callback | AsyncCallback&lt;[EditorAttribute](#EditorAttribute)&gt; | Yes|  Attribute of the edit box.               |

**Example**
E
ester.zhou 已提交
971

972
  ```js
E
ester.zhou 已提交
973 974 975 976 977 978 979 980
  TextInputClient.getEditorAttribute((err, editorAttribute) => {
      if (err === undefined) {
          console.error('getEditorAttribute callback result---err: ' + err.msg);
          return;
      }
      console.log('editorAttribute.inputPattern(callback): ' + JSON.stringify(editorAttribute.inputPattern));
      console.log('editorAttribute.enterKeyType(callback): ' + JSON.stringify(editorAttribute.enterKeyType));
  });
981
  ```
E
ester.zhou 已提交
982 983 984

### getEditorAttribute

E
ester.zhou 已提交
985
getEditorAttribute(): Promise&lt;EditorAttribute&gt;
E
ester.zhou 已提交
986

E
ester.zhou 已提交
987
Obtains the attribute of the edit box. This API uses a promise to return the result. If any parameter is passed in, an exception is thrown.
E
ester.zhou 已提交
988

989
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
990

E
ester.zhou 已提交
991 992 993 994
**Return value**

| Type                           | Description                                                        |
| ------------------------------- | ------------------------------------------------------------ |
E
ester.zhou 已提交
995
| Promise&lt;[EditorAttribute](#EditorAttribute)&gt; |  Promise used to return the attribute of the edit box.          |
E
ester.zhou 已提交
996

E
ester.zhou 已提交
997 998
**Example**

999
   ```js
E
ester.zhou 已提交
1000 1001 1002 1003 1004 1005 1006 1007
   async function InputMethodEngine() {
       await TextInputClient.getEditorAttribute().then((editorAttribute) => {
           console.info('editorAttribute.inputPattern(promise): ' + JSON.stringify(editorAttribute.inputPattern));
           console.info('editorAttribute.enterKeyType(promise): ' + JSON.stringify(editorAttribute.enterKeyType));
       }).catch((err) => {
           console.error('getEditorAttribute promise err: ' + err.msg);
       });
   }
1008
   ```
E
ester.zhou 已提交
1009

E
ester.zhou 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
### moveCursor<sup>9+</sup>

moveCursor(direction: number, callback: AsyncCallback&lt;void&gt;): void

Moves the cursor. This API uses an asynchronous callback to return the result. If the required parameter is not passed in, an exception is thrown.

**System capability**: SystemCapability.MiscServices.InputMethodFramework

**Parameters**

| Name   | Type                     | Mandatory| Description          |
| --------- | ------------------------- | ---- | -------------- |
| direction | number                    | Yes  | Direction in which the cursor moves.|
| callback  | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.    |

**Example**

```js
TextInputClient.moveCursor(inputMethodEngine.CURSOR_xxx, (err) => {
    if (err === undefined) {
        console.error('moveCursor callback result---err: ' + err.msg);
        return;
    }
});
```

### moveCursor<sup>9+</sup>

moveCursor(direction: number): Promise&lt;void&gt;

Moves the cursor. This API uses a promise to return the result. If the required parameter is not passed in, an exception is thrown.

**System capability**: SystemCapability.MiscServices.InputMethodFramework

**Parameters**

| Name   | Type  | Mandatory| Description          |
| --------- | ------ | ---- | -------------- |
| direction | number | Yes  | Direction in which the cursor moves.|

**Return value** 

| Type               | Description                     |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|

**Example**

  ```js
async function InputMethodEngine() {
    await TextInputClient.moveCursor(inputMethodEngine.CURSOR_xxx).then(async (err) => {
        console.log('moveCursor success');
    }).catch((err) => {
        console.error('moveCursor success err: ' + err.msg);
    });
}
  ```

E
ester.zhou 已提交
1068 1069 1070 1071
## EditorAttribute<a name="EditorAttribute"></a>

Describes the attribute of the edit box.

1072
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

| 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<a name="KeyEvent"></a>

Describes the attribute of a key.

1083
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
1084 1085 1086 1087 1088

| Name     | Type| Readable| Writable| Description        |
| --------- | -------- | ---- | ---- | ------------ |
| keyCode   | number   | Yes  | No  | Key value.|
| keyAction | number   | Yes  | No  | Key status.|