js-apis-inputmethod.md 16.4 KB
Newer Older
E
ester.zhou 已提交
1 2
# Input Method Framework

3 4 5
The **inputMethod** module provides an input method framework, which can be used to hide the keyboard, obtain the list of installed input methods, display the dialog box for input method selection, and more.

>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
E
ester.zhou 已提交
8 9 10 11 12


## Modules to Import

```
13
import inputMethod from '@ohos.inputmethod';
E
ester.zhou 已提交
14 15 16 17 18 19
```

## inputMethod<sup>8+</sup>

Provides the constants.

20
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
21 22 23 24 25 26

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| MAX_TYPE_NUM | number | Yes| No| Maximum number of supported input methods.|


E
ester.zhou 已提交
27
## InputMethodProperty<sup>8+</sup>
E
ester.zhou 已提交
28 29 30

Describes the input method application attributes.

31
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
32 33 34 35 36 37

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| packageName | string | Yes| No| Package name.|
| methodId | string | Yes| No| Ability name.|

E
ester.zhou 已提交
38
## inputMethod.getInputMethodController
E
ester.zhou 已提交
39 40 41

getInputMethodController(): InputMethodController

42
Obtains an **[InputMethodController](#inputmethodcontroller)** instance.
E
ester.zhou 已提交
43

44
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
45

E
ester.zhou 已提交
46
**Return value**
E
ester.zhou 已提交
47

E
ester.zhou 已提交
48 49
| Type                                           | Description                    |
| ----------------------------------------------- | ------------------------ |
50
| [InputMethodController](#inputmethodcontroller) | Returns the current **InputMethodController** instance.|
E
ester.zhou 已提交
51 52 53

**Example**

E
ester.zhou 已提交
54
```js
E
ester.zhou 已提交
55
  var InputMethodController = inputMethod.getInputMethodController();
E
ester.zhou 已提交
56
```
E
ester.zhou 已提交
57

E
ester.zhou 已提交
58
## inputMethod.getInputMethodSetting<sup>8+</sup>
E
ester.zhou 已提交
59 60 61

getInputMethodSetting(): InputMethodSetting

62
Obtains an **[InputMethodSetting](#inputmethodsetting8)** instance.
E
ester.zhou 已提交
63

64
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
65

E
ester.zhou 已提交
66
**Return value**
E
ester.zhou 已提交
67

E
ester.zhou 已提交
68 69
| Type                                     | Description                        |
| ----------------------------------------- | ---------------------------- |
70 71
| [InputMethodSetting](#inputmethodsetting8) | Returns the current **InputMethodSetting** instance.|

E
ester.zhou 已提交
72

E
ester.zhou 已提交
73
**Example**
E
ester.zhou 已提交
74

E
ester.zhou 已提交
75
```js
E
ester.zhou 已提交
76
  var InputMethodSetting = inputMethod.getInputMethodSetting();
E
ester.zhou 已提交
77
```
78 79
## inputMethod.switchInputMethod<sup>9+</sup>

E
ester.zhou 已提交
80
switchInputMethod(target: InputMethodProperty, callback: AsyncCallback&lt;boolean&gt;): void
81

E
ester.zhou 已提交
82
Switches to another input method. This API can be used only in the stage model. It uses an asynchronous callback to return the result. If the required two parameters are not passed in, an exception is thrown.
83 84 85 86 87 88 89 90

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
|target | [InputmethodProperty](#inputmethodproperty8) | Yes| Input method to switch to.|
E
ester.zhou 已提交
91
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the execution result.|
92 93 94 95 96


**Example**

```js
E
ester.zhou 已提交
97 98 99 100 101 102 103 104 105 106 107
inputMethod.switchInputMethod({packageName:"com.example.kikakeyboard", methodId:"com.example.kikakeyboard"} ,(err,result) => {
    if (err) {
        console.error("switchInputMethod err: " + JSON.stringify(err));
        return;
    }
    if (result) {
        console.info("Success to switchInputMethod.(callback)");
    } else {
        console.error("Failed to switchInputMethod.(callback)");
    }
});
108 109
```
## inputMethod.switchInputMethod<sup>9+</sup>
E
ester.zhou 已提交
110
switchInputMethod(target: InputMethodProperty): Promise&lt;boolean&gt;
111

E
ester.zhou 已提交
112
Switches to another input method. This API can be used only in the stage model. It uses a promise to return the result. If the required parameter is not passed in, an exception is thrown.
E
ester.zhou 已提交
113

114 115 116 117 118 119 120 121 122
**System capability**: SystemCapability.MiscServices.InputMethodFramework

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
|target |  [InputmethodProperty](#inputmethodproperty8)| Yes| Input method to switch to.|

**Return value**
E
ester.zhou 已提交
123

124 125
| Type                                     | Description                        |
| ----------------------------------------- | ---------------------------- |
E
ester.zhou 已提交
126
| Promise\<boolean> | Promise used to return the execution result.|
127 128 129 130 131

**Example**


```js
E
ester.zhou 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
inputMethod.switchInputMethod({packageName:"com.example.kikakeyboard", methodId:"com.example.kikakeyboard"}).then((result) => {
    if (result) {
        console.info("Success to switchInputMethod.(promise)");
    } else {
        console.error("Failed to switchInputMethod.(promise)");
    }
}).catch((err) => {
    console.error("switchInputMethod promise err: " + err);
})
```
## inputMethod.getCurrentInputMethod<sup>9+</sup>

getCurrentInputMethod(): InputMethodProperty

Obtains the current input method. This API synchronously returns the **InputmethodProperty** instance of the current input method.

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

**Return value**

| Type                                        | Description                    |
| -------------------------------------------- | ------------------------ |
| [InputmethodProperty](#inputmethodproperty8) | **InputmethodProperty** instance of the current input method. |

**Example**


```js
var currentIme = inputMethod.getCurrentInputMethod();
161
```
E
ester.zhou 已提交
162

E
ester.zhou 已提交
163
## InputMethodController
E
ester.zhou 已提交
164

E
ester.zhou 已提交
165
In the following API examples, you must first use **[getInputMethodController](#inputmethodgetinputmethodcontroller)** to obtain an **InputMethodController** instance, and then call the APIs using the obtained instance.
E
ester.zhou 已提交
166 167 168 169 170

### stopInput

stopInput(callback: AsyncCallback&lt;boolean&gt;): void

E
ester.zhou 已提交
171
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 已提交
172

173
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
174

E
ester.zhou 已提交
175
**Parameters**
E
ester.zhou 已提交
176

E
ester.zhou 已提交
177 178 179
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return whether the keyboard is successfully hidden.|
E
ester.zhou 已提交
180

E
ester.zhou 已提交
181
**Example**
E
ester.zhou 已提交
182

E
ester.zhou 已提交
183
```js
E
ester.zhou 已提交
184 185 186 187 188 189 190 191 192 193 194
InputMethodController.stopInput((error, result) => {
    if (error) {
        console.error("failed to stopInput because: " + JSON.stringify(error));
        return;
    }
    if (result) {
        console.info("Success to stopInput.(callback)");
    } else {
        console.error("Failed to stopInput.(callback)");
    }
});
E
ester.zhou 已提交
195 196 197 198 199 200
```

### stopInput

stopInput(): Promise&lt;boolean&gt;

E
ester.zhou 已提交
201
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 已提交
202

203
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
204

E
ester.zhou 已提交
205
**Return value**
E
ester.zhou 已提交
206

E
ester.zhou 已提交
207 208
| Type| Description|
| -------- | -------- |
E
ester.zhou 已提交
209
| Promise&lt;boolean&gt; | Promise used to return whether the keyboard is successfully hidden.|
E
ester.zhou 已提交
210

E
ester.zhou 已提交
211
**Example**
E
ester.zhou 已提交
212 213


E
ester.zhou 已提交
214
```js
E
ester.zhou 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
InputMethodController.stopInput().then((result) => {
    if (result) {
        console.info("Success to stopInput.(promise)");
    } else {
        console.error("Failed to stopInput.(promise)");
    }
}).catch((err) => {
    console.error("stopInput promise err: " + err);
})
```

### showSoftKeyboard<sup>9+</sup> ###

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

Shows this soft keyboard. 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      |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the execution result.|

**Example**

```js
InputMethodController.showSoftKeyboard((err) => {
    if (err == undefined) {
        console.info('showSoftKeyboard success');
    } else {
        console.error('showSoftKeyboard failed because : ' + JSON.stringify(err));
    }
})
```


### showSoftKeyboard<sup>9+</sup> ###

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

Shows this soft keyboard. This API uses a promise to return the result. If any parameter is passed in, an exception is thrown.

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

**Return value**

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

**Example**

```js
InputMethodController.showSoftKeyboard().then(async (err) => {
    console.log('showSoftKeyboard success');
}).catch((err) => {
    console.error('showSoftKeyboard promise err: ' + JSON.stringify(err));
});
```

### hideSoftKeyboard<sup>9+</sup> ###

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

Hides this soft keyboard. 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      |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the execution result.|

**Example**

```js
InputMethodController.hideSoftKeyboard((err) => {
    if (err == undefined) {
        console.info('hideSoftKeyboard success');
    } else {
        console.error('hideSoftKeyboard failed because : ' + JSON.stringify(err));
    }
})
```


### hideSoftKeyboard<sup>9+</sup> ###

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

Hides this soft keyboard. This API uses a promise to return the result. If any parameter is passed in, an exception is thrown.

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

**Return value**

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

**Example**

```js
InputMethodController.hideSoftKeyboard().then(async (err) => {
    console.log('hideSoftKeyboard success');
}).catch((err) => {
    console.error('hideSoftKeyboard promise err: ' + JSON.stringify(err));
});
E
ester.zhou 已提交
326 327
```

E
ester.zhou 已提交
328
## InputMethodSetting<sup>8+</sup>
E
ester.zhou 已提交
329

E
ester.zhou 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
In the following API examples, you must first use **[getInputMethodSetting](#inputmethodgetinputmethodcontroller)** to obtain an **InputMethodSetting** instance, and then call the APIs using the obtained instance.

### listInputMethod<sup>9+</sup>

listInputMethod(enable: boolean, callback: AsyncCallback&lt;Array&lt;InputMethodProperty&gt;&gt;): void

Obtains a list of activated or deactivated input methods. This API uses an asynchronous callback to return the result. If the required two parameters are not passed in, an exception is thrown.

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

**Parameters**

| Name  | Type                                               | Mandatory| Description                         |
| -------- | --------------------------------------------------- | ---- | ----------------------------- |
| enable   | boolean                                             | Yes  | Whether to return a list of activated input methods. The value **true** means to return a list of activated input methods, and **false** means to return a list of deactivated input methods.      |
| callback | Array<[InputMethodProperty](#inputmethodproperty8)> | Yes  | Callback used to return a list of activated or deactivated input methods. |

**Example**

```js
imeList: Array<inputMethod.InputMethodProperty> = null
InputMethodSetting.listInputMethod(true, (err,data) => {
    if (err) {
        console.error("listInputMethod failed because: " + JSON.stringify(err));
        return;
    }
    console.log("listInputMethod success");
    this.imeList = data;
 });
```

### listInputMethod<sup>9+</sup>

listInputMethod(enable: boolean): Promise&lt;Array&lt;InputMethodProperty&gt;&gt;

Obtains a list of activated or deactivated input methods. This API uses a promise to return the result. If any parameter is passed in, an exception is thrown.

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

**Parameters**

| Name| Type   | Mandatory| Description                   |
| ------ | ------- | ---- | ----------------------- |
| enable | boolean | Yes  | Whether to return a list of activated input methods. The value **true** means to return a list of activated input methods, and **false** means to return a list of deactivated input methods.|

**Return value**

| Type                                                        | Description                         |
| ------------------------------------------------------------ | ----------------------------- |
| Promise<Array<[InputMethodProperty](#inputmethodproperty8)>> | Promise used to return a list of activated or deactivated input methods. |

**Example**

```js
imeList: Array<inputMethod.InputMethodProperty> = null
InputMethodSetting.listInputMethod(true).then((data) => {
    console.info("listInputMethod success");
    this.imeList = data;
}).catch((err) => {
    console.error("listInputMethod promise err: " + err);
})
```
E
ester.zhou 已提交
392 393 394 395 396

### listInputMethod

listInputMethod(callback: AsyncCallback&lt;Array&lt;InputMethodProperty&gt;&gt;): void

E
ester.zhou 已提交
397
Obtains a list of installed input methods. 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 已提交
398

399
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
400

E
ester.zhou 已提交
401
**Parameters**
E
ester.zhou 已提交
402

E
ester.zhou 已提交
403 404
| Name  | Type                                              | Mandatory| Description                  |
| -------- | -------------------------------------------------- | ---- | ---------------------- |
405
| callback | Array<[InputMethodProperty](#inputmethodproperty8)> | Yes  | Callback used to return the list of installed input methods.|
E
ester.zhou 已提交
406 407 408

**Example**

E
ester.zhou 已提交
409
```js
E
ester.zhou 已提交
410 411 412 413 414
imeList: Array<inputMethod.InputMethodProperty> = null
InputMethodSetting.listInputMethod((err,data) => {
    if (err) {
        console.error("listInputMethod failed because: " + JSON.stringify(err));
        return;
E
ester.zhou 已提交
415
    }
E
ester.zhou 已提交
416 417 418
    console.log("listInputMethod success");
    this.imeList = data;
 });
E
ester.zhou 已提交
419
```
E
ester.zhou 已提交
420 421 422

### listInputMethod

E
ester.zhou 已提交
423
listInputMethod(): Promise&lt;Array&lt;InputMethodProperty&gt;&gt;
E
ester.zhou 已提交
424

E
ester.zhou 已提交
425
Obtains a list of installed input methods. This API uses a promise to return the result. If any parameter is passed in, an exception is thrown.
E
ester.zhou 已提交
426

427
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
428

E
ester.zhou 已提交
429
**Return value**
E
ester.zhou 已提交
430

E
ester.zhou 已提交
431 432
| Type                                                       | Description                  |
| ----------------------------------------------------------- | ---------------------- |
E
ester.zhou 已提交
433
| Promise<Array<[InputMethodProperty](#inputmethodproperty8)>> | Promise used to return the list of installed input methods.|
E
ester.zhou 已提交
434 435 436 437

**Example**

```js
E
ester.zhou 已提交
438 439 440 441 442 443 444
imeList: Array<inputMethod.InputMethodProperty> = null
InputMethodSetting.listInputMethod().then((data) => {
    console.info("listInputMethod success");
    this.imeList = data;
}).catch((err) => {
    console.error("listInputMethod promise err: " + err);
})
E
ester.zhou 已提交
445
```
E
ester.zhou 已提交
446 447 448 449 450

### displayOptionalInputMethod

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

E
ester.zhou 已提交
451
Displays a dialog box for selecting an input method. 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 已提交
452

453
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
454

455
**Parameters**
E
ester.zhou 已提交
456 457 458 459 460

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|

E
ester.zhou 已提交
461
**Example**
462

E
ester.zhou 已提交
463
```js
E
ester.zhou 已提交
464 465 466 467 468 469 470
InputMethodSetting.displayOptionalInputMethod((err) => {
    if (err) {
        console.error("displayOptionalInputMethod failed because: " + JSON.stringify(err));
        return;
    }
    console.info("displayOptionalInputMethod success");
});
E
ester.zhou 已提交
471
```
E
ester.zhou 已提交
472 473 474

### displayOptionalInputMethod

E
ester.zhou 已提交
475
displayOptionalInputMethod(): Promise&lt;void&gt;
E
ester.zhou 已提交
476

E
ester.zhou 已提交
477
Displays a dialog box for selecting an input method. This API uses a promise to return the result. If any parameter is passed in, an exception is thrown.
E
ester.zhou 已提交
478

E
ester.zhou 已提交
479
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
480

E
ester.zhou 已提交
481
**Return value**
E
ester.zhou 已提交
482 483 484

| Type| Description|
| -------- | -------- |
E
ester.zhou 已提交
485
| Promise&lt;void&gt; | Promise that returns no value.|
E
ester.zhou 已提交
486

E
ester.zhou 已提交
487 488 489
**Example**

```js
E
ester.zhou 已提交
490 491 492 493 494
InputMethodSetting.displayOptionalInputMethod().then(() => {
    console.info("displayOptionalInputMethod success.(promise)");
}).catch((err) => {
    console.error("displayOptionalInputMethod promise err: " + err);
})
495
```