js-apis-inputmethod.md 16.0 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                    |
| ----------------------------------------------- | ------------------------ |
E
ester.zhou 已提交
50
| [InputMethodController](#inputmethodcontroller) | 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                        |
| ----------------------------------------- | ---------------------------- |
E
ester.zhou 已提交
70
| [InputMethodSetting](#inputmethodsetting8) | Current **InputMethodSetting** instance.|
71

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
inputMethod.switchInputMethod({packageName:'com.example.kikakeyboard', methodId:'com.example.kikakeyboard'} ,(err,result) => {
E
ester.zhou 已提交
98
    if (err) {
E
ester.zhou 已提交
99
        console.error('switchInputMethod err: ' + JSON.stringify(err));
E
ester.zhou 已提交
100 101 102
        return;
    }
    if (result) {
E
ester.zhou 已提交
103
        console.info('Success to switchInputMethod.(callback)');
E
ester.zhou 已提交
104
    } else {
E
ester.zhou 已提交
105
        console.error('Failed to switchInputMethod.(callback)');
E
ester.zhou 已提交
106 107
    }
});
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
inputMethod.switchInputMethod({packageName:'com.example.kikakeyboard', methodId:'com.example.kikakeyboard'}).then((result) => {
E
ester.zhou 已提交
133
    if (result) {
E
ester.zhou 已提交
134
        console.info('Success to switchInputMethod.(promise)');
E
ester.zhou 已提交
135
    } else {
E
ester.zhou 已提交
136
        console.error('Failed to switchInputMethod.(promise)');
E
ester.zhou 已提交
137 138
    }
}).catch((err) => {
E
ester.zhou 已提交
139
    console.error('switchInputMethod promise err: ' + err);
E
ester.zhou 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153
})
```
## 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                    |
| -------------------------------------------- | ------------------------ |
E
ester.zhou 已提交
154
| [InputmethodProperty](#inputmethodproperty8) | **InputmethodProperty** instance of the current input method.|
E
ester.zhou 已提交
155 156 157 158 159 160

**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
InputMethodController.stopInput((error, result) => {
    if (error) {
E
ester.zhou 已提交
186
        console.error('failed to stopInput because: ' + JSON.stringify(error));
E
ester.zhou 已提交
187 188 189
        return;
    }
    if (result) {
E
ester.zhou 已提交
190
        console.info('Success to stopInput.(callback)');
E
ester.zhou 已提交
191
    } else {
E
ester.zhou 已提交
192
        console.error('Failed to stopInput.(callback)');
E
ester.zhou 已提交
193 194
    }
});
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
InputMethodController.stopInput().then((result) => {
    if (result) {
E
ester.zhou 已提交
217
        console.info('Success to stopInput.(promise)');
E
ester.zhou 已提交
218
    } else {
E
ester.zhou 已提交
219
        console.error('Failed to stopInput.(promise)');
E
ester.zhou 已提交
220 221
    }
}).catch((err) => {
E
ester.zhou 已提交
222
    console.error('stopInput promise err: ' + err);
E
ester.zhou 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
})
```

### 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) => {
E
ester.zhou 已提交
244
    if (err === undefined) {
E
ester.zhou 已提交
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
        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) => {
E
ester.zhou 已提交
295
    if (err === undefined) {
E
ester.zhou 已提交
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
        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
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.      |
E
ester.zhou 已提交
345
| callback | Array<[InputMethodProperty](#inputmethodproperty8)> | Yes  | Callback used to return a list of activated or deactivated input methods.|
E
ester.zhou 已提交
346 347 348 349 350 351

**Example**

```js
InputMethodSetting.listInputMethod(true, (err,data) => {
    if (err) {
E
ester.zhou 已提交
352
        console.error('listInputMethod failed because: ' + JSON.stringify(err));
E
ester.zhou 已提交
353 354
        return;
    }
E
ester.zhou 已提交
355
    console.log('listInputMethod success');
E
ester.zhou 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
 });
```

### 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                         |
| ------------------------------------------------------------ | ----------------------------- |
E
ester.zhou 已提交
377
| Promise<Array<[InputMethodProperty](#inputmethodproperty8)>> | Promise used to return a list of activated or deactivated input methods.|
E
ester.zhou 已提交
378 379 380 381 382

**Example**

```js
InputMethodSetting.listInputMethod(true).then((data) => {
E
ester.zhou 已提交
383
    console.info('listInputMethod success');
E
ester.zhou 已提交
384
}).catch((err) => {
E
ester.zhou 已提交
385
    console.error('listInputMethod promise err: ' + err);
E
ester.zhou 已提交
386 387
})
```
E
ester.zhou 已提交
388 389 390 391 392

### listInputMethod

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

E
ester.zhou 已提交
393
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 已提交
394

395
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
396

E
ester.zhou 已提交
397
**Parameters**
E
ester.zhou 已提交
398

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

**Example**

E
ester.zhou 已提交
405
```js
E
ester.zhou 已提交
406 407
InputMethodSetting.listInputMethod((err,data) => {
    if (err) {
E
ester.zhou 已提交
408
        console.error('listInputMethod failed because: ' + JSON.stringify(err));
E
ester.zhou 已提交
409
        return;
E
ester.zhou 已提交
410
    }
E
ester.zhou 已提交
411
    console.log('listInputMethod success');
E
ester.zhou 已提交
412
 });
E
ester.zhou 已提交
413
```
E
ester.zhou 已提交
414 415 416

### listInputMethod

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

E
ester.zhou 已提交
419
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 已提交
420

421
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
422

E
ester.zhou 已提交
423
**Return value**
E
ester.zhou 已提交
424

E
ester.zhou 已提交
425 426
| Type                                                       | Description                  |
| ----------------------------------------------------------- | ---------------------- |
E
ester.zhou 已提交
427
| Promise<Array<[InputMethodProperty](#inputmethodproperty8)>> | Promise used to return the list of installed input methods.|
E
ester.zhou 已提交
428 429 430 431

**Example**

```js
E
ester.zhou 已提交
432
InputMethodSetting.listInputMethod().then((data) => {
E
ester.zhou 已提交
433
    console.info('listInputMethod success');
E
ester.zhou 已提交
434
}).catch((err) => {
E
ester.zhou 已提交
435
    console.error('listInputMethod promise err: ' + err);
E
ester.zhou 已提交
436
})
E
ester.zhou 已提交
437
```
E
ester.zhou 已提交
438 439 440 441 442

### displayOptionalInputMethod

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

E
ester.zhou 已提交
443
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 已提交
444

445
**System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
446

447
**Parameters**
E
ester.zhou 已提交
448 449 450 451 452

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

E
ester.zhou 已提交
453
**Example**
454

E
ester.zhou 已提交
455
```js
E
ester.zhou 已提交
456 457
InputMethodSetting.displayOptionalInputMethod((err) => {
    if (err) {
E
ester.zhou 已提交
458
        console.error('displayOptionalInputMethod failed because: ' + JSON.stringify(err));
E
ester.zhou 已提交
459 460
        return;
    }
E
ester.zhou 已提交
461
    console.info('displayOptionalInputMethod success');
E
ester.zhou 已提交
462
});
E
ester.zhou 已提交
463
```
E
ester.zhou 已提交
464 465 466

### displayOptionalInputMethod

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

E
ester.zhou 已提交
469
  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 已提交
470

E
ester.zhou 已提交
471
  **System capability**: SystemCapability.MiscServices.InputMethodFramework
E
ester.zhou 已提交
472

E
ester.zhou 已提交
473
**Return value**
E
ester.zhou 已提交
474 475 476

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

E
ester.zhou 已提交
479 480 481
**Example**

```js
E
ester.zhou 已提交
482
InputMethodSetting.displayOptionalInputMethod().then(() => {
E
ester.zhou 已提交
483
    console.info('displayOptionalInputMethod success.(promise)');
E
ester.zhou 已提交
484
}).catch((err) => {
E
ester.zhou 已提交
485
    console.error('displayOptionalInputMethod promise err: ' + err);
E
ester.zhou 已提交
486
})
487
```