1# Input Method Framework 2 3The **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. 4 5> **NOTE** 6> 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. 8 9 10## Modules to Import 11 12```js 13import inputMethod from '@ohos.inputmethod'; 14``` 15 16## Constants<sup>8+</sup> 17 18Provides the constants. 19 20**System capability**: SystemCapability.Miscservices.InputMethodFramework 21 22| Name| Type| Value| Description| 23| -------- | -------- | -------- | -------- | 24| MAX_TYPE_NUM | number | 128 | Maximum number of supported input methods.| 25 26## InputMethodProperty<sup>8+</sup> 27 28Describes the input method application attributes. 29 30**System capability**: SystemCapability.Miscservices.InputMethodFramework 31 32| Name| Type| Readable| Writable| Description| 33| -------- | -------- | -------- | -------- | -------- | 34| 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**.| 35| 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**.| 36 37 38 39## inputMethod.getInputMethodController 40 41getInputMethodController(): InputMethodController 42 43Obtains an **[InputMethodController](#inputmethodcontroller)** instance. 44 45**System capability**: SystemCapability.Miscservices.InputMethodFramework 46 47**Return value** 48 49| Type | Description | 50| ----------------------------------------------- | ------------------------ | 51| [InputMethodController](#inputmethodcontroller) | Current **InputMethodController** instance.| 52 53**Example** 54 55```js 56let inputMethodController = inputMethod.getInputMethodController(); 57``` 58 59## inputMethod.getInputMethodSetting<sup>8+</sup> 60 61getInputMethodSetting(): InputMethodSetting 62 63Obtains an **[InputMethodSetting](#inputmethodsetting)** instance. 64 65**System capability**: SystemCapability.Miscservices.InputMethodFramework 66 67**Return value** 68 69| Type | Description | 70| ----------------------------------------- | ---------------------------- | 71| [InputMethodSetting](#inputmethodsetting) | Current **InputMethodSetting** instance.| 72 73**Example** 74 75```js 76let inputMethodSetting = inputMethod.getInputMethodSetting(); 77``` 78 79## InputMethodController 80 81In the following API examples, you must first use [getInputMethodController](#inputmethodgetinputmethodcontroller) to obtain an **InputMethodController** instance, and then call the APIs using the obtained instance. 82 83### stopInput 84 85stopInput(callback: AsyncCallback<boolean>): void 86 87Ends 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. 88 89**System capability**: SystemCapability.Miscservices.InputMethodFramework 90 91**Parameters** 92 93| Name| Type| Mandatory| Description| 94| -------- | -------- | -------- | -------- | 95| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is **true**. Otherwise, **err** is an error object. | 96 97**Example** 98 99```js 100inputMethodController.stopInput((error, result) => { 101 if (error) { 102 console.error('Failed to stop inputmethod session: ' + JSON.stringify(error)); 103 return; 104 } 105 if (result) { 106 console.info('Succeeded in stopping inputmethod session.'); 107 } else { 108 console.error('Failed to stop inputmethod session.'); 109 } 110}); 111``` 112 113### stopInput 114 115stopInput(): Promise<boolean> 116 117Ends 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. 118 119**System capability**: SystemCapability.Miscservices.InputMethodFramework 120 121**Return value** 122 123| Type| Description| 124| -------- | -------- | 125| Promise<boolean> | Promise used to return the result. The value **true** means that the hiding is successful, and **false** means the opposite.| 126 127**Example** 128 129```js 130inputMethodController.stopInput().then((result) => { 131 if (result) { 132 console.info('Succeeded in stopping inputmethod session.'); 133 } else { 134 console.error('Failed to stop inputmethod session'); 135 } 136}) 137``` 138 139## InputMethodSetting<sup>8+</sup> 140 141In the following API examples, you must first use [getInputMethodSetting](#inputmethodgetinputmethodsetting) to obtain an **InputMethodSetting** instance, and then call the APIs using the obtained instance. 142 143### listInputMethod 144 145listInputMethod(callback: AsyncCallback<Array<InputMethodProperty>>): void 146 147Obtains a list of installed input methods. This API uses an asynchronous callback to return the result. 148 149**System capability**: SystemCapability.Miscservices.InputMethodFramework 150 151**Parameters** 152 153| Name | Type | Mandatory| Description | 154| -------- | -------------------------------------------------- | ---- | ---------------------- | 155| callback | AsyncCallback<Array<[InputMethodProperty](#inputmethodproperty8)>> | Yes | Callback used to return the list of installed input methods.| 156 157**Example** 158 159```js 160inputMethodSetting.listInputMethod((err, data) => { 161 if(err) { 162 console.error('Failed to list inputmethods: ' + JSON.stringify(err)); 163 return; 164 } 165 console.log('Succeeded in listing inputmethods, data: ' + JSON.stringify(data)); 166 }); 167``` 168 169### listInputMethod<sup>8+</sup> 170 171listInputMethod(): Promise<Array<InputMethodProperty>> 172 173Obtains a list of installed input methods. This API uses a promise to return the result. 174 175**System capability**: SystemCapability.Miscservices.InputMethodFramework 176 177**Return value** 178 179| Type | Description | 180| ----------------------------------------------------------- | ---------------------- | 181| Promise<Array<[InputMethodProperty](#inputmethodproperty8)>> | Promise used to return the list of installed input methods.| 182 183**Example** 184 185```js 186inputMethodSetting.listInputMethod().then((data) => { 187 console.info('Succeeded in listing inputMethod.'); 188}).catch((err) => { 189 console.error('Failed to list inputMethod: ' + JSON.stringify(err)); 190}) 191``` 192 193### displayOptionalInputMethod<sup>8+</sup> 194 195displayOptionalInputMethod(callback: AsyncCallback<void>): void 196 197Displays a dialog box for selecting an input method. This API uses an asynchronous callback to return the result. 198 199**System capability**: SystemCapability.Miscservices.InputMethodFramework 200 201**Parameters** 202 203| Name| Type| Mandatory| Description| 204| -------- | -------- | -------- | -------- | 205| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 206 207**Example** 208 209```js 210inputMethodSetting.displayOptionalInputMethod((err) => { 211 if (err) { 212 console.error('Failed to display optionalInputMethod:' + JSON.stringify(err)); 213 return; 214 } 215 console.info('Succeeded in displaying optionalInputMethod.'); 216}); 217``` 218 219### displayOptionalInputMethod<sup>8+</sup> 220 221displayOptionalInputMethod(): Promise<void> 222 223Displays a dialog box for selecting an input method. This API uses a promise to return the result. 224 225**System capability**: SystemCapability.Miscservices.InputMethodFramework 226 227**Return value** 228 229| Type| Description| 230| -------- | -------- | 231| Promise<void> | Promise that returns no value.| 232 233**Example** 234 235```js 236inputMethodSetting.displayOptionalInputMethod().then(() => { 237 console.info('Succeeded in displaying optionalInputMethod.'); 238}).catch((err) => { 239 console.error('Failed to display optionalInputMethod: ' + JSON.stringify(err)); 240}) 241``` 242