js-apis-inputmethod.md 8.0 KB
Newer Older
Z
zengyawen 已提交
1 2
# Input Method Framework

E
esterzhou 已提交
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
esterzhou 已提交
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.
Z
zengyawen 已提交
8 9 10 11


## Modules to Import

E
esterzhou 已提交
12
```js
E
ester.zhou 已提交
13
import inputMethod from '@ohos.inputmethod';
Z
zengyawen 已提交
14 15
```

E
ester.zhou 已提交
16
## Constants<sup>8+</sup>
Z
zengyawen 已提交
17 18 19

Provides the constants.

E
esterzhou 已提交
20
**System capability**: SystemCapability.Miscservices.InputMethodFramework
Z
zengyawen 已提交
21

E
esterzhou 已提交
22 23 24
| Name| Type| Value| Description|
| -------- | -------- | -------- | -------- |
| MAX_TYPE_NUM | number | 128 | Maximum number of supported input methods.|
Z
zengyawen 已提交
25

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

Describes the input method application attributes.

E
esterzhou 已提交
30
**System capability**: SystemCapability.Miscservices.InputMethodFramework
Z
zengyawen 已提交
31 32 33

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
34 35 36 37
| packageName | string | Yes| No| Name of the input method package.<br>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. You are advised to use **name**.|
| methodId | string | Yes| No| Unique ID of the input method.<br>**NOTE**<br>This API is supported since API version 8 and deprecated since API version 9. You are advised to use **id**.|


Z
zengyawen 已提交
38

E
ester.zhou 已提交
39
## inputMethod.getInputMethodController
Z
zengyawen 已提交
40 41 42

getInputMethodController(): InputMethodController

E
esterzhou 已提交
43
Obtains an **[InputMethodController](#inputmethodcontroller)** instance.
Z
zengyawen 已提交
44

E
esterzhou 已提交
45
**System capability**: SystemCapability.Miscservices.InputMethodFramework
Z
zengyawen 已提交
46

E
ester.zhou 已提交
47
**Return value**
Z
zengyawen 已提交
48

E
esterzhou 已提交
49 50 51
| Type                                           | Description                    |
| ----------------------------------------------- | ------------------------ |
| [InputMethodController](#inputmethodcontroller) | Current **InputMethodController** instance.|
E
ester.zhou 已提交
52 53 54

**Example**

E
ester.zhou 已提交
55
```js
E
esterzhou 已提交
56
let inputMethodController = inputMethod.getInputMethodController();
E
ester.zhou 已提交
57
```
Z
zengyawen 已提交
58

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

getInputMethodSetting(): InputMethodSetting

E
esterzhou 已提交
63
Obtains an **[InputMethodSetting](#inputmethodsetting)** instance.
Z
zengyawen 已提交
64

E
esterzhou 已提交
65
**System capability**: SystemCapability.Miscservices.InputMethodFramework
Z
zengyawen 已提交
66

E
ester.zhou 已提交
67
**Return value**
Z
zengyawen 已提交
68

E
ester.zhou 已提交
69 70
| Type                                     | Description                        |
| ----------------------------------------- | ---------------------------- |
E
esterzhou 已提交
71
| [InputMethodSetting](#inputmethodsetting) | Current **InputMethodSetting** instance.|
Z
zengyawen 已提交
72

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

E
ester.zhou 已提交
75
```js
E
esterzhou 已提交
76
let inputMethodSetting = inputMethod.getInputMethodSetting();
E
ester.zhou 已提交
77
```
Z
zengyawen 已提交
78

E
ester.zhou 已提交
79
## InputMethodController
Z
zengyawen 已提交
80

E
esterzhou 已提交
81
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.
Z
zengyawen 已提交
82 83 84 85 86

### stopInput

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

E
esterzhou 已提交
87
Ends this input session. The invoking of this API takes effect only after the input session is enabled by clicking the text box. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
88

E
esterzhou 已提交
89
**System capability**: SystemCapability.Miscservices.InputMethodFramework
Z
zengyawen 已提交
90

E
ester.zhou 已提交
91
**Parameters**
Z
zengyawen 已提交
92

E
ester.zhou 已提交
93 94
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
95
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is **true**. Otherwise, **err** is an error object. |
Z
zengyawen 已提交
96

E
ester.zhou 已提交
97
**Example**
Z
zengyawen 已提交
98

E
ester.zhou 已提交
99
```js
E
esterzhou 已提交
100 101 102 103 104 105 106 107 108 109 110
inputMethodController.stopInput((error, result) => {
    if (error) {
        console.error('Failed to stop inputmethod session: ' + JSON.stringify(error));
        return;
    }
    if (result) {
        console.info('Succeeded in stopping inputmethod session.');
    } else {
        console.error('Failed to stop inputmethod session.');
    }
});
Z
zengyawen 已提交
111 112 113 114 115 116
```

### stopInput

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

E
esterzhou 已提交
117
Ends this input session. The invoking of this API takes effect only after the input session is enabled by clicking the text box. This API uses a promise to return the result.
Z
zengyawen 已提交
118

E
esterzhou 已提交
119
**System capability**: SystemCapability.Miscservices.InputMethodFramework
Z
zengyawen 已提交
120

E
ester.zhou 已提交
121
**Return value**
Z
zengyawen 已提交
122

E
ester.zhou 已提交
123 124
| Type| Description|
| -------- | -------- |
E
esterzhou 已提交
125
| Promise&lt;boolean&gt; | Promise used to return the result. The value **true** means that the hiding is successful, and **false** means the opposite.|
Z
zengyawen 已提交
126

E
ester.zhou 已提交
127
**Example**
Z
zengyawen 已提交
128

E
ester.zhou 已提交
129
```js
E
esterzhou 已提交
130 131 132 133 134 135 136
inputMethodController.stopInput().then((result) => {
    if (result) {
        console.info('Succeeded in stopping inputmethod session.');
    } else {
        console.error('Failed to stop inputmethod session');
    }
})
Z
zengyawen 已提交
137 138
```

E
ester.zhou 已提交
139
## InputMethodSetting<sup>8+</sup>
Z
zengyawen 已提交
140

E
esterzhou 已提交
141
In the following API examples, you must first use [getInputMethodSetting](#inputmethodgetinputmethodsetting) to obtain an **InputMethodSetting** instance, and then call the APIs using the obtained instance.
Z
zengyawen 已提交
142 143 144 145 146

### listInputMethod

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

E
esterzhou 已提交
147
Obtains a list of installed input methods. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
148

E
esterzhou 已提交
149
**System capability**: SystemCapability.Miscservices.InputMethodFramework
Z
zengyawen 已提交
150

E
ester.zhou 已提交
151 152 153 154
**Parameters**

| Name  | Type                                              | Mandatory| Description                  |
| -------- | -------------------------------------------------- | ---- | ---------------------- |
E
ester.zhou 已提交
155
| callback | AsyncCallback&lt;Array<[InputMethodProperty](#inputmethodproperty8)>&gt; | Yes  | Callback used to return the list of installed input methods.|
E
ester.zhou 已提交
156 157 158

**Example**

E
ester.zhou 已提交
159
```js
E
esterzhou 已提交
160 161 162 163
inputMethodSetting.listInputMethod((err, data) => {
    if(err) {
        console.error('Failed to list inputmethods: ' + JSON.stringify(err));
        return;
E
ester.zhou 已提交
164
    }
E
esterzhou 已提交
165 166
    console.log('Succeeded in listing inputmethods, data: ' + JSON.stringify(data));
 });
E
ester.zhou 已提交
167
```
Z
zengyawen 已提交
168

E
ester.zhou 已提交
169
### listInputMethod<sup>8+</sup>
Z
zengyawen 已提交
170 171 172

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

E
esterzhou 已提交
173
Obtains a list of installed input methods. This API uses a promise to return the result.
Z
zengyawen 已提交
174

E
esterzhou 已提交
175
**System capability**: SystemCapability.Miscservices.InputMethodFramework
Z
zengyawen 已提交
176

E
ester.zhou 已提交
177
**Return value**
Z
zengyawen 已提交
178

E
ester.zhou 已提交
179 180
| Type                                                       | Description                  |
| ----------------------------------------------------------- | ---------------------- |
E
esterzhou 已提交
181
| Promise<Array<[InputMethodProperty](#inputmethodproperty8)>> | Promise used to return the list of installed input methods.|
E
ester.zhou 已提交
182 183 184 185

**Example**

```js
E
esterzhou 已提交
186 187 188 189 190
inputMethodSetting.listInputMethod().then((data) => {
    console.info('Succeeded in listing inputMethod.');
}).catch((err) => {
    console.error('Failed to list inputMethod: ' + JSON.stringify(err));
})
E
ester.zhou 已提交
191
```
Z
zengyawen 已提交
192

E
ester.zhou 已提交
193
### displayOptionalInputMethod<sup>8+</sup>
Z
zengyawen 已提交
194 195 196 197 198

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

Displays a dialog box for selecting an input method. This API uses an asynchronous callback to return the result.

E
esterzhou 已提交
199
**System capability**: SystemCapability.Miscservices.InputMethodFramework
Z
zengyawen 已提交
200

E
esterzhou 已提交
201
**Parameters**
Z
zengyawen 已提交
202 203 204

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
205
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. |
Z
zengyawen 已提交
206

E
ester.zhou 已提交
207
**Example**
E
esterzhou 已提交
208

E
ester.zhou 已提交
209
```js
E
esterzhou 已提交
210 211 212 213 214 215 216
inputMethodSetting.displayOptionalInputMethod((err) => {
    if (err) {
        console.error('Failed to display optionalInputMethod:' + JSON.stringify(err));
        return;
    }
    console.info('Succeeded in displaying optionalInputMethod.');
});
E
ester.zhou 已提交
217
```
Z
zengyawen 已提交
218

E
ester.zhou 已提交
219
### displayOptionalInputMethod<sup>8+</sup>
Z
zengyawen 已提交
220 221 222

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

E
esterzhou 已提交
223
Displays a dialog box for selecting an input method. This API uses a promise to return the result.
Z
zengyawen 已提交
224

E
esterzhou 已提交
225
**System capability**: SystemCapability.Miscservices.InputMethodFramework
Z
zengyawen 已提交
226

E
ester.zhou 已提交
227
**Return value**
Z
zengyawen 已提交
228 229 230

| Type| Description|
| -------- | -------- |
E
esterzhou 已提交
231
| Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
232

E
ester.zhou 已提交
233 234 235
**Example**

```js
E
esterzhou 已提交
236 237 238 239 240 241
inputMethodSetting.displayOptionalInputMethod().then(() => {
    console.info('Succeeded in displaying optionalInputMethod.');
}).catch((err) => {
    console.error('Failed to display optionalInputMethod: ' + JSON.stringify(err));
})
```