• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.inputMethod (输入法框架)
2<!--Kit: IME Kit-->
3<!--Subsystem: MiscServices-->
4<!--Owner: @illybyy-->
5<!--Designer: @andeszhang-->
6<!--Tester: @murphy1984-->
7<!--Adviser: @zhang_yixin13-->
8
9本模块主要面向普通前台应用(备忘录、信息、设置等系统应用与三方应用),提供对输入法(输入法应用)的控制、管理能力,包括显示/隐藏输入法软键盘、切换输入法、获取所有输入法列表等等。
10
11> **说明:**
12>
13> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
14
15
16## 导入模块
17
18```ts
19import { inputMethod } from '@kit.IMEKit';
20```
21
22## 常量
23
24常量值。
25
26**系统能力:** SystemCapability.MiscServices.InputMethodFramework
27
28| 参数名 | 类型 | 常量值 | 说明 |
29| -------- | -------- | -------- | -------- |
30| MAX_TYPE_NUM<sup>8+</sup> | number | 128 | 可支持的最大输入法个数。 |
31
32## InputMethodProperty<sup>8+</sup>
33
34输入法应用属性。
35
36**系统能力:** SystemCapability.MiscServices.InputMethodFramework
37
38| 名称 | 类型 | 只读 | 可选 | 说明 |
39| -------- | -------- | -------- | -------- | -------- |
40| name<sup>9+</sup>  | string | 是 | 否 | 必填。输入法包名。|
41| id<sup>9+</sup>    | string | 是 | 否 | 必填。输入法扩展在应用内唯一标识,与name一起组成输入法扩展的全局唯一标识。|
42| label<sup>9+</sup>    | string | 是 | 是 | 非必填。<br>- 当InputMethodProperty用于切换、查询等接口的入参时,开发者可不填写此字段,通过name和id即可唯一指定一个输入法扩展。<br>- 当InputMethodProperty作为查询接口的返回值时(如[getCurrentInputMethod](#inputmethodgetcurrentinputmethod9)),此字段表示输入法扩展对外显示的名称,优先使用InputMethodExtensionAbility中配置的label,若未配置,自动使用应用入口ability的label;当应用入口ability未配置label时,自动使用应用AppScope中配置的label。|
43| labelId<sup>10+</sup>    | number | 是 | 是 | 非必填。<br>- 当InputMethodProperty用于切换、查询等接口的入参时,开发者可不填写此字段,通过name和id即可唯一指定一个输入法扩展。<br>- 当InputMethodProperty作为查询接口的返回值时(如[getCurrentInputMethod](#inputmethodgetcurrentinputmethod9)),此字段表示label字段的资源号。|
44| icon<sup>9+</sup>    | string | 是 | 是 | 非必填。<br>- 当InputMethodProperty用于切换、查询等接口的入参时,开发者可不填写此字段,通过name和id即可唯一指定一个输入法扩展。<br>- 当InputMethodProperty作为查询接口的返回值时(如[getCurrentInputMethod](#inputmethodgetcurrentinputmethod9)),此字段表示输入法图标数据,可以通过iconId查询获取。|
45| iconId<sup>9+</sup>    | number | 是 | 是 | 非必填。<br>- 当InputMethodProperty用于切换、查询等接口的入参时,开发者可不填写此字段,通过name和id即可唯一指定一个输入法扩展。<br>- 当InputMethodProperty作为查询接口的返回值时(如[getCurrentInputMethod](#inputmethodgetcurrentinputmethod9)),此字段表示icon字段的资源号。|
46| enabledState<sup>20+</sup>    | [EnabledState](js-apis-inputmethod.md#enabledstate15) | 是 | 是 | 非必填。<br>- 当InputMethodProperty用于切换、查询等接口的入参时,开发者可不填写此字段,通过name和id即可唯一指定一个输入法扩展<br>- 当InputMethodProperty作为查询接口的返回值时(如[getCurrentInputMethod](#inputmethodgetcurrentinputmethod9)),此字段表示该输入法启用状态。|
47| extra<sup>9+</sup>    | object | 否 | 是 | 输入法扩展信息。预留字段,当前无具体含义,暂不支持使用。<br/>- API version 10起:非必填;<br/>- API version 9:必填。|
48| packageName<sup>(deprecated)</sup> | string | 是 | 否 | 输入法包名。必填。<br/>说明:从API version 8开始支持,从API version 9开始废弃,建议使用name替代。 |
49| methodId<sup>(deprecated)</sup> | string | 是 | 否 | 输入法唯一标识。必填。<br/>说明:从API version 8开始支持,从API version 9开始废弃,建议使用id替代。 |
50
51## CapitalizeMode<sup>20+</sup>
52
53枚举,定义了文本首字母大写的不同模式。
54
55**系统能力:** SystemCapability.MiscServices.InputMethodFramework
56
57| 名称 | 值 | 说明 |
58| -------- | -- | -------- |
59| NONE | 0 | 不进行任何首字母大写处理。|
60| SENTENCES | 1 | 每个句子的首字母大写。|
61| WORDS | 2 | 每个单词首字母大写。|
62| CHARACTERS | 3 | 每个字母都大写。|
63
64## inputMethod.getController<sup>9+</sup>
65
66getController(): InputMethodController
67
68获取客户端实例[InputMethodController](#inputmethodcontroller)。
69
70**系统能力:** SystemCapability.MiscServices.InputMethodFramework
71
72**返回值:**
73
74| 类型                                            | 说明                   |
75| ----------------------------------------------- | ---------------------- |
76| [InputMethodController](#inputmethodcontroller) | 返回当前客户端实例。 |
77
78**错误码:**
79
80以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
81
82| 错误码ID | 错误信息                     |
83| -------- | ------------------------------ |
84| 12800006 | input method controller error. Possible cause: create InputMethodController object failed. |
85
86**示例:**
87
88```ts
89let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
90```
91
92## inputMethod.getDefaultInputMethod<sup>11+</sup>
93
94getDefaultInputMethod(): InputMethodProperty
95
96获取默认输入法。
97
98**系统能力:** SystemCapability.MiscServices.InputMethodFramework
99
100**返回值:**
101
102| 类型                                         | 说明                     |
103| -------------------------------------------- | ------------------------ |
104| [InputMethodProperty](#inputmethodproperty8) | 返回默认输入法属性对象。 |
105
106**错误码:**
107
108以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
109
110| 错误码ID | 错误信息                             |
111| -------- | -------------------------------------- |
112| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
113
114**示例:**
115
116```ts
117let defaultIme: inputMethod.InputMethodProperty = inputMethod.getDefaultInputMethod();
118```
119
120## inputMethod.getSystemInputMethodConfigAbility<sup>11+</sup>
121
122getSystemInputMethodConfigAbility(): ElementName
123
124获取系统输入法设置界面Ability信息。
125
126**系统能力:** SystemCapability.MiscServices.InputMethodFramework
127
128**返回值:**
129
130| 类型                                         | 说明                     |
131| -------------------------------------------- | ------------------------ |
132| [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | 系统输入法设置界面Ability的ElementName。 |
133
134**错误码:**
135
136以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
137
138| 错误码ID | 错误信息                             |
139| -------- | -------------------------------------- |
140| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
141
142**示例:**
143
144```ts
145import { bundleManager } from '@kit.AbilityKit';
146
147let inputMethodConfig: bundleManager.ElementName = inputMethod.getSystemInputMethodConfigAbility();
148```
149
150## inputMethod.getSetting<sup>9+</sup>
151
152getSetting(): InputMethodSetting
153
154获取客户端设置实例[InputMethodSetting](#inputmethodsetting8)。
155
156**系统能力:** SystemCapability.MiscServices.InputMethodFramework
157
158**返回值:**
159
160| 类型                                      | 说明                       |
161| ----------------------------------------- | -------------------------- |
162| [InputMethodSetting](#inputmethodsetting8) | 返回当前客户端设置实例。 |
163
164**错误码:**
165
166以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
167
168| 错误码ID | 错误信息                             |
169| -------- | -------------------------------------- |
170| 12800007 |  input method setter error. Possible cause: create InputMethodSetting object failed. |
171
172**示例:**
173
174```ts
175let inputMethodSetting: inputMethod.InputMethodSetting = inputMethod.getSetting();
176```
177
178## inputMethod.switchInputMethod<sup>9+</sup>
179
180switchInputMethod(target: InputMethodProperty, callback: AsyncCallback&lt;boolean&gt;): void
181
182切换输入法,使用callback异步回调。
183> **说明:**
184>
185>  - 在API version 9-10版本,仅支持系统应用调用且需要权限ohos.permission.CONNECT_IME_ABILITY186>  - 在API version 11版本起,仅支持当前输入法应用调用。
187
188**系统能力:** SystemCapability.MiscServices.InputMethodFramework
189
190**参数:**
191
192| 参数名 | 类型 | 必填 | 说明 |
193| -------- | -------- | -------- | -------- |
194| target | [InputMethodProperty](#inputmethodproperty8) | 是 | 目标输入法。 |
195| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数。当输入法切换成功,err为undefined,data为true;否则为错误对象。 |
196
197**错误码:**
198
199以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
200
201| 错误码ID | 错误信息                             |
202| -------- | -------------------------------------- |
203| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
204| 12800005 | configuration persistence error.        |
205| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
206
207**示例:**
208
209```ts
210import { BusinessError } from '@kit.BasicServicesKit';
211
212let currentIme: inputMethod.InputMethodProperty = inputMethod.getCurrentInputMethod();
213inputMethod.switchInputMethod(currentIme, (err: BusinessError, result: boolean) => {
214  if (err) {
215    console.error(`Failed to switchInputMethod, code: ${err.code}, message: ${err.message}`);
216    return;
217  }
218  if (result) {
219    console.info('Succeeded in switching input method.');
220  } else {
221    console.error('Failed to switch input method.');
222  }
223});
224```
225
226> **说明:**
227>
228> 在 api11 中 ` 201 permissions check fails.` 这个错误码被移除。
229
230## inputMethod.switchInputMethod<sup>9+</sup>
231switchInputMethod(target: InputMethodProperty): Promise&lt;boolean&gt;
232
233切换输入法,使用promise异步回调。
234> **说明:**
235>
236>  - 在API version 9-10版本,仅支持系统应用调用且需要权限ohos.permission.CONNECT_IME_ABILITY237>  - 在API version 11版本起,仅支持当前输入法应用调用。
238
239**系统能力:** SystemCapability.MiscServices.InputMethodFramework
240
241**参数:**
242
243  | 参数名 | 类型 | 必填 | 说明 |
244  | -------- | -------- | -------- | -------- |
245  |target |  [InputMethodProperty](#inputmethodproperty8)| 是 | 目标输入法。 |
246
247**返回值:**
248
249  | 类型                                      | 说明                         |
250  | ----------------------------------------- | ---------------------------- |
251  | Promise\<boolean> | Promise对象。返回true表示切换输入法成功,返回false表示切换输入法失败。 |
252
253**错误码:**
254
255以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
256
257| 错误码ID | 错误信息                             |
258| -------- | -------------------------------------- |
259| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
260| 12800005 | configuration persistence error.        |
261| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
262
263**示例:**
264
265```ts
266import { BusinessError } from '@kit.BasicServicesKit';
267
268let currentIme: inputMethod.InputMethodProperty = inputMethod.getCurrentInputMethod();
269inputMethod.switchInputMethod(currentIme).then((result: boolean) => {
270  if (result) {
271    console.info('Succeeded in switching input method.');
272  } else {
273    console.error('Failed to switch input method.');
274  }
275}).catch((err: BusinessError) => {
276  console.error(`Failed to switchInputMethod, code: ${err.code}, message: ${err.message}`);
277});
278```
279
280> **说明:**
281>
282> 在 api11 中 ` 201 permissions check fails.` 这个错误码被移除。
283
284## inputMethod.getCurrentInputMethod<sup>9+</sup>
285
286getCurrentInputMethod(): InputMethodProperty
287
288使用同步方法获取当前输入法。
289
290**系统能力:** SystemCapability.MiscServices.InputMethodFramework
291
292**返回值:**
293
294| 类型                                         | 说明                     |
295| -------------------------------------------- | ------------------------ |
296| [InputMethodProperty](#inputmethodproperty8) | 返回当前输入法属性对象。 |
297
298**示例:**
299
300```ts
301let currentIme: inputMethod.InputMethodProperty = inputMethod.getCurrentInputMethod();
302```
303
304## inputMethod.switchCurrentInputMethodSubtype<sup>9+</sup>
305
306switchCurrentInputMethodSubtype(target: InputMethodSubtype, callback: AsyncCallback\<boolean>): void
307
308切换当前输入法的子类型。使用callback异步回调。
309
310> **说明:**
311>
312>  - 在API version 9版本,仅支持系统应用调用且需要权限ohos.permission.CONNECT_IME_ABILITY313>  - 在API version 10版本,支持系统应用和当前输入法应用调用;需要权限ohos.permission.CONNECT_IME_ABILITY314>  - 在API version 11版本起,仅支持当前输入法调用。
315
316**系统能力:** SystemCapability.MiscServices.InputMethodFramework
317
318**参数:**
319
320| 参数名 | 类型 | 必填 | 说明 |
321| -------- | -------- | -------- | -------- |
322| target |  [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)| 是 | 目标输入法子类型。 |
323| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数。当输入法子类型切换成功,err为undefined,data为true;否则为错误对象。|
324
325**错误码:**
326
327以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
328
329| 错误码ID | 错误信息                             |
330| -------- | -------------------------------------- |
331| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
332| 12800005 | configuration persistence error.        |
333| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
334
335**示例:**
336
337```ts
338import { BusinessError } from '@kit.BasicServicesKit';
339
340let extra: Record<string, string> = {}
341inputMethod.switchCurrentInputMethodSubtype({
342  id: "ServiceExtAbility",
343  label: "",
344  name: "com.example.keyboard",
345  mode: "upper",
346  locale: "",
347  language: "",
348  icon: "",
349  iconId: 0,
350  extra: extra
351}, (err: BusinessError, result: boolean) => {
352  if (err) {
353    console.error(`Failed to switchCurrentInputMethodSubtype, code: ${err.code}, message: ${err.message}`);
354    return;
355  }
356  if (result) {
357    console.info('Succeeded in switching currentInputMethodSubtype.');
358  } else {
359    console.error('Failed to switchCurrentInputMethodSubtype');
360  }
361});
362```
363
364> **说明:**
365>
366> 在 api11 中 ` 201 permissions check fails.` 这个错误码被移除。
367
368## inputMethod.switchCurrentInputMethodSubtype<sup>9+</sup>
369
370switchCurrentInputMethodSubtype(target: InputMethodSubtype): Promise&lt;boolean&gt;
371
372切换当前输入法的子类型。使用promise异步回调。
373
374> **说明:**
375>
376>  - 在API version 9版本,仅支持系统应用调用且需要权限ohos.permission.CONNECT_IME_ABILITY377>  - 在API version 10版本,支持系统应用和当前输入法应用调用;需要权限ohos.permission.CONNECT_IME_ABILITY378>  - 在API version 11版本起,仅支持当前输入法调用。
379
380**系统能力:** SystemCapability.MiscServices.InputMethodFramework
381
382**参数:**
383
384| 参数名 | 类型 | 必填 | 说明 |
385| -------- | -------- | -------- | -------- |
386|target |  [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)| 是 | 目标输入法子类型。 |
387
388**返回值:**
389
390| 类型                                      | 说明                         |
391| ----------------------------------------- | ---------------------------- |
392| Promise\<boolean> | Promise对象。返回true表示当前输入法切换子类型成功,返回false表示当前输入法切换子类型成功失败。 |
393
394**错误码:**
395
396以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
397
398| 错误码ID | 错误信息                             |
399| -------- | -------------------------------------- |
400| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
401| 12800005 | configuration persistence error.        |
402| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
403
404**示例:**
405
406```ts
407import { BusinessError } from '@kit.BasicServicesKit';
408
409let extra: Record<string, string> = {}
410inputMethod.switchCurrentInputMethodSubtype({
411  id: "ServiceExtAbility",
412  label: "",
413  name: "com.example.keyboard",
414  mode: "upper",
415  locale: "",
416  language: "",
417  icon: "",
418  iconId: 0,
419  extra: extra
420}).then((result: boolean) => {
421  if (result) {
422    console.info('Succeeded in switching currentInputMethodSubtype.');
423  } else {
424    console.error('Failed to switchCurrentInputMethodSubtype.');
425  }
426}).catch((err: BusinessError) => {
427  console.error(`Failed to switchCurrentInputMethodSubtype, code: ${err.code}, message: ${err.message}`);
428});
429```
430
431> **说明:**
432>
433> 在 api11 中 ` 201 permissions check fails.` 这个错误码被移除。
434
435## inputMethod.getCurrentInputMethodSubtype<sup>9+</sup>
436
437getCurrentInputMethodSubtype(): InputMethodSubtype
438
439获取当前输入法的子类型。
440
441**系统能力:** SystemCapability.MiscServices.InputMethodFramework
442
443**返回值:**
444
445| 类型                                         | 说明                     |
446| -------------------------------------------- | ------------------------ |
447| [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype) | 返回当前输入法子类型对象。 |
448
449**示例:**
450
451```ts
452import { InputMethodSubtype } from '@kit.IMEKit';
453
454let currentImeSubType: InputMethodSubtype = inputMethod.getCurrentInputMethodSubtype();
455```
456
457## inputMethod.switchCurrentInputMethodAndSubtype<sup>9+</sup>
458
459switchCurrentInputMethodAndSubtype(inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype, callback: AsyncCallback\<boolean>): void
460
461切换至指定输入法的指定子类型,适用于跨输入法切换子类型。使用callback异步回调。
462
463> **说明:**
464>
465>  - 在API version 9-10版本,仅支持系统应用调用且需要权限ohos.permission.CONNECT_IME_ABILITY466>  - 在API version 11版本起,仅支持当前输入法调用。
467
468**系统能力:** SystemCapability.MiscServices.InputMethodFramework
469
470**参数:**
471
472| 参数名 | 类型 | 必填 | 说明 |
473| -------- | -------- | -------- | -------- |
474|inputMethodProperty |  [InputMethodProperty](#inputmethodproperty8)| 是 | 目标输入法。 |
475|inputMethodSubtype |  [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)| 是 | 目标输入法子类型。 |
476| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数。当输入法和子类型切换成功,err为undefined,data为获取到的切换子类型结果true;否则为错误对象。 |
477
478**错误码:**
479
480以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
481
482| 错误码ID | 错误信息                             |
483| -------- | -------------------------------------- |
484| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
485| 12800005 | configuration persistence error.        |
486| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
487
488**示例:**
489
490```ts
491import { InputMethodSubtype } from '@kit.IMEKit';
492import { BusinessError } from '@kit.BasicServicesKit';
493
494let currentIme: inputMethod.InputMethodProperty = inputMethod.getCurrentInputMethod();
495let imSubType: InputMethodSubtype = inputMethod.getCurrentInputMethodSubtype();
496inputMethod.switchCurrentInputMethodAndSubtype(currentIme, imSubType, (err: BusinessError, result: boolean) => {
497  if (err) {
498    console.error(`Failed to switchCurrentInputMethodAndSubtype, code: ${err.code}, message: ${err.message}`);
499    return;
500  }
501  if (result) {
502    console.info('Succeeded in switching currentInputMethodAndSubtype.');
503  } else {
504    console.error('Failed to switchCurrentInputMethodAndSubtype.');
505  }
506});
507```
508
509> **说明:**
510>
511> 在 api11 中 ` 201 permissions check fails.` 这个错误码被移除。
512
513## inputMethod.switchCurrentInputMethodAndSubtype<sup>9+</sup>
514
515switchCurrentInputMethodAndSubtype(inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype): Promise&lt;boolean&gt;
516
517切换至指定输入法的指定子类型,适用于跨输入法切换子类型。使用promise异步回调。
518
519> **说明:**
520>
521>  - 在API version 9-10版本,仅支持系统应用调用且需要权限ohos.permission.CONNECT_IME_ABILITY522>  - 在API version 11版本起,仅支持当前输入法调用。
523
524**系统能力:** SystemCapability.MiscServices.InputMethodFramework
525
526**参数:**
527
528| 参数名 | 类型 | 必填 | 说明 |
529| -------- | -------- | -------- | -------- |
530|inputMethodProperty |  [InputMethodProperty](#inputmethodproperty8)| 是 | 目标输入法。 |
531|inputMethodSubtype |  [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)| 是 | 目标输入法子类型。 |
532
533**返回值:**
534
535| 类型                                      | 说明                         |
536| ----------------------------------------- | ---------------------------- |
537| Promise\<boolean> | Promise对象。返回true表示切换至指定输入法的指定子类型成功,返回false表示切换至指定输入法的指定子类型失败。 |
538
539**错误码:**
540
541以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
542
543| 错误码ID | 错误信息                             |
544| -------- | -------------------------------------- |
545| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
546| 12800005 | configuration persistence error.        |
547| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
548
549**示例:**
550
551```ts
552import { InputMethodSubtype } from '@kit.IMEKit';
553import { BusinessError } from '@kit.BasicServicesKit';
554
555let currentIme: inputMethod.InputMethodProperty = inputMethod.getCurrentInputMethod();
556let imSubType: InputMethodSubtype = inputMethod.getCurrentInputMethodSubtype();
557inputMethod.switchCurrentInputMethodAndSubtype(currentIme, imSubType).then((result: boolean) => {
558  if (result) {
559    console.info('Succeeded in switching currentInputMethodAndSubtype.');
560  } else {
561    console.error('Failed to switchCurrentInputMethodAndSubtype.');
562  }
563}).catch((err: BusinessError) => {
564  console.error(`Failed to switchCurrentInputMethodAndSubtype, code: ${err.code}, message: ${err.message}`);
565});
566```
567
568> **说明:**
569>
570> 在 api11 中 ` 201 permissions check fails.` 这个错误码被移除。
571
572## inputMethod.getInputMethodController<sup>(deprecated)</sup>
573
574getInputMethodController(): InputMethodController
575
576获取客户端实例[InputMethodController](#inputmethodcontroller)。
577
578> **说明:**
579>
580> 从API version 6开始支持,从API version 9开始废弃,建议使用[getController()](#inputmethodgetcontroller9)替代。
581
582**系统能力:** SystemCapability.MiscServices.InputMethodFramework
583
584**返回值:**
585
586| 类型                                            | 说明                     |
587| ----------------------------------------------- | ------------------------ |
588| [InputMethodController](#inputmethodcontroller) | 回调返回当前客户端实例。 |
589
590**示例:**
591
592```ts
593let inputMethodController: inputMethod.InputMethodController = inputMethod.getInputMethodController();
594```
595
596## inputMethod.getInputMethodSetting<sup>(deprecated)</sup>
597
598getInputMethodSetting(): InputMethodSetting
599
600获取客户端设置实例[InputMethodSetting](#inputmethodsetting8)。
601
602> **说明:**
603>
604> 从API version 8开始支持,从API version 9开始废弃,建议使用[getSetting()](#inputmethodgetsetting9)替代。
605
606**系统能力:** SystemCapability.MiscServices.InputMethodFramework
607
608**返回值:**
609
610| 类型                                      | 说明                       |
611| ----------------------------------------- | -------------------------- |
612| [InputMethodSetting](#inputmethodsetting8) | 返回当前客户端设置实例。 |
613
614**示例:**
615
616```ts
617let inputMethodSetting: inputMethod.InputMethodSetting = inputMethod.getInputMethodSetting();
618```
619
620## inputMethod.setSimpleKeyboardEnabled<sup>20+</sup>
621
622setSimpleKeyboardEnabled(enable: boolean): void
623
624编辑框应用设置简单键盘标志。
625
626**系统能力:** SystemCapability.MiscServices.InputMethodFramework
627
628**参数:**
629
630| 参数名 | 类型 | 必填 | 说明 |
631| -------- | -------- | -------- | -------- |
632| enable | boolean | 是 | 简单键盘是否使能标志,true标识简单键盘使能,false标识简单键盘去使能。<br/> 原生编辑框组件在下一次点击获焦时生效;自绘控件在下一次调用[attach](#attach10)绑定输入法时生效。 |
633
634**示例:**
635
636```ts
637  let enable: boolean = false;
638  inputMethod.setSimpleKeyboardEnabled(enable);
639```
640
641## TextInputType<sup>10+</sup>
642
643文本输入类型。
644
645**系统能力:** SystemCapability.MiscServices.InputMethodFramework
646
647| 名称 | 值 |说明 |
648| -------- | -------- |-------- |
649| NONE  | -1 |NONE。 |
650| TEXT  | 0 |文本类型。 |
651| MULTILINE  | 1 |多行类型。 |
652| NUMBER  | 2 |数字类型。 |
653| PHONE  | 3 |电话号码类型。 |
654| DATETIME  | 4 |日期类型。 |
655| EMAIL_ADDRESS  | 5 |邮箱地址类型。 |
656| URL  | 6 |链接类型。 |
657| VISIBLE_PASSWORD  | 7 |密码类型。 |
658| NUMBER_PASSWORD<sup>11+</sup> | 8 |数字密码类型。 |
659| SCREEN_LOCK_PASSWORD<sup>20+</sup> | 9 |锁屏密码类型。 |
660| USER_NAME<sup>20+</sup> | 10 |用户名类型。 |
661| NEW_PASSWORD<sup>20+</sup> | 11 |新密码类型。 |
662| NUMBER_DECIMAL<sup>20+</sup> | 12 |带小数点的数字类型。 |
663| ONE_TIME_CODE<sup>20+</sup> | 13 |验证码类型。 |
664
665## EnterKeyType<sup>10+</sup>
666
667Enter键的功能类型。
668
669**系统能力:** SystemCapability.MiscServices.InputMethodFramework
670
671| 名称 | 值 |说明 |
672| -------- | -------- |-------- |
673| UNSPECIFIED  | 0 |未指定。 |
674| NONE  | 1 |NONE。 |
675| GO  | 2 |前往。 |
676| SEARCH  | 3 |查找。 |
677| SEND  | 4 |发送。 |
678| NEXT  | 5 |下一步。 |
679| DONE  | 6 |完成。 |
680| PREVIOUS  | 7 |上一步。 |
681| NEWLINE<sup>12+</sup>  | 8 | 换行。|
682
683## KeyboardStatus<sup>10+</sup>
684
685输入法软键盘状态。
686
687**系统能力:** SystemCapability.MiscServices.InputMethodFramework
688
689| 名称 | 值 |说明 |
690| -------- | -------- |-------- |
691| NONE  | 0 |NONE。 |
692| HIDE  | 1 |隐藏状态。 |
693| SHOW  | 2 |显示状态。 |
694
695## Direction<sup>10+</sup>
696
697光标移动方向。
698
699**系统能力:** SystemCapability.MiscServices.InputMethodFramework
700
701| 名称 | 值 |说明 |
702| -------- | -------- |-------- |
703| CURSOR_UP  | 1 |向上。 |
704| CURSOR_DOWN  | 2 |向下。 |
705| CURSOR_LEFT  | 3 |向左。 |
706| CURSOR_RIGHT  | 4 |向右。 |
707
708## ExtendAction<sup>10+</sup>
709
710编辑框中文本的扩展编辑操作类型,如剪切、复制等。
711
712**系统能力:** SystemCapability.MiscServices.InputMethodFramework
713
714| 名称 | 值 |说明 |
715| -------- | -------- |-------- |
716| SELECT_ALL  | 0 |全选。 |
717| CUT  | 3 |剪切。 |
718| COPY  | 4 |复制。 |
719| PASTE  | 5 |粘贴。 |
720
721## FunctionKey<sup>10+</sup>
722
723输入法功能键类型。
724
725**系统能力:** SystemCapability.MiscServices.InputMethodFramework
726
727| 名称 | 类型 | 只读 | 可选 | 说明 |
728| -------- | -------- | -------- | -------- | -------- |
729| enterKeyType<sup>10+</sup>  | [EnterKeyType](#enterkeytype10) | 否 | 否 | 输入法enter键类型。|
730
731## InputAttribute<sup>10+</sup>
732
733编辑框属性,包含文本输入类型和Enter键功能类型。
734
735**系统能力:** SystemCapability.MiscServices.InputMethodFramework
736
737| 名称 | 类型 | 只读 | 可选 | 说明 |
738| -------- | -------- | -------- | -------- | -------- |
739| textInputType<sup>10+</sup>  | [TextInputType](#textinputtype10) | 否 | 否 | 文本输入类型。|
740| enterKeyType<sup>10+</sup>  | [EnterKeyType](#enterkeytype10) | 否 | 否 | Enter键功能类型。|
741| placeholder<sup>20+</sup> | string | 否 | 是 | 编辑框设置的占位符信息。 <br/>- 编辑框设置占位符信息时,长度不超过255个字符(如果超出将会自动截断为255个字符),用于提示或引导用户输入临时性文本或符号。(例如:提示输入项为"必填"或"非必填"的输入结果反馈。)<br/>- 编辑框没有设置占位符信息时,默认为空字符串。<br/>- 该字段在调用[attach](#attach10)时提供给输入法应用。|
742| abilityName<sup>20+</sup> | string | 否 | 是 | 编辑框设置的ability名称。<br/>- 编辑框设置ability名称时,长度不超过127个字符(如果超出将会自动截断为127个字符)。<br/>- 编辑框未设置ability名称时,默认为空字符串。<br/>- 该字段在调用绑定[attach](#attach10)时提供给输入法应用。|
743
744## TextConfig<sup>10+</sup>
745
746编辑框的配置信息。
747
748**系统能力:** SystemCapability.MiscServices.InputMethodFramework
749
750| 名称 | 类型 | 只读 | 可选 | 说明 |
751| -------- | -------- | -------- | -------- | -------- |
752| inputAttribute<sup>10+</sup>  | [InputAttribute](#inputattribute10) | 否 | 否 | 编辑框属性。|
753| cursorInfo<sup>10+</sup>  | [CursorInfo](#cursorinfo10) | 否 | 是 | 光标信息。|
754| selection<sup>10+</sup>  | [Range](#range10) | 否 | 是 | 文本选中的范围。|
755| windowId<sup>10+</sup>  | number | 否 | 是 | 编辑框所在的窗口Id,该参数应为整数。<br>推荐使用[getWindowProperties()](../apis-arkui/arkts-apis-window-Window.md#getwindowproperties9)方法获取窗口id属性。|
756| newEditBox<sup>20+</sup> | boolean | 否 | 是 | 表示是否为新编辑框。true表示新编辑框,false表示非新编辑框。 |
757| capitalizeMode<sup>20+</sup> | [CapitalizeMode](#capitalizemode20) | 否 | 是 | 编辑框设置大小写模式。如果没有设置或设置非法值,默认不进行任何首字母大写处理。|
758
759## CursorInfo<sup>10+</sup>
760
761光标信息。
762
763**系统能力:** SystemCapability.MiscServices.InputMethodFramework
764
765| 名称 | 类型 | 只读 | 可选 | 说明 |
766| -------- | -------- | -------- | -------- | -------- |
767| left  | number | 否 | 否 | 光标的横坐标,单位为px。该参数应为整数,最小值为0,最大值为当前屏幕的宽度。|
768| top  | number | 否 | 否 | 光标的纵坐标,单位为px。该参数应为整数,最小值为0,最大值为当前屏幕的高度。|
769| width  | number | 否 | 否 | 光标的宽度,单位为px。该参数应为整数,最小值为0,最大值为当前屏幕的宽度。|
770| height  | number | 否 | 否 | 光标的高度,单位为px。该参数应为整数,最小值为0,最大值为当前屏幕的高度|
771
772## Range<sup>10+</sup>
773
774文本的选中范围。
775
776**系统能力:** SystemCapability.MiscServices.InputMethodFramework
777
778| 名称 | 类型 | 只读 | 可选 | 说明 |
779| -------- | -------- | -------- | -------- | -------- |
780| start  | number | 否 | 否 | 选中文本的首字符在编辑框的索引值。该参数应为大于或等于0的整数,不超过文本实际长度。|
781| end  | number | 否 | 否 | 选中文本的末字符在编辑框的索引值。该参数应为大于或等于0的整数,不超过文本实际长度,end值要大于start值。|
782
783## Movement<sup>10+</sup>
784
785选中文本时,光标移动的方向。
786
787**系统能力:** SystemCapability.MiscServices.InputMethodFramework
788
789| 名称 | 类型 | 只读 | 可选 | 说明 |
790| -------- | -------- | -------- | -------- | -------- |
791| direction  | [Direction](#direction10) | 否 | 否 | 选中文本时,光标的移动方向。|
792
793## InputWindowInfo<sup>10+</sup>
794
795输入法软键盘的窗口信息。
796
797**系统能力:** SystemCapability.MiscServices.InputMethodFramework
798
799| 名称 | 类型 | 只读 | 可选 | 说明 |
800| -------- | -------- | -------- | -------- | -------- |
801| name  | string | 否 | 否 | 输入法窗口的名称。|
802| left  | number | 否 | 否 | 输入法窗口左上顶点的横坐标,单位为px。该参数应为整数,最小值为0,最大值为当前屏幕的宽度。|
803| top  | number | 否 | 否 | 输入法窗口左上顶点的纵坐标,单位为px。该参数应为整数,最小值为0,最大值为当前屏幕的高度。|
804| width  | number | 否 | 否 | 输入法窗口的宽度,单位为px。该参数应为整数,最小值为0,最大值为当前屏幕的宽度。|
805| height  | number | 否 | 否 | 输入法窗口的高度,单位为px。该参数应为整数,最小值为0,最大值为当前屏幕的高度。|
806
807## EnabledState<sup>15+</sup>
808
809输入法启用状态。
810
811**系统能力:** SystemCapability.MiscServices.InputMethodFramework
812
813| 名称 | 值 |说明 |
814| -------- | -------- |-------- |
815| DISABLED   | 0 |未启用。 |
816| BASIC_MODE  | 1 |基础模式。 |
817| FULL_EXPERIENCE_MODE  | 2 |完整体验模式。 |
818
819## RequestKeyboardReason<sup>15+</sup>
820
821请求键盘输入原因。
822
823**系统能力:** SystemCapability.MiscServices.InputMethodFramework
824
825| 名称 | 值 |说明 |
826| -------- | -------- |-------- |
827| NONE   | 0 |表示没有特定的原因触发键盘请求。 |
828| MOUSE  | 1 |表示键盘请求是由鼠标操作触发的。 |
829| TOUCH  | 2 |表示键盘请求是由触摸操作触发的。 |
830| OTHER  | 20 |表示键盘请求是由其他原因触发的。 |
831
832## MessageHandler<sup>15+</sup>
833
834自定义通信对象。
835
836> **说明:**
837>
838> 开发者可通过注册此对象来接收输入法应用发送的自定义通信数据,接收到自定义通信数据时会触发此对象中[onMessage](#onmessage15)回调函数。
839>
840> 此对象全局唯一,多次注册仅保留最后一次注册的对象及有效性,并触发上一个已注册对象的[onTerminated](#onterminated15)回调函数。
841>
842> 若取消注册全局已注册的对象时,会触发被取消对象中[onTerminated](#onterminated15)回调函数。
843
844### onMessage<sup>15+</sup>
845
846onMessage(msgId: string, msgParam?: ArrayBuffer): void
847
848接收输入法应用发送的自定义数据回调函数。
849
850> **说明:**
851>
852> 当已注册的MessageHandler接收到来自输入法应用发送的自定义通信数据时,会触发该回调函数。
853>
854> msgId为必选参数,msgParam为可选参数。存在收到仅有msgId自定义数据的可能,需与数据发送方确认自定义数据。
855
856**系统能力:** SystemCapability.MiscServices.InputMethodFramework
857
858**参数:**
859
860| 参数名   | 类型        | 必填 | 说明                             |
861| -------- | ----------- | ---- | -------------------------------- |
862| msgId    | string      | 是   | 接收到的自定义通信数据的标识符。 |
863| msgParam | ArrayBuffer | 否   | 接收到的自定义通信数据的消息体。 |
864
865**示例:**
866
867```ts
868let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
869
870let messageHandler: inputMethod.MessageHandler = {
871  onTerminated(): void {
872    console.info('OnTerminated.');
873  },
874  onMessage(msgId: string, msgParam?: ArrayBuffer): void {
875    console.info('recv message.');
876  }
877};
878inputMethodController.recvMessage(messageHandler);
879```
880
881### onTerminated<sup>15+</sup>
882
883onTerminated(): void
884
885监听对象终止回调函数。
886
887> **说明:**
888>
889> 当应用注册新的MessageHandler对象时,会触发上一个已注册MessageHandler对象的OnTerminated回调函数。
890>
891> 当应用取消注册时,会触发当前已注册MessageHandler对象的OnTerminated回调函数。
892
893**系统能力:** SystemCapability.MiscServices.InputMethodFramework
894
895**示例:**
896
897```ts
898let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
899
900let messageHandler: inputMethod.MessageHandler = {
901  onTerminated(): void {
902    console.info('OnTerminated.');
903  },
904  onMessage(msgId: string, msgParam?: ArrayBuffer): void {
905    console.info('recv message.');
906  }
907};
908inputMethodController.recvMessage(messageHandler);
909```
910
911## SetPreviewTextCallback<sup>17+</sup>
912
913type SetPreviewTextCallback = (text: string, range: Range) => void
914
915当输入法框架需要显示预览文本时触发的回调。
916
917**系统能力:** SystemCapability.MiscServices.InputMethodFramework
918
919**参数:**
920
921| 参数名       | 类型          | 必填 | 说明                          |
922| ------- | ----------------- | ---- | ----------------------------- |
923| text    | string            | 是   | 预览文本内容。                 |
924| range   | [Range](#range10) | 是   | 文本的选中范围。 |
925
926## InputMethodController
927
928下列API示例中都需使用[getController](#inputmethodgetcontroller9)获取到InputMethodController实例,再通过实例调用对应方法。
929
930### attach<sup>10+</sup>
931
932attach(showKeyboard: boolean, textConfig: TextConfig, callback: AsyncCallback&lt;void&gt;): void
933
934自绘控件绑定输入法。使用callback异步回调。
935
936> **说明:**
937>
938> 需要先调用此接口,完成自绘控件与输入法的绑定,才能使用以下功能:显示/隐藏键盘、更新光标信息、更改编辑框选中范围、保存配置信息、监听处理由输入法应用发送的信息或命令等。
939
940**系统能力:** SystemCapability.MiscServices.InputMethodFramework
941
942**参数:**
943
944| 参数名 | 类型 | 必填 | 说明 |
945| -------- | -------- | -------- | -------- |
946| showKeyboard | boolean | 是 | 绑定输入法成功后,是否拉起输入法键盘。<br>- true表示拉起。<br>- false表示不拉起。 |
947| textConfig | [TextConfig](#textconfig10) | 是 | 编辑框的配置信息。 |
948| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当绑定输入法成功后,err为undefined;否则为错误对象。 |
949
950**错误码:**
951
952以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
953
954| 错误码ID | 错误信息                             |
955| -------- | -------------------------------------- |
956| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
957| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
958| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
959
960**示例:**
961
962```ts
963import { BusinessError } from '@kit.BasicServicesKit';
964
965let inputAttribute: inputMethod.InputAttribute = {
966  textInputType: inputMethod.TextInputType.TEXT,
967  enterKeyType: inputMethod.EnterKeyType.GO
968}
969let textConfig: inputMethod.TextConfig = { inputAttribute: inputAttribute };
970inputMethod.getController().attach(true, textConfig, (err: BusinessError) => {
971  if (err) {
972    console.error(`Failed to attach, code: ${err.code}, message: ${err.message}`);
973    return;
974  }
975  console.info('Succeeded in attaching the inputMethod.');
976});
977```
978
979### attach<sup>10+</sup>
980
981attach(showKeyboard: boolean, textConfig: TextConfig): Promise&lt;void&gt;
982
983自绘控件绑定输入法。使用promise异步回调。
984
985> **说明:**
986>
987> 需要先调用此接口,完成自绘控件与输入法的绑定,才能使用以下功能:显示/隐藏键盘、更新光标信息、更改编辑框选中范围、保存配置信息、监听处理由输入法应用发送的信息或命令等。
988
989**系统能力:** SystemCapability.MiscServices.InputMethodFramework
990
991**参数:**
992
993| 参数名 | 类型 | 必填 | 说明 |
994| -------- | -------- | -------- | -------- |
995| showKeyboard | boolean | 是 | 绑定输入法成功后,是否拉起输入法键盘。<br>- true表示拉起。<br>- false表示不拉起。|
996| textConfig | [TextConfig](#textconfig10) | 是 | 编辑框的配置信息。 |
997
998**返回值:**
999
1000| 类型 | 说明 |
1001| -------- | -------- |
1002| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1003
1004**错误码:**
1005
1006以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
1007
1008| 错误码ID | 错误信息                             |
1009| -------- | -------------------------------------- |
1010| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1011| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1012| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1013
1014**示例:**
1015
1016```ts
1017import { BusinessError } from '@kit.BasicServicesKit';
1018
1019let inputAttribute: inputMethod.InputAttribute = {
1020  textInputType: inputMethod.TextInputType.TEXT,
1021  enterKeyType: inputMethod.EnterKeyType.GO
1022}
1023let textConfig: inputMethod.TextConfig = { inputAttribute: inputAttribute };
1024inputMethod.getController().attach(true, textConfig).then(() => {
1025  console.info('Succeeded in attaching inputMethod.');
1026}).catch((err: BusinessError) => {
1027  console.error(`Failed to attach, code: ${err.code}, message: ${err.message}`);
1028});
1029```
1030
1031### attach<sup>15+</sup>
1032
1033attach(showKeyboard: boolean, textConfig: TextConfig, requestKeyboardReason: RequestKeyboardReason): Promise&lt;void&gt;
1034
1035自绘控件绑定输入法。使用promise异步回调。
1036
1037> **说明:**
1038>
1039> 需要先调用此接口,完成自绘控件与输入法的绑定,才能使用以下功能:显示/隐藏键盘、更新光标信息、更改编辑框选中范围、保存配置信息、监听处理由输入法应用发送的信息或命令等。
1040
1041**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1042
1043**参数:**
1044
1045| 参数名 | 类型 | 必填 | 说明 |
1046| -------- | -------- | -------- | -------- |
1047| showKeyboard | boolean | 是 | 绑定输入法成功后,是否拉起输入法键盘。<br>- true表示拉起。<br>- false表示不拉起。|
1048| textConfig | [TextConfig](#textconfig10) | 是 | 编辑框的配置信息。 |
1049| requestKeyboardReason | [RequestKeyboardReason](#requestkeyboardreason15) | 是 | 请求键盘输入原因。 |
1050
1051**返回值:**
1052
1053| 类型 | 说明 |
1054| -------- | -------- |
1055| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1056
1057**错误码:**
1058
1059以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)和[通用错误码说明文档](../errorcode-universal.md)。
1060
1061| 错误码ID | 错误信息                             |
1062| -------- | -------------------------------------- |
1063| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1064| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1065| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1066
1067**示例:**
1068
1069```ts
1070import { BusinessError } from '@kit.BasicServicesKit';
1071
1072let inputAttribute: inputMethod.InputAttribute = {
1073  textInputType: inputMethod.TextInputType.TEXT,
1074  enterKeyType: inputMethod.EnterKeyType.GO
1075}
1076let textConfig: inputMethod.TextConfig = { inputAttribute: inputAttribute };
1077let requestKeyboardReason: inputMethod.RequestKeyboardReason = inputMethod.RequestKeyboardReason.MOUSE;
1078
1079inputMethod.getController().attach(true, textConfig, requestKeyboardReason).then(() => {
1080  console.info('Succeeded in attaching inputMethod.');
1081}).catch((err: BusinessError) => {
1082  console.error(`Failed to attach, code: ${err.code}, message: ${err.message}`);
1083});
1084```
1085
1086### discardTypingText<sup>20+</sup>
1087
1088discardTypingText(): Promise&lt;void&gt;
1089
1090编辑框应用发送“清空正在输入的文字”命令到输入法。使用promise异步回调。
1091
1092> **说明:**
1093>
1094> 当编辑框应用与输入法绑定成功后,才可调用该接口实现此功能。
1095
1096**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1097
1098**返回值:**
1099
1100| 类型 | 说明 |
1101| -------- | -------- |
1102| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
1103
1104**错误码:**
1105
1106以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
1107
1108| 错误码ID | 错误信息                             |
1109| -------- | -------------------------------------- |
1110| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1111| 12800009 | input method client detached. |
1112| 12800015 | the other side does not accept the request. |
1113
1114**示例:**
1115
1116```ts
1117import { BusinessError } from '@kit.BasicServicesKit';
1118
1119inputMethod.getController().discardTypingText().then(() => {
1120  console.info('Succeeded discardTypingText.');
1121}).catch((err: BusinessError) => {
1122  console.error(`Failed to discardTypingText, code: ${err.code}, message: ${err.message}`);
1123});
1124```
1125
1126### showTextInput<sup>10+</sup>
1127
1128showTextInput(callback: AsyncCallback&lt;void&gt;): void
1129
1130进入文本编辑状态。使用callback异步回调。
1131
1132> **说明:**
1133>
1134> 编辑框与输入法绑定成功后,可调用该接口拉起软键盘,进入文本编辑状态。
1135
1136**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1137
1138**参数:**
1139
1140| 参数名 | 类型 | 必填 | 说明 |
1141| -------- | -------- | -------- | -------- |
1142| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。若成功进入编辑状态,err为undefined;否则为错误对象。 |
1143
1144**错误码:**
1145
1146以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
1147
1148| 错误码ID | 错误信息                             |
1149| -------- | -------------------------------------- |
1150| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1151| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1152| 12800009 | input method client detached. |
1153
1154**示例:**
1155
1156```ts
1157import { BusinessError } from '@kit.BasicServicesKit';
1158
1159inputMethod.getController().showTextInput((err: BusinessError) => {
1160  if (err) {
1161    console.error(`Failed to showTextInput, code: ${err.code}, message: ${err.message}`);
1162    return;
1163  }
1164  console.info('Succeeded in showing the inputMethod.');
1165});
1166```
1167
1168### showTextInput<sup>10+</sup>
1169
1170showTextInput(): Promise&lt;void&gt;
1171
1172进入文本编辑状态。使用promise异步回调。
1173
1174> **说明:**
1175>
1176> 编辑框与输入法绑定成功后,可调用该接口拉起软键盘,进入文本编辑状态。
1177
1178**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1179
1180**返回值:**
1181
1182| 类型 | 说明 |
1183| -------- | -------- |
1184| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1185
1186**错误码:**
1187
1188以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
1189
1190| 错误码ID | 错误信息                             |
1191| -------- | -------------------------------------- |
1192| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1193| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1194| 12800009 | input method client detached. |
1195
1196**示例:**
1197
1198```ts
1199import { BusinessError } from '@kit.BasicServicesKit';
1200
1201inputMethod.getController().showTextInput().then(() => {
1202  console.info('Succeeded in showing text input.');
1203}).catch((err: BusinessError) => {
1204  console.error(`Failed to showTextInput, code: ${err.code}, message: ${err.message}`);
1205});
1206```
1207
1208### showTextInput<sup>15+</sup>
1209
1210showTextInput(requestKeyboardReason: RequestKeyboardReason): Promise&lt;void&gt;
1211
1212进入文本编辑状态。使用promise异步回调。
1213
1214> **说明:**
1215>
1216> 编辑框与输入法绑定成功后,可调用该接口拉起软键盘,进入文本编辑状态。
1217
1218**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1219
1220**参数:**
1221
1222| 参数名 | 类型 | 必填 | 说明 |
1223| -------- | -------- | -------- | -------- |
1224| requestKeyboardReason | [RequestKeyboardReason](#requestkeyboardreason15) | 是 | 请求键盘输入原因。 |
1225
1226**返回值:**
1227
1228| 类型 | 说明 |
1229| -------- | -------- |
1230| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1231
1232**错误码:**
1233
1234以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
1235
1236| 错误码ID | 错误信息                             |
1237| -------- | -------------------------------------- |
1238| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1239| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1240| 12800009 | input method client detached. |
1241
1242**示例:**
1243
1244```ts
1245import { BusinessError } from '@kit.BasicServicesKit';
1246
1247let requestKeyboardReason: inputMethod.RequestKeyboardReason = inputMethod.RequestKeyboardReason.MOUSE;
1248
1249inputMethod.getController().showTextInput(requestKeyboardReason).then(() => {
1250  console.info('Succeeded in showing text input.');
1251}).catch((err: BusinessError) => {
1252  console.error(`Failed to showTextInput, code: ${err.code}, message: ${err.message}`);
1253});
1254```
1255
1256### hideTextInput<sup>10+</sup>
1257
1258hideTextInput(callback: AsyncCallback&lt;void&gt;): void
1259
1260退出文本编辑状态。使用callback异步回调。
1261
1262> **说明:**
1263>
1264> 调用接口时,若软键盘处于显示状态,调用接口后软键盘会被隐藏。
1265>
1266> 调用该接口不会解除与输入法的绑定,再次调用[showTextInput](#showtextinput10)时,可重新进入文本编辑状态。
1267
1268**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1269
1270**参数:**
1271
1272| 参数名 | 类型 | 必填 | 说明 |
1273| -------- | -------- | -------- | -------- |
1274| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当成功退出编辑状态时,err为undefined;否则为错误对象。 |
1275
1276**错误码:**
1277
1278以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
1279
1280| 错误码ID | 错误信息                             |
1281| -------- | -------------------------------------- |
1282| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1283| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1284| 12800009 | input method client detached.             |
1285
1286**示例:**
1287
1288```ts
1289import { BusinessError } from '@kit.BasicServicesKit';
1290
1291inputMethod.getController().hideTextInput((err: BusinessError) => {
1292  if (err) {
1293    console.error(`Failed to hideTextInput, code: ${err.code}, message: ${err.message}`);
1294    return;
1295  }
1296  console.info('Succeeded in hiding text input.');
1297});
1298```
1299
1300### hideTextInput<sup>10+</sup>
1301
1302hideTextInput(): Promise&lt;void&gt;
1303
1304退出文本编辑状态。使用promise异步回调。
1305
1306> **说明:**
1307>
1308> 调用接口时,若软键盘处于显示状态,调用接口后软键盘会被隐藏。
1309>
1310> 调用该接口不会解除与输入法的绑定,再次调用[showTextInput](#showtextinput10)时,可重新进入文本编辑状态。
1311
1312**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1313
1314**返回值:**
1315
1316| 类型 | 说明 |
1317| -------- | -------- |
1318| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1319
1320**错误码:**
1321
1322以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
1323
1324| 错误码ID | 错误信息                             |
1325| -------- | -------------------------------------- |
1326| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1327| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1328| 12800009 | input method client detached. |
1329
1330**示例:**
1331
1332```ts
1333import { BusinessError } from '@kit.BasicServicesKit';
1334
1335inputMethod.getController().hideTextInput().then(() => {
1336  console.info('Succeeded in hiding inputMethod.');
1337}).catch((err: BusinessError) => {
1338  console.error(`Failed to hideTextInput, code: ${err.code}, message: ${err.message}`);
1339})
1340```
1341
1342### detach<sup>10+</sup>
1343
1344detach(callback: AsyncCallback&lt;void&gt;): void
1345
1346自绘控件解除与输入法的绑定。使用callback异步回调。
1347
1348**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1349
1350**参数:**
1351
1352| 参数名 | 类型 | 必填 | 说明 |
1353| -------- | -------- | -------- | -------- |
1354| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当解绑定输入法成功时,err为undefined;否则为错误对象。 |
1355
1356**错误码:**
1357
1358以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
1359
1360| 错误码ID | 错误信息                             |
1361| -------- | -------------------------------------- |
1362| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1363| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1364
1365**示例:**
1366
1367```ts
1368import { BusinessError } from '@kit.BasicServicesKit';
1369
1370inputMethod.getController().detach((err: BusinessError) => {
1371  if (err) {
1372    console.error(`Failed to detach, code: ${err.code}, message: ${err.message}`);
1373    return;
1374  }
1375  console.info('Succeeded in detaching inputMethod.');
1376});
1377```
1378
1379### detach<sup>10+</sup>
1380
1381detach(): Promise&lt;void&gt;
1382
1383自绘控件解除与输入法的绑定。使用promise异步回调。
1384
1385**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1386
1387**返回值:**
1388
1389| 类型 | 说明 |
1390| -------- | -------- |
1391| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1392
1393**错误码:**
1394
1395以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
1396
1397| 错误码ID | 错误信息                             |
1398| -------- | -------------------------------------- |
1399| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1400| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1401
1402**示例:**
1403
1404```ts
1405import { BusinessError } from '@kit.BasicServicesKit';
1406
1407inputMethod.getController().detach().then(() => {
1408  console.info('Succeeded in detaching inputMethod.');
1409}).catch((err: BusinessError) => {
1410  console.error(`Failed to detach, code: ${err.code}, message: ${err.message}`);
1411});
1412```
1413
1414### setCallingWindow<sup>10+</sup>
1415
1416setCallingWindow(windowId: number, callback: AsyncCallback&lt;void&gt;): void
1417
1418设置要避让软键盘的窗口。使用callback异步回调。
1419
1420> **说明:**
1421>
1422> 将绑定到输入法的应用程序所在的窗口Id传入,此窗口可以避让输入法窗口。
1423
1424**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1425
1426**参数:**
1427
1428| 参数名 | 类型 | 必填 | 说明 |
1429| -------- | -------- | -------- | -------- |
1430| windowId | number | 是 | 绑定输入法应用的应用程序所在的窗口Id。该参数应为整数。|
1431| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当设置成功时,err为undefined;否则为错误对象。 |
1432
1433**错误码:**
1434
1435以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
1436
1437| 错误码ID | 错误信息                             |
1438| -------- | -------------------------------------- |
1439| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1440| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1441| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1442| 12800009 | input method client detached.             |
1443
1444**示例:**
1445
1446```ts
1447import { BusinessError } from '@kit.BasicServicesKit';
1448
1449let windowId: number = 2000;
1450inputMethod.getController().setCallingWindow(windowId, (err: BusinessError) => {
1451  if (err) {
1452    console.error(`Failed to setCallingWindow, code: ${err.code}, message: ${err.message}`);
1453    return;
1454  }
1455  console.info('Succeeded in setting callingWindow.');
1456});
1457```
1458
1459### setCallingWindow<sup>10+</sup>
1460
1461setCallingWindow(windowId: number): Promise&lt;void&gt;
1462
1463设置要避让软键盘的窗口。使用promise异步回调。
1464
1465> **说明:**
1466>
1467> 将绑定到输入法的应用程序所在的窗口Id传入,此窗口可以避让输入法窗口。
1468
1469**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1470
1471**参数:**
1472
1473| 参数名 | 类型 | 必填 | 说明 |
1474| -------- | -------- | -------- | -------- |
1475| windowId | number | 是 | 绑定输入法应用的应用程序所在的窗口Id。该参数应为整数。 |
1476
1477**返回值:**
1478
1479| 类型 | 说明 |
1480| -------- | -------- |
1481| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1482
1483**错误码:**
1484
1485以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
1486
1487| 错误码ID | 错误信息                             |
1488| -------- | -------------------------------------- |
1489| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1490| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1491| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1492| 12800009 | input method client detached. |
1493
1494**示例:**
1495
1496```ts
1497import { BusinessError } from '@kit.BasicServicesKit';
1498
1499let windowId: number = 2000;
1500inputMethod.getController().setCallingWindow(windowId).then(() => {
1501  console.info('Succeeded in setting callingWindow.');
1502}).catch((err: BusinessError) => {
1503  console.error(`Failed to setCallingWindow, code: ${err.code}, message: ${err.message}`);
1504})
1505```
1506
1507### updateCursor<sup>10+</sup>
1508
1509updateCursor(cursorInfo: CursorInfo, callback: AsyncCallback&lt;void&gt;): void
1510
1511当编辑框内的光标信息发生变化时,调用该接口使输入法感知到光标变化。使用callback异步回调。
1512
1513**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1514
1515**参数:**
1516
1517| 参数名 | 类型 | 必填 | 说明 |
1518| -------- | -------- | -------- | -------- |
1519| cursorInfo | [CursorInfo](#cursorinfo10) | 是 | 光标信息。 |
1520| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当光标信息更新成功时,err为undefined;否则为错误对象。 |
1521
1522**错误码:**
1523
1524以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
1525
1526| 错误码ID | 错误信息                             |
1527| -------- | -------------------------------------- |
1528| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
1529| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1530| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1531| 12800009 | input method client detached.             |
1532
1533**示例:**
1534
1535```ts
1536import { BusinessError } from '@kit.BasicServicesKit';
1537
1538let cursorInfo: inputMethod.CursorInfo = {
1539  left: 0,
1540  top: 0,
1541  width: 600,
1542  height: 800
1543};
1544inputMethod.getController().updateCursor(cursorInfo, (err: BusinessError) => {
1545  if (err) {
1546    console.error(`Failed to updateCursor, code: ${err.code}, message: ${err.message}`);
1547    return;
1548  }
1549  console.info('Succeeded in updating cursorInfo.');
1550});
1551
1552```
1553
1554### updateCursor<sup>10+</sup>
1555
1556updateCursor(cursorInfo: CursorInfo): Promise&lt;void&gt;
1557
1558当编辑框内的光标信息发生变化时,调用该接口使输入法感知到光标变化。使用promise异步回调。
1559
1560**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1561
1562**参数:**
1563
1564| 参数名 | 类型 | 必填 | 说明 |
1565| -------- | -------- | -------- | -------- |
1566| cursorInfo | [CursorInfo](#cursorinfo10) | 是 | 光标信息。 |
1567
1568**返回值:**
1569
1570| 类型 | 说明 |
1571| -------- | -------- |
1572| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1573
1574**错误码:**
1575
1576以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
1577
1578| 错误码ID | 错误信息                             |
1579| -------- | -------------------------------------- |
1580| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
1581| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1582| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1583| 12800009 | input method client detached. |
1584
1585**示例:**
1586
1587```ts
1588import { BusinessError } from '@kit.BasicServicesKit';
1589
1590let cursorInfo: inputMethod.CursorInfo = {
1591  left: 0,
1592  top: 0,
1593  width: 600,
1594  height: 800
1595};
1596inputMethod.getController().updateCursor(cursorInfo).then(() => {
1597  console.info('Succeeded in updating cursorInfo.');
1598}).catch((err: BusinessError) => {
1599  console.error(`Failed to updateCursor, code: ${err.code}, message: ${err.message}`);
1600});
1601```
1602
1603### changeSelection<sup>10+</sup>
1604
1605changeSelection(text: string, start: number, end: number, callback: AsyncCallback&lt;void&gt;): void
1606
1607当编辑框内被选中的文本信息内容或文本范围发生变化时,可调用该接口更新文本信息,使输入法应用感知到变化。使用callback异步回调。
1608
1609**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1610
1611**参数:**
1612
1613| 参数名 | 类型 | 必填 | 说明 |
1614| -------- | -------- | -------- | -------- |
1615| text | string | 是 | 整个输入文本。 |
1616| start | number | 是 | 所选文本的起始位置。该参数应为大于或等于0的整数。 |
1617| end | number | 是 | 所选文本的结束位置。该参数应为大于或等于0的整数。 |
1618| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当文本信息更新成功时,err为undefined;否则为错误对象。 |
1619
1620**错误码:**
1621
1622以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
1623
1624| 错误码ID | 错误信息                             |
1625| -------- | -------------------------------------- |
1626| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1627| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1628| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1629| 12800009 | input method client detached.             |
1630
1631**示例:**
1632
1633```ts
1634import { BusinessError } from '@kit.BasicServicesKit';
1635
1636inputMethod.getController().changeSelection('text', 0, 5, (err: BusinessError) => {
1637  if (err) {
1638    console.error(`Failed to changeSelection, code: ${err.code}, message: ${err.message}`);
1639    return;
1640  }
1641  console.info('Succeeded in changing selection.');
1642});
1643```
1644
1645### changeSelection<sup>10+</sup>
1646
1647changeSelection(text: string, start: number, end: number): Promise&lt;void&gt;
1648
1649当编辑框内被选中的文本信息内容或文本范围发生变化时,可调用该接口更新文本信息,使输入法应用感知到变化。使用promise异步回调。
1650
1651**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1652
1653**参数:**
1654
1655| 参数名 | 类型 | 必填 | 说明 |
1656| -------- | -------- | -------- | -------- |
1657| text | string | 是 | 整个输入文本。 |
1658| start | number | 是 | 所选文本的起始位置。该参数应为大于或等于0的整数。 |
1659| end | number | 是 | 所选文本的结束位置。该参数应为大于或等于0的整数。 |
1660
1661**返回值:**
1662
1663| 类型 | 说明 |
1664| -------- | -------- |
1665| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1666
1667**错误码:**
1668
1669以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
1670
1671| 错误码ID | 错误信息                             |
1672| -------- | -------------------------------------- |
1673| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1674| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1675| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1676| 12800009 | input method client detached. |
1677
1678**示例:**
1679
1680```ts
1681import { BusinessError } from '@kit.BasicServicesKit';
1682
1683inputMethod.getController().changeSelection('test', 0, 5).then(() => {
1684  console.info('Succeeded in changing selection.');
1685}).catch((err: BusinessError) => {
1686  console.error(`Failed to changeSelection, code: ${err.code}, message: ${err.message}`);
1687});
1688```
1689
1690### updateAttribute<sup>10+</sup>
1691
1692updateAttribute(attribute: InputAttribute, callback: AsyncCallback&lt;void&gt;): void
1693
1694更新编辑框属性信息。使用callback异步回调。
1695
1696**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1697
1698**参数:**
1699
1700| 参数名 | 类型 | 必填 | 说明 |
1701| -------- | -------- | -------- | -------- |
1702| attribute | [InputAttribute](#inputattribute10) | 是 | 编辑框属性对象。 |
1703| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当编辑框属性信息更新成功时,err为undefined;否则为错误对象。 |
1704
1705**错误码:**
1706
1707以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
1708
1709| 错误码ID | 错误信息                             |
1710| -------- | -------------------------------------- |
1711| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1712| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1713| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1714| 12800009 | input method client detached.             |
1715
1716**示例:**
1717
1718```ts
1719import { BusinessError } from '@kit.BasicServicesKit';
1720
1721let inputAttribute: inputMethod.InputAttribute = { textInputType: 0, enterKeyType: 1 };
1722inputMethod.getController().updateAttribute(inputAttribute, (err: BusinessError) => {
1723  if (err) {
1724    console.error(`Failed to updateAttribute, code: ${err.code}, message: ${err.message}`);
1725    return;
1726  }
1727  console.info('Succeeded in updating attribute.');
1728});
1729```
1730
1731### updateAttribute<sup>10+</sup>
1732
1733updateAttribute(attribute: InputAttribute): Promise&lt;void&gt;
1734
1735更新编辑框属性信息。使用promise异步回调。
1736
1737**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1738
1739**参数:**
1740
1741| 参数名 | 类型 | 必填 | 说明 |
1742| -------- | -------- | -------- | -------- |
1743| attribute | [InputAttribute](#inputattribute10) | 是 |  编辑框属性对象。 |
1744
1745**返回值:**
1746
1747| 类型 | 说明 |
1748| -------- | -------- |
1749| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1750
1751**错误码:**
1752
1753以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
1754
1755| 错误码ID | 错误信息                             |
1756| -------- | -------------------------------------- |
1757| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1758| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1759| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1760| 12800009 | input method client detached. |
1761
1762**示例:**
1763
1764```ts
1765import { BusinessError } from '@kit.BasicServicesKit';
1766
1767let inputAttribute: inputMethod.InputAttribute = { textInputType: 0, enterKeyType: 1 };
1768inputMethod.getController().updateAttribute(inputAttribute).then(() => {
1769  console.info('Succeeded in updating attribute.');
1770}).catch((err: BusinessError) => {
1771  console.error(`Failed to updateAttribute, code: ${err.code}, message: ${err.message}`);
1772});
1773```
1774
1775### stopInputSession<sup>9+</sup>
1776
1777stopInputSession(callback: AsyncCallback&lt;boolean&gt;): void
1778
1779结束输入会话。使用callback异步回调。
1780
1781> **说明:**
1782>
1783> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用该接口结束输入会话。
1784
1785**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1786
1787**参数:**
1788
1789| 参数名 | 类型 | 必填 | 说明 |
1790| -------- | -------- | -------- | -------- |
1791| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数。当结束输入会话成功时,err为undefined,data为true;否则为错误对象。 |
1792
1793**错误码:**
1794
1795以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
1796
1797| 错误码ID | 错误信息                             |
1798| -------- | -------------------------------------- |
1799| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1800| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1801
1802**示例:**
1803
1804```ts
1805import { BusinessError } from '@kit.BasicServicesKit';
1806
1807inputMethod.getController().stopInputSession((err: BusinessError, result: boolean) => {
1808  if (err) {
1809    console.error(`Failed to stopInputSession, code: ${err.code}, message: ${err.message}`);
1810    return;
1811  }
1812  if (result) {
1813    console.info('Succeeded in stopping inputSession.');
1814  } else {
1815    console.error('Failed to stopInputSession.');
1816  }
1817});
1818```
1819
1820### stopInputSession<sup>9+</sup>
1821
1822stopInputSession(): Promise&lt;boolean&gt;
1823
1824结束输入会话。使用promise异步回调。
1825
1826> **说明:**
1827>
1828> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用该接口结束输入会话。
1829
1830**系统能力:** SystemCapability.MiscServices.InputMethodFramework
1831
1832**返回值:**
1833
1834| 类型 | 说明 |
1835| -------- | -------- |
1836| Promise&lt;boolean&gt; | Promise对象。返回true表示结束输入会话成功,返回false表示结束输入会话失败。 |
1837
1838**错误码:**
1839
1840以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
1841
1842| 错误码ID | 错误信息                             |
1843| -------- | -------------------------------------- |
1844| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1845| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1846
1847**示例:**
1848
1849```ts
1850import { BusinessError } from '@kit.BasicServicesKit';
1851
1852inputMethod.getController().stopInputSession().then((result: boolean) => {
1853  if (result) {
1854    console.info('Succeeded in stopping inputSession.');
1855  } else {
1856    console.error('Failed to stopInputSession.');
1857  }
1858}).catch((err: BusinessError) => {
1859  console.error(`Failed to stopInputSession, code: ${err.code}, message: ${err.message}`);
1860});
1861```
1862
1863### showSoftKeyboard<sup>9+</sup>
1864
1865showSoftKeyboard(callback: AsyncCallback&lt;void&gt;): void
1866
1867显示输入法软键盘。使用callback异步回调。
1868
1869> **说明:**
1870>
1871> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用显示当前输入法的软键盘。
1872
1873**需要权限:** ohos.permission.CONNECT_IME_ABILITY,仅系统应用可用。
1874
1875**系统能力:**  SystemCapability.MiscServices.InputMethodFramework
1876
1877**参数:**
1878
1879| 参数名   | 类型                  | 必填 | 说明       |
1880| -------- | ------------------------- | ---- | ---------- |
1881| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当软键盘显示成功。err为undefined,否则为错误对象。 |
1882
1883**错误码:**
1884
1885以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
1886
1887| 错误码ID | 错误信息                             |
1888| -------- | -------------------------------------- |
1889| 201      | permissions check fails.  |
1890| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1891| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1892
1893**示例:**
1894
1895```ts
1896import { BusinessError } from '@kit.BasicServicesKit';
1897
1898inputMethod.getController().showSoftKeyboard((err: BusinessError) => {
1899  if (!err) {
1900    console.info('Succeeded in showing softKeyboard.');
1901  } else {
1902    console.error(`Failed to show softKeyboard, ${err.code}, message: ${err.message}`);
1903  }
1904});
1905```
1906
1907### showSoftKeyboard<sup>9+</sup>
1908
1909showSoftKeyboard(): Promise&lt;void&gt;
1910
1911显示输入法软键盘。使用Promise异步回调。
1912
1913> **说明:**
1914>
1915> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用显示当前输入法的软键盘。
1916
1917**需要权限:** ohos.permission.CONNECT_IME_ABILITY,仅系统应用可用。
1918
1919**系统能力:**  SystemCapability.MiscServices.InputMethodFramework
1920
1921**返回值:**
1922
1923| 类型                | 说明                      |
1924| ------------------- | ------------------------- |
1925| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
1926
1927**错误码:**
1928
1929以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
1930
1931| 错误码ID | 错误信息                             |
1932| -------- | -------------------------------------- |
1933| 201      | permissions check fails.  |
1934| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1935| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1936
1937**示例:**
1938
1939```ts
1940import { BusinessError } from '@kit.BasicServicesKit';
1941
1942inputMethod.getController().showSoftKeyboard().then(() => {
1943  console.info('Succeeded in showing softKeyboard.');
1944}).catch((err: BusinessError) => {
1945  console.error(`Failed to show softKeyboard, code: ${err.code}, message: ${err.message}`);
1946});
1947```
1948
1949### hideSoftKeyboard<sup>9+</sup>
1950
1951hideSoftKeyboard(callback: AsyncCallback&lt;void&gt;): void
1952
1953隐藏输入法软键盘。使用callback异步回调。
1954
1955> **说明:**
1956>
1957> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用隐藏当前输入法的软键盘。
1958
1959**需要权限:** ohos.permission.CONNECT_IME_ABILITY,仅系统应用可用。
1960
1961**系统能力:**  SystemCapability.MiscServices.InputMethodFramework
1962
1963**参数:**
1964
1965| 参数名   | 类型                  | 必填 | 说明       |
1966| -------- | ------------------------- | ---- | ---------- |
1967| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当软键盘隐藏成功。err为undefined,否则为错误对象。 |
1968
1969**错误码:**
1970
1971以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
1972
1973| 错误码ID | 错误信息                             |
1974| -------- | -------------------------------------- |
1975| 201      | permissions check fails.  |
1976| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
1977| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
1978
1979**示例:**
1980
1981```ts
1982import { BusinessError } from '@kit.BasicServicesKit';
1983
1984inputMethod.getController().hideSoftKeyboard((err: BusinessError) => {
1985  if (!err) {
1986    console.info('Succeeded in hiding softKeyboard.');
1987  } else {
1988    console.error(`Failed to hide softKeyboard, code: ${err.code}, message: ${err.message}`);
1989  }
1990})
1991```
1992
1993### hideSoftKeyboard<sup>9+</sup>
1994
1995hideSoftKeyboard(): Promise&lt;void&gt;
1996
1997隐藏输入法软键盘。使用Promise异步回调。
1998
1999> **说明:**
2000>
2001> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用隐藏当前输入法的软键盘。
2002
2003**需要权限:** ohos.permission.CONNECT_IME_ABILITY,仅系统应用可用。
2004
2005**系统能力:**  SystemCapability.MiscServices.InputMethodFramework
2006
2007**返回值:**
2008
2009| 类型                | 说明                      |
2010| ------------------- | ------------------------- |
2011| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
2012
2013**错误码:**
2014
2015以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2016
2017| 错误码ID | 错误信息                             |
2018| -------- | -------------------------------------- |
2019| 201      | permissions check fails.  |
2020| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
2021| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
2022
2023**示例:**
2024
2025```ts
2026import { BusinessError } from '@kit.BasicServicesKit';
2027
2028inputMethod.getController().hideSoftKeyboard().then(() => {
2029  console.info('Succeeded in hiding softKeyboard.');
2030}).catch((err: BusinessError) => {
2031  console.error(`Failed to hide softKeyboard, code: ${err.code}, message: ${err.message}`);
2032});
2033```
2034
2035### sendMessage<sup>15+</sup>
2036
2037sendMessage(msgId: string, msgParam?: ArrayBuffer): Promise<void&gt;
2038
2039发送自定义通信至输入法应用。使用Promise异步回调。
2040
2041> **说明:**
2042>
2043> 该接口需要编辑框与输入法绑定并进入编辑状态,且输入法应用处于完整体验模式时才能调用。
2044>
2045> msgId最大限制256B,msgParam最大限制128KB。
2046
2047**系统能力:**  SystemCapability.MiscServices.InputMethodFramework
2048
2049**参数:**
2050
2051| 参数名   | 类型        | 必填 | 说明                                       |
2052| -------- | ----------- | ---- | ------------------------------------------ |
2053| msgId    | string      | 是   | 需要发送至输入法应用的自定义数据的标识符。 |
2054| msgParam | ArrayBuffer | 否   | 需要发送至输入法应用的自定义数据的消息体。 |
2055
2056**返回值:**
2057
2058| 类型                | 说明                      |
2059| ------------------- | ------------------------- |
2060| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
2061
2062**错误码:**
2063
2064以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2065
2066| 错误码ID | 错误信息                                    |
2067| -------- | ------------------------------------------- |
2068| 401      | parameter error. Possible causes: 1. Incorrect parameter types. 2. Incorrect parameter length.  |
2069| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. |
2070| 12800009 | input method client detached.               |
2071| 12800014 | the input method is in basic mode.          |
2072| 12800015 | the other side does not accept the request. |
2073| 12800016 | input method client is not editable.        |
2074
2075**示例:**
2076
2077```ts
2078import { BusinessError } from '@kit.BasicServicesKit';
2079
2080let msgId: string = "testMsgId";
2081let msgParam: ArrayBuffer = new ArrayBuffer(128);
2082inputMethod.getController().sendMessage(msgId, msgParam).then(() => {
2083  console.info('Succeeded send message.');
2084}).catch((err: BusinessError) => {
2085  console.error(`Failed to send message, code: ${err.code}, message: ${err.message}`);
2086});
2087```
2088
2089### recvMessage<sup>15+</sup>
2090
2091recvMessage(msgHandler?: MessageHandler): void
2092
2093注册或取消注册MessageHandler。
2094
2095> **说明:**
2096>
2097> [MessageHandler](#messagehandler15)对象全局唯一,多次注册仅保留最后一次注册的对象及有效性,并触发上一个已注册对象的[onTerminated](#onterminated15)回调函数。
2098>
2099> 未填写参数,则取消全局已注册的[MessageHandler](#messagehandler15),并触发被取消注册对象中[onTerminated](#onterminated15)回调函数。
2100
2101**系统能力:**  SystemCapability.MiscServices.InputMethodFramework
2102
2103**参数:**
2104
2105| 参数名     | 类型                                | 必填 | 说明                                                         |
2106| ---------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
2107| msgHandler | [MessageHandler](#messagehandler15) | 否   | 该对象通过[onMessage](#onmessage15)接收来自输入法应用所发送的自定义通信数据,并通过[onTerminated](#onterminated15)接收终止此对象订阅的消息。<br>若不填写此参数,则取消全局已注册的[MessageHandler](#messagehandler15)对象,同时触发其[onTerminated](#onterminated15)回调函数。 |
2108
2109**错误码:**
2110
2111以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2112
2113| 错误码ID | 错误信息         |
2114| -------- | ---------------- |
2115| 401      | parameter error. Possible causes: 1. Incorrect parameter types. |
2116
2117**示例:**
2118
2119```ts
2120import { BusinessError } from '@kit.BasicServicesKit';
2121
2122let messageHandler: inputMethod.MessageHandler = {
2123  onTerminated(): void {
2124    console.info('OnTerminated.');
2125  },
2126  onMessage(msgId: string, msgParam?: ArrayBuffer): void {
2127    console.info('recv message.');
2128  }
2129};
2130let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2131inputMethodController.recvMessage(messageHandler);
2132// 取消已注册的MessageHandler
2133inputMethodController.recvMessage();
2134```
2135
2136### stopInput<sup>(deprecated)</sup>
2137
2138stopInput(callback: AsyncCallback&lt;boolean&gt;): void
2139
2140结束输入会话。使用callback异步回调。
2141
2142> **说明:**
2143>
2144> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用该接口结束输入会话。
2145>
2146> 从API version 6开始支持,从API version 9开始废弃,建议使用[stopInputSession()](#stopinputsession9)替代。
2147
2148**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2149
2150**参数:**
2151
2152| 参数名 | 类型 | 必填 | 说明 |
2153| -------- | -------- | -------- | -------- |
2154| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数。当会话结束成功,err为undefined,data为true;否则为错误对象。 |
2155
2156**示例:**
2157
2158```ts
2159import { BusinessError } from '@kit.BasicServicesKit';
2160
2161inputMethod.getController().stopInput((err: BusinessError, result: boolean) => {
2162  if (err) {
2163    console.error(`Failed to stopInput, code: ${err.code}, message: ${err.message}`);
2164    return;
2165  }
2166  if (result) {
2167    console.info('Succeeded in stopping input.');
2168  } else {
2169    console.error('Failed to stopInput.');
2170  }
2171});
2172```
2173
2174### stopInput<sup>(deprecated)</sup>
2175
2176stopInput(): Promise&lt;boolean&gt;
2177
2178结束输入会话。使用promise异步回调。
2179
2180> **说明:**
2181>
2182> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用该接口结束输入会话。
2183>
2184> 从API version 6开始支持,从API version 9开始废弃,建议使用[stopInputSession()](#stopinputsession9)替代。
2185
2186**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2187
2188**返回值:**
2189
2190| 类型 | 说明 |
2191| -------- | -------- |
2192| Promise&lt;boolean&gt; | Promise对象。返回true表示会话结束成功;返回false表示会话结束失败。 |
2193
2194**示例:**
2195
2196```ts
2197import { BusinessError } from '@kit.BasicServicesKit';
2198
2199inputMethod.getController().stopInput().then((result: boolean) => {
2200  if (result) {
2201    console.info('Succeeded in stopping input.');
2202  } else {
2203    console.error('Failed to stopInput.');
2204  }
2205}).catch((err: BusinessError) => {
2206  console.error(`Failed to stopInput, code: ${err.code}, message: ${err.message}`);
2207})
2208```
2209
2210### on('insertText')<sup>10+</sup>
2211
2212on(type: 'insertText', callback: (text: string) => void): void
2213
2214订阅输入法应用插入文本事件。使用callback异步回调。
2215
2216**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2217
2218**参数:**
2219
2220| 参数名   | 类型                                                         | 必填 | 说明                                                         |
2221| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
2222| type     | string                                                       | 是   | 设置监听类型,固定取值为'insertText'。 |
2223| callback | (text: string) => void | 是   | 回调函数,返回需要插入的文本内容。<br/>根据传入的文本,在回调函数中操作编辑框中的内容。 |
2224
2225**错误码:**
2226
2227以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2228
2229| 错误码ID | 错误信息                             |
2230| -------- | -------------------------------------- |
2231| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2232| 12800009 | input method client detached. |
2233
2234**示例:**
2235
2236```ts
2237function callback1(text: string): void {
2238  console.info(`Succeeded in getting callback1, data: ${text}`);
2239}
2240
2241function callback2(text: string): void {
2242  console.info(`Succeeded in getting callback2, data: ${text}`);
2243}
2244
2245let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2246// 注册回调
2247inputMethodController.on('insertText', callback1);
2248inputMethodController.on('insertText', callback2);
2249// 仅取消insertText的callback1的回调
2250inputMethodController.off('insertText', callback1);
2251// 取消insertText的所有回调
2252inputMethodController.off('insertText');
2253```
2254
2255### off('insertText')<sup>10+</sup>
2256
2257off(type: 'insertText', callback?: (text: string) => void): void
2258
2259取消订阅输入法应用插入文本事件。
2260
2261**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2262
2263**参数:**
2264
2265| 参数名   | 类型                   | 必填 | 说明                                                         |
2266| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
2267| type     | string                 | 是   | 设置监听类型,固定取值为'insertText'。 |
2268| callback | (text: string) => void | 否   | 取消订阅的回调函数,需要与on接口传入的保持一致。<br/>参数不填写时,取消订阅type对应的所有回调事件。 |
2269
2270**示例:**
2271
2272```ts
2273import { Callback } from '@kit.BasicServicesKit';
2274
2275let onInsertTextCallback: Callback<string> = (text: string): void => {
2276  console.info(`Succeeded in subscribing insertText: ${text}`);
2277};
2278
2279let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2280inputMethodController.off('insertText', onInsertTextCallback);
2281inputMethodController.off('insertText');
2282```
2283
2284### on('deleteLeft')<sup>10+</sup>
2285
2286on(type: 'deleteLeft', callback: (length: number) => void): void
2287
2288订阅输入法应用向左删除文本事件。使用callback异步回调。
2289
2290**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2291
2292**参数:**
2293
2294| 参数名   | 类型 | 必填 | 说明 |
2295| -------- | ----- | ---- | ----- |
2296| type     | string  | 是   | 设置监听类型,固定取值为'deleteLeft'。|
2297| callback | (length: number) => void | 是   | 回调函数,返回需要向左删除的文本长度。<br/>根据传入的删除长度,在回调函数中操作编辑框中的文本。 |
2298
2299**错误码:**
2300
2301以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2302
2303| 错误码ID | 错误信息                             |
2304| -------- | -------------------------------------- |
2305| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2306| 12800009 | input method client detached. |
2307
2308**示例:**
2309
2310```ts
2311inputMethod.getController().on('deleteLeft', (length: number) => {
2312  console.info(`Succeeded in subscribing deleteLeft, length: ${length}`);
2313});
2314```
2315
2316### off('deleteLeft')<sup>10+</sup>
2317
2318off(type: 'deleteLeft', callback?: (length: number) => void): void
2319
2320取消订阅输入法应用向左删除文本事件。
2321
2322**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2323
2324**参数:**
2325
2326| 参数名   | 类型                     | 必填 | 说明                                                         |
2327| -------- | ------------------------ | ---- | ------------------------------------------------------------ |
2328| type     | string                   | 是   | 设置监听,固定取值为'deleteLeft'。 |
2329| callback | (length: number) => void | 否   | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 |
2330
2331**示例:**
2332
2333```ts
2334import { Callback } from '@kit.BasicServicesKit';
2335
2336let onDeleteLeftCallback: Callback<number> = (length: number): void => {
2337  console.info(`Succeeded in subscribing deleteLeft, length: ${length}`);
2338};
2339
2340let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2341inputMethodController.off('deleteLeft', onDeleteLeftCallback);
2342inputMethodController.off('deleteLeft');
2343```
2344
2345### on('deleteRight')<sup>10+</sup>
2346
2347on(type: 'deleteRight', callback: (length: number) => void): void
2348
2349订阅输入法应用向右删除文本事件。使用callback异步回调。
2350
2351**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2352
2353**参数:**
2354
2355| 参数名   | 类型 | 必填 | 说明 |
2356| -------- | ----- | ---- | ----- |
2357| type     | string  | 是   | 设置监听类型,固定取值为'deleteRight'。|
2358| callback | (length: number) => void | 是   | 回调函数,返回需要向右删除的文本长度。<br/>根据传入的删除长度,在回调函数中操作编辑框中的文本。 |
2359
2360**错误码:**
2361
2362以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2363
2364| 错误码ID | 错误信息                             |
2365| -------- | -------------------------------------- |
2366| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2367| 12800009 | input method client detached. |
2368
2369**示例:**
2370
2371```ts
2372inputMethod.getController().on('deleteRight', (length: number) => {
2373  console.info(`Succeeded in subscribing deleteRight, length: ${length}`);
2374});
2375```
2376
2377### off('deleteRight')<sup>10+</sup>
2378
2379off(type: 'deleteRight', callback?: (length: number) => void): void
2380
2381取消订阅输入法应用向右删除文本事件。
2382
2383**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2384
2385**参数:**
2386
2387| 参数名   | 类型                     | 必填 | 说明                                                         |
2388| -------- | ------------------------ | ---- | ------------------------------------------------------------ |
2389| type     | string                   | 是   | 设置监听类型,固定取值为`deleteRight`。 |
2390| callback | (length: number) => void | 否   | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 |
2391
2392**示例:**
2393
2394```ts
2395import { Callback } from '@kit.BasicServicesKit';
2396
2397let onDeleteRightCallback: Callback<number> = (length: number): void => {
2398  console.info(`Succeeded in subscribing deleteRight, length: ${length}`);
2399};
2400let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2401inputMethodController.off('deleteRight', onDeleteRightCallback);
2402inputMethodController.off('deleteRight');
2403```
2404
2405### on('sendKeyboardStatus')<sup>10+</sup>
2406
2407on(type: 'sendKeyboardStatus', callback: (keyboardStatus: KeyboardStatus) => void): void
2408
2409订阅输入法应用发送输入法软键盘状态事件。使用callback异步回调。
2410
2411**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2412
2413**参数:**
2414
2415| 参数名   | 类型  | 必填 | 说明    |
2416| -------- | ------ | ---- | ---- |
2417| type     | string  | 是   | 设置监听类型,固定取值为'sendKeyboardStatus'。 |
2418| callback | (keyboardStatus: [KeyboardStatus](#keyboardstatus10)) => void | 是   | 回调函数,返回软键盘状态。<br/>根据传入的软键盘状态,在回调函数中做相应操作。 |
2419
2420**错误码:**
2421
2422以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2423
2424| 错误码ID | 错误信息                             |
2425| -------- | -------------------------------------- |
2426| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2427| 12800009 | input method client detached. |
2428
2429**示例:**
2430
2431```ts
2432inputMethod.getController().on('sendKeyboardStatus', (keyboardStatus: inputMethod.KeyboardStatus) => {
2433  console.info(`Succeeded in subscribing sendKeyboardStatus, keyboardStatus: ${keyboardStatus}`);
2434});
2435```
2436
2437### off('sendKeyboardStatus')<sup>10+</sup>
2438
2439off(type: 'sendKeyboardStatus', callback?: (keyboardStatus: KeyboardStatus) => void): void
2440
2441取消订阅输入法应用发送输入法软键盘状态事件。
2442
2443**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2444
2445**参数:**
2446
2447| 参数名   | 类型                                                         | 必填 | 说明                                                         |
2448| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
2449| type     | string                                                       | 是   | 设置监听类型,固定取值为'sendKeyboardStatus'。 |
2450| callback | (keyboardStatus: [KeyboardStatus](#keyboardstatus10)) => void | 否   | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 |
2451
2452**示例:**
2453
2454```ts
2455import { Callback } from '@kit.BasicServicesKit';
2456
2457let onSendKeyboardStatus: Callback<inputMethod.KeyboardStatus> = (keyboardStatus: inputMethod.KeyboardStatus): void => {
2458  console.info(`Succeeded in subscribing sendKeyboardStatus, keyboardStatus: ${keyboardStatus}`);
2459};
2460
2461let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2462inputMethodController.off('sendKeyboardStatus', onSendKeyboardStatus);
2463inputMethodController.off('sendKeyboardStatus');
2464```
2465
2466### on('sendFunctionKey')<sup>10+</sup>
2467
2468on(type: 'sendFunctionKey', callback: (functionKey: FunctionKey) => void): void
2469
2470订阅输入法应用发送功能键事件。使用callback异步回调。
2471
2472**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2473
2474**参数:**
2475
2476| 参数名   | 类型  | 必填 | 说明     |
2477| -------- | -------- | ---- | ----- |
2478| type     | string  | 是   | 设置监听类型,固定取值为'sendFunctionKey'。|
2479| callback | (functionKey: [FunctionKey](#functionkey10)) => void | 是   | 回调函数,返回输入法应用发送的功能键信息。<br/>根据返回的功能键信息,做相应操作。 |
2480
2481**错误码:**
2482
2483以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2484
2485| 错误码ID | 错误信息                             |
2486| -------- | -------------------------------------- |
2487| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2488| 12800009 | input method client detached. |
2489
2490**示例:**
2491
2492```ts
2493inputMethod.getController().on('sendFunctionKey', (functionKey: inputMethod.FunctionKey) => {
2494  console.info(`Succeeded in subscribing sendFunctionKey, functionKey.enterKeyType: ${functionKey.enterKeyType}`);
2495});
2496```
2497
2498### off('sendFunctionKey')<sup>10+</sup>
2499
2500off(type: 'sendFunctionKey', callback?: (functionKey: FunctionKey) => void): void
2501
2502取消订阅输入法应用发送功能键事件。
2503
2504**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2505
2506**参数:**
2507
2508| 参数名   | 类型                                                 | 必填 | 说明                                                         |
2509| -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
2510| type     | string                                               | 是   | 设置监听类型,固定取值为'sendFunctionKey'。 |
2511| callback | (functionKey: [FunctionKey](#functionkey10)) => void | 否   | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 |
2512
2513**示例:**
2514
2515```ts
2516import { Callback } from '@kit.BasicServicesKit';
2517
2518let onSendFunctionKey: Callback<inputMethod.FunctionKey> = (functionKey: inputMethod.FunctionKey): void => {
2519  console.info(`Succeeded in subscribing sendFunctionKey, functionKey: ${functionKey.enterKeyType}`);
2520};
2521
2522let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2523inputMethodController.off('sendFunctionKey', onSendFunctionKey);
2524inputMethodController.off('sendFunctionKey');
2525```
2526
2527### on('moveCursor')<sup>10+</sup>
2528
2529on(type: 'moveCursor', callback: (direction: Direction) => void): void
2530
2531订阅输入法应用移动光标事件。使用callback异步回调。
2532
2533**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2534
2535**参数:**
2536
2537| 参数名   | 类型 | 必填 | 说明   |
2538| -------- | ------ | ---- | ------ |
2539| type     | string | 是   | 设置监听类型,固定取值为'moveCursor'。 |
2540| callback | (direction: [Direction](#direction10)) => void | 是   | 回调函数,返回光标信息。<br/>根据返回的光标移动方向,改变光标位置,如光标向上或向下。  |
2541
2542**错误码:**
2543
2544以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2545
2546| 错误码ID | 错误信息                           |
2547| -------- | -------------------------------- |
2548| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2549| 12800009 | input method client detached. |
2550
2551**示例:**
2552
2553```ts
2554inputMethod.getController().on('moveCursor', (direction: inputMethod.Direction) => {
2555  console.info(`Succeeded in subscribing moveCursor, direction: ${direction}`);
2556});
2557```
2558
2559### off('moveCursor')<sup>10+</sup>
2560
2561off(type: 'moveCursor', callback?: (direction: Direction) => void): void
2562
2563取消订阅输入法应用移动光标事件。
2564
2565**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2566
2567**参数:**
2568
2569| 参数名  | 类型    | 必填 | 说明  |
2570| ------ | ------ | ---- | ---- |
2571| type   | string | 是   | 设置监听类型,固定取值为'moveCursor'。 |
2572| callback | (direction: [Direction<sup>10+</sup>](#direction10)) => void | 否 | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 |
2573
2574**示例:**
2575
2576```ts
2577import { Callback } from '@kit.BasicServicesKit';
2578
2579let onMoveCursorCallback: Callback<inputMethod.Direction> = (direction: inputMethod.Direction): void => {
2580  console.info(`Succeeded in subscribing moveCursor, direction: ${direction}`);
2581};
2582
2583let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2584inputMethodController.off('moveCursor', onMoveCursorCallback);
2585inputMethodController.off('moveCursor');
2586```
2587
2588### on('handleExtendAction')<sup>10+</sup>
2589
2590on(type: 'handleExtendAction', callback: (action: ExtendAction) => void): void
2591
2592订阅输入法应用发送扩展编辑操作事件。使用callback异步回调。
2593
2594**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2595
2596**参数:**
2597
2598| 参数名   | 类型  | 必填 | 说明   |
2599| -------- | ------ | ---- | -------- |
2600| type     | string    | 是   | 设置监听类型,固定取值为'handleExtendAction'。 |
2601| callback | (action: [ExtendAction](#extendaction10)) => void | 是   | 回调函数,返回扩展编辑操作类型。<br/>根据传入的扩展编辑操作类型,做相应的操作,如剪切、复制等。|
2602
2603**错误码:**
2604
2605以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2606
2607| 错误码ID | 错误信息                             |
2608| -------- | -------------------------------------- |
2609| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2610| 12800009 | input method client detached. |
2611
2612**示例:**
2613
2614```ts
2615inputMethod.getController().on('handleExtendAction', (action: inputMethod.ExtendAction) => {
2616  console.info(`Succeeded in subscribing handleExtendAction, action: ${action}`);
2617});
2618```
2619
2620### off('handleExtendAction')<sup>10+</sup>
2621
2622off(type: 'handleExtendAction', callback?: (action: ExtendAction) => void): void
2623
2624取消订阅输入法应用发送扩展编辑操作事件。使用callback异步回调。
2625
2626**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2627
2628**参数:**
2629
2630| 参数名 | 类型   | 必填 | 说明  |
2631| ------ | ------ | ---- | ------- |
2632| type   | string | 是   | 设置监听类型,固定取值为'handleExtendAction'。 |
2633| callback | (action: [ExtendAction](#extendaction10)) => void | 否 | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 |
2634
2635**示例:**
2636
2637```ts
2638import { Callback } from '@kit.BasicServicesKit';
2639
2640let onHandleExtendActionCallback: Callback<inputMethod.ExtendAction> = (action: inputMethod.ExtendAction): void => {
2641  console.info(`Succeeded in subscribing handleExtendAction, action: ${action}`);
2642};
2643
2644let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2645inputMethodController.off('handleExtendAction', onHandleExtendActionCallback);
2646inputMethodController.off('handleExtendAction');
2647```
2648
2649### on('selectByRange')<sup>10+</sup>
2650
2651on(type: 'selectByRange', callback: Callback&lt;Range&gt;): void
2652
2653订阅输入法应用按范围选中文本事件。使用callback异步回调。
2654
2655**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2656
2657**参数:**
2658
2659| 参数名   | 类型     | 必填 | 说明     |
2660| -------- | ---- | ---- | ------- |
2661| type     | string  | 是   | 设置监听类型,固定取值为'selectByRange'。 |
2662| callback | Callback&lt;[Range](#range10)&gt; | 是   | 回调函数,返回需要选中的文本范围。<br/>根据传入的文本范围,开发者在回调函数中编辑框中相应文本。|
2663
2664**错误码:**
2665
2666以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。
2667
2668| 错误码ID | 错误信息                                                |
2669| -------- | ------------------------------------------------------- |
2670| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2671
2672**示例:**
2673
2674```ts
2675inputMethod.getController().on('selectByRange', (range: inputMethod.Range) => {
2676  console.info(`Succeeded in subscribing selectByRange: start: ${range.start} , end: ${range.end}`);
2677});
2678```
2679
2680### off('selectByRange')<sup>10+</sup>
2681
2682off(type: 'selectByRange', callback?:  Callback&lt;Range&gt;): void
2683
2684取消订阅输入法应用按范围选中文本事件。使用callback异步回调。
2685
2686**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2687
2688**参数:**
2689
2690| 参数名   | 类型                              | 必填 | 说明                                                         |
2691| -------- | --------------------------------- | ---- | ------------------------------------------------------------ |
2692| type     | string                            | 是   | 设置监听类型,固定取值为'selectByRange'。 |
2693| callback | Callback&lt;[Range](#range10)&gt; | 否   | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 |
2694
2695**示例:**
2696
2697```ts
2698import { Callback } from '@kit.BasicServicesKit';
2699
2700let onSelectByRangeCallback: Callback<inputMethod.Range> = (range: inputMethod.Range): void => {
2701  console.info(`Succeeded in subscribing selectByRange, start: ${range.start} , end: ${range.end}`);
2702};
2703
2704let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2705inputMethodController.off('selectByRange', onSelectByRangeCallback);
2706inputMethodController.off('selectByRange');
2707```
2708
2709### on('selectByMovement')<sup>10+</sup>
2710
2711on(type: 'selectByMovement', callback: Callback&lt;Movement&gt;): void
2712
2713订阅输入法应用按光标移动方向,选中文本事件。使用callback异步回调。
2714
2715**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2716
2717**参数:**
2718
2719| 参数名   | 类型   | 必填 | 说明     |
2720| -------- | ----- | ---- | ------ |
2721| type     | string  | 是   | 设置监听类型,固定取值为'selectByMovement'。 |
2722| callback | Callback&lt;[Movement](#movement10)&gt; | 是   | 回调函数,返回光标移动的方向。<br/>根据传入的光标移动方向,选中编辑框中相应文本。 |
2723
2724**错误码:**
2725
2726以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。
2727
2728| 错误码ID | 错误信息                                                |
2729| -------- | ------------------------------------------------------- |
2730| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2731
2732**示例:**
2733
2734```ts
2735inputMethod.getController().on('selectByMovement', (movement: inputMethod.Movement) => {
2736  console.info('Succeeded in subscribing selectByMovement: direction: ' + movement.direction);
2737});
2738```
2739
2740### off('selectByMovement')<sup>10+</sup>
2741
2742off(type: 'selectByMovement', callback?: Callback&lt;Movement&gt;): void
2743
2744取消订阅输入法应用按光标移动方向,选中文本事件。使用callback异步回调。
2745
2746**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2747
2748**参数:**
2749
2750| 参数名   | 类型                                 | 必填 | 说明                                                         |
2751| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
2752| type     | string                               | 是   | 设置监听类型,固定取值为'selectByMovement'。 |
2753| callback | Callback&lt;[Movement](#movement10)> | 否   | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 |
2754
2755**示例:**
2756
2757```ts
2758import { Callback } from '@kit.BasicServicesKit';
2759
2760let onSelectByMovementCallback: Callback<inputMethod.Movement> = (movement: inputMethod.Movement): void => {
2761  console.info(`Succeeded in subscribing selectByMovement, movement.direction: ${movement.direction}`);
2762};
2763
2764let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2765inputMethodController.off('selectByMovement', onSelectByMovementCallback);
2766inputMethodController.off('selectByMovement');
2767```
2768
2769### on('getLeftTextOfCursor')<sup>10+</sup>
2770
2771on(type: 'getLeftTextOfCursor', callback: (length: number) => string): void
2772
2773订阅输入法应用获取光标左侧指定长度文本事件。使用callback异步回调。
2774
2775**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2776
2777**参数:**
2778
2779| 参数名   | 类型   | 必填 | 说明     |
2780| -------- | ----- | ---- | ------ |
2781| type     | string  | 是   | 设置监听类型,固定取值为'getLeftTextOfCursor'。 |
2782| callback | (length: number) => string | 是   | 回调函数,获取编辑框最新状态下光标左侧指定长度的文本内容并返回。 |
2783
2784**错误码:**
2785
2786以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2787
2788| 错误码ID | 错误信息                             |
2789| -------- | -------------------------------------- |
2790| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2791| 12800009 | input method client detached. |
2792
2793**示例:**
2794
2795```ts
2796inputMethod.getController().on('getLeftTextOfCursor', (length: number) => {
2797  console.info(`Succeeded in subscribing getLeftTextOfCursor, length: ${length}`);
2798  let text: string = "";
2799  return text;
2800});
2801```
2802
2803### off('getLeftTextOfCursor')<sup>10+</sup>
2804
2805off(type: 'getLeftTextOfCursor', callback?: (length: number) => string): void
2806
2807取消订阅输入法应用获取光标左侧指定长度文本事件。使用callback异步回调。
2808
2809**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2810
2811**参数:**
2812
2813| 参数名 | 类型   | 必填 | 说明                                                         |
2814| ------ | ------ | ---- | ------------------------------------------------------------ |
2815| type   | string | 是   | 设置监听类型,固定取值为'getLeftTextOfCursor'。 |
2816| callback | (length: number) => string | 否  | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。|
2817
2818**示例:**
2819
2820```ts
2821let getLeftTextOfCursorCallback: (length: number) => string = (length: number): string => {
2822  console.info(`Succeeded in unsubscribing getLeftTextOfCursor, length: ${length}`);
2823  let text: string = "";
2824  return text;
2825};
2826
2827let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2828inputMethodController.off('getLeftTextOfCursor', getLeftTextOfCursorCallback);
2829inputMethodController.off('getLeftTextOfCursor');
2830```
2831
2832### on('getRightTextOfCursor')<sup>10+</sup>
2833
2834on(type: 'getRightTextOfCursor', callback: (length: number) => string): void
2835
2836订阅输入法应用获取光标右侧指定长度文本事件。使用callback异步回调。
2837
2838**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2839
2840**参数:**
2841
2842| 参数名   | 类型   | 必填 | 说明     |
2843| -------- | ----- | ---- | ------ |
2844| type     | string  | 是   | 设置监听类型,固定取值为'getRightTextOfCursor'。 |
2845| callback | (length: number) => string | 是   | 回调函数,获取编辑框最新状态下光标右侧指定长度的文本内容并返回。 |
2846
2847**错误码:**
2848
2849以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2850
2851| 错误码ID | 错误信息                             |
2852| -------- | -------------------------------------- |
2853| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2854| 12800009 | input method client detached. |
2855
2856**示例:**
2857
2858```ts
2859inputMethod.getController().on('getRightTextOfCursor', (length: number) => {
2860  console.info(`Succeeded in subscribing getRightTextOfCursor, length: ${length}`);
2861  let text: string = "";
2862  return text;
2863});
2864```
2865
2866### off('getRightTextOfCursor')<sup>10+</sup>
2867
2868off(type: 'getRightTextOfCursor', callback?: (length: number) => string): void
2869
2870取消订阅输入法应用获取光标右侧指定长度文本事件。使用callback异步回调。
2871
2872**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2873
2874**参数:**
2875
2876| 参数名 | 类型   | 必填 | 说明                                                         |
2877| ------ | ------ | ---- | ------------------------------------------------------------ |
2878| type   | string | 是   | 设置监听类型,固定取值为'getRightTextOfCursor'。 |
2879| callback | (length: number) => string | 否  |取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。|
2880
2881**示例:**
2882
2883```ts
2884let getRightTextOfCursorCallback: (length: number) => string = (length: number): string => {
2885  console.info(`Succeeded in unsubscribing getRightTextOfCursor, length: ${length}`);
2886  let text: string = "";
2887  return text;
2888};
2889
2890let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2891inputMethodController.off('getRightTextOfCursor', getRightTextOfCursorCallback);
2892inputMethodController.off('getRightTextOfCursor');
2893```
2894
2895### on('getTextIndexAtCursor')<sup>10+</sup>
2896
2897on(type: 'getTextIndexAtCursor', callback: () => number): void
2898
2899订阅输入法应用获取光标处文本索引事件。使用callback异步回调。
2900
2901**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2902
2903**参数:**
2904
2905| 参数名   | 类型   | 必填 | 说明     |
2906| -------- | ----- | ---- | ------ |
2907| type     | string  | 是   | 设置监听类型,固定取值为'getTextIndexAtCursor'。 |
2908| callback | () => number | 是   | 回调函数,获取编辑框最新状态下光标处文本索引并返回。 |
2909
2910**错误码:**
2911
2912以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
2913
2914| 错误码ID | 错误信息                             |
2915| -------- | -------------------------------------- |
2916| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2917| 12800009 | input method client detached. |
2918
2919**示例:**
2920
2921```ts
2922inputMethod.getController().on('getTextIndexAtCursor', () => {
2923  console.info(`Succeeded in subscribing getTextIndexAtCursor.`);
2924  let index: number = 0;
2925  return index;
2926});
2927```
2928
2929### off('getTextIndexAtCursor')<sup>10+</sup>
2930
2931off(type: 'getTextIndexAtCursor', callback?: () => number): void
2932
2933取消订阅输入法应用获取光标处文本索引事件。使用callback异步回调。
2934
2935**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2936
2937**参数:**
2938
2939| 参数名 | 类型   | 必填 | 说明                                                         |
2940| ------ | ------ | ---- | ------------------------------------------------------------ |
2941| type   | string | 是   | 设置监听类型,固定取值为'getTextIndexAtCursor'。 |
2942| callback | () => number | 否  | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。|
2943
2944**示例:**
2945
2946```ts
2947let getTextIndexAtCursorCallback: () => number = (): number => {
2948  console.info(`Succeeded in unsubscribing getTextIndexAtCursor.`);
2949  let index: number = 0;
2950  return index;
2951};
2952
2953let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2954inputMethodController.off('getTextIndexAtCursor', getTextIndexAtCursorCallback);
2955inputMethodController.off('getTextIndexAtCursor');
2956```
2957
2958### on('setPreviewText')<sup>17+</sup>
2959
2960on(type: 'setPreviewText', callback: SetPreviewTextCallback): void
2961
2962订阅输入法应用操作文本预览内容的事件。使用callback异步回调。
2963
2964> **说明:**
2965>
2966> 使用预览文本功能,需在调用[attach](#attach10)前订阅此事件,并和[on('finishTextPreview')](#onfinishtextpreview17)一起订阅。
2967
2968**系统能力:** SystemCapability.MiscServices.InputMethodFramework
2969
2970**参数:**
2971
2972| 参数名   | 类型   | 必填 | 说明     |
2973| -------- | ----- | ---- | ------ |
2974| type     | string  | 是   | 设置监听类型,固定取值为'setPreviewText'。 |
2975| callback | [SetPreviewTextCallback](#setpreviewtextcallback17) | 是   | 回调函数。用于接收文本预览的内容并返回。 |
2976
2977**错误码:**
2978
2979以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。
2980
2981| 错误码ID | 错误信息                             |
2982| -------- | -------------------------------------- |
2983| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
2984
2985**示例:**
2986
2987```ts
2988let setPreviewTextCallback1: inputMethod.SetPreviewTextCallback = (text: string, range: inputMethod.Range): void => {
2989  console.info(`SetPreviewTextCallback1: Received text - ${text}, Received range - start: ${range.start}, end: ${range.end}`);
2990};
2991
2992let setPreviewTextCallback2: inputMethod.SetPreviewTextCallback = (text: string, range: inputMethod.Range): void => {
2993  console.info(`setPreviewTextCallback2: Received text - ${text}, Received range - start: ${range.start}, end: ${range.end}`);
2994};
2995
2996let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
2997inputMethodController.on('setPreviewText', setPreviewTextCallback1);
2998console.info(`SetPreviewTextCallback1 subscribed to setPreviewText`);
2999inputMethodController.on('setPreviewText', setPreviewTextCallback2);
3000console.info(`SetPreviewTextCallback2 subscribed to setPreviewText`);
3001// 仅取消setPreviewText的callback1的回调。
3002inputMethodController.off('setPreviewText', setPreviewTextCallback1);
3003console.info(`SetPreviewTextCallback1 unsubscribed from setPreviewText`);
3004// 取消setPreviewText的所有回调。
3005inputMethodController.off('setPreviewText');
3006console.info(`All callbacks unsubscribed from setPreviewText`);
3007```
3008
3009### off('setPreviewText')<sup>17+</sup>
3010
3011off(type: 'setPreviewText', callback?: SetPreviewTextCallback): void
3012
3013取消订阅输入法应用操作文本预览内容的事件。使用callback异步回调。
3014
3015**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3016
3017**参数:**
3018
3019| 参数名 | 类型   | 必填 | 说明                                                         |
3020| ------ | ------ | ---- | ------------------------------------------------------------ |
3021| type   | string | 是   | 设置监听类型,固定取值为'setPreviewText'。 |
3022| callback | [SetPreviewTextCallback](#setpreviewtextcallback17) | 否  | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。|
3023
3024**示例:**
3025
3026```ts
3027let setPreviewTextCallback1: inputMethod.SetPreviewTextCallback = (text: string, range: inputMethod.Range): void => {
3028  console.info(`SetPreviewTextCallback1: Received text - ${text}, Received range - start: ${range.start}, end: ${range.end}`);
3029};
3030
3031let setPreviewTextCallback2: inputMethod.SetPreviewTextCallback = (text: string, range: inputMethod.Range): void => {
3032  console.info(`setPreviewTextCallback2: Received text - ${text}, Received range - start: ${range.start}, end: ${range.end}`);
3033};
3034
3035let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
3036inputMethodController.on('setPreviewText', setPreviewTextCallback1);
3037console.info(`SetPreviewTextCallback1 subscribed to setPreviewText`);
3038inputMethodController.on('setPreviewText', setPreviewTextCallback2);
3039console.info(`SetPreviewTextCallback2 subscribed to setPreviewText`);
3040// 仅取消setPreviewText的callback1的回调。
3041inputMethodController.off('setPreviewText', setPreviewTextCallback1);
3042console.info(`SetPreviewTextCallback1 unsubscribed from setPreviewText`);
3043// 取消setPreviewText的所有回调。
3044inputMethodController.off('setPreviewText');
3045console.info(`All callbacks unsubscribed from setPreviewText`);
3046```
3047
3048### on('finishTextPreview')<sup>17+</sup>
3049
3050on(type: 'finishTextPreview', callback: Callback&lt;void&gt;): void
3051
3052订阅结束文本预览事件。使用callback异步回调。
3053
3054> **说明:**
3055>
3056> 使用预览文本功能,需在调用[attach](#attach10)前订阅此事件,并和[on('setPreviewText')](#onsetpreviewtext17)一起订阅。
3057
3058**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3059
3060**参数:**
3061
3062| 参数名   | 类型   | 必填 | 说明     |
3063| -------- | ----- | ---- | ------ |
3064| type     | string  | 是   | 设置监听类型,固定取值为'finishTextPreview'。 |
3065| callback | Callback&lt;void&gt; | 是   | 回调函数。用于处理预览文本结束的逻辑,类型为void。|
3066
3067**错误码:**
3068
3069以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。
3070
3071| 错误码ID | 错误信息                             |
3072| -------- | -------------------------------------- |
3073| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
3074
3075**示例:**
3076
3077```ts
3078import { Callback } from '@kit.BasicServicesKit';
3079
3080let finishTextPreviewCallback1: Callback<void> = (): void => {
3081  console.info(`FinishTextPreviewCallback1: finishTextPreview event triggered`);
3082};
3083let finishTextPreviewCallback2: Callback<void> = (): void => {
3084  console.info(`FinishTextPreviewCallback2: finishTextPreview event triggered`);
3085};
3086
3087let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
3088inputMethodController.on('finishTextPreview', finishTextPreviewCallback1);
3089console.info(`FinishTextPreviewCallback1 subscribed to finishTextPreview`);
3090inputMethodController.on('finishTextPreview', finishTextPreviewCallback2);
3091console.info(`FinishTextPreviewCallback2 subscribed to finishTextPreview`);
3092// 仅取消finishTextPreview的callback1的回调。
3093inputMethodController.off('finishTextPreview', finishTextPreviewCallback1);
3094console.info(`FinishTextPreviewCallback1 unsubscribed from finishTextPreview`);
3095// 取消finishTextPreview的所有回调。
3096inputMethodController.off('finishTextPreview');
3097console.info(`All callbacks unsubscribed from finishTextPreview`);
3098```
3099
3100### off('finishTextPreview')<sup>17+</sup>
3101
3102off(type: 'finishTextPreview', callback?: Callback&lt;void&gt;): void
3103
3104取消订阅结束文本预览事件。使用callback异步回调。
3105
3106**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3107
3108**参数:**
3109
3110| 参数名 | 类型   | 必填 | 说明                                                         |
3111| ------ | ------ | ---- | ------------------------------------------------------------ |
3112| type   | string | 是   | 设置监听类型,固定取值为'finishTextPreview'。 |
3113| callback | Callback&lt;void&gt; | 否  | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。|
3114
3115**示例:**
3116
3117```ts
3118import { Callback } from '@kit.BasicServicesKit';
3119
3120let finishTextPreviewCallback1: Callback<void> = (): void => {
3121  console.info(`FinishTextPreviewCallback1: finishTextPreview event triggered`);
3122};
3123let finishTextPreviewCallback2: Callback<void> = (): void => {
3124  console.info(`FinishTextPreviewCallback2: finishTextPreview event triggered`);
3125};
3126
3127let inputMethodController: inputMethod.InputMethodController = inputMethod.getController();
3128inputMethodController.on('finishTextPreview', finishTextPreviewCallback1);
3129console.info(`FinishTextPreviewCallback1 subscribed to finishTextPreview`);
3130inputMethodController.on('finishTextPreview', finishTextPreviewCallback2);
3131console.info(`FinishTextPreviewCallback2 subscribed to finishTextPreview`);
3132// 仅取消finishTextPreview的callback1的回调。
3133inputMethodController.off('finishTextPreview', finishTextPreviewCallback1);
3134console.info(`FinishTextPreviewCallback1 unsubscribed from finishTextPreview`);
3135// 取消finishTextPreview的所有回调。
3136inputMethodController.off('finishTextPreview');
3137console.info(`All callbacks unsubscribed from finishTextPreview`);
3138```
3139
3140## InputMethodSetting<sup>8+</sup>
3141
3142下列API均需使用[getSetting](#inputmethodgetsetting9)获取到InputMethodSetting实例后,通过实例调用。
3143
3144### on('imeChange')<sup>9+</sup>
3145
3146on(type: 'imeChange', callback: (inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => void): void
3147
3148订阅输入法及子类型变化监听事件。使用callback异步回调。
3149
3150**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3151
3152**参数:**
3153
3154| 参数名   | 类型                            | 必填 | 说明                                                         |
3155| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
3156| type     | string                        | 是   | 设置监听类型,固定取值为'imeChange'。 |
3157| callback | (inputMethodProperty: [InputMethodProperty](#inputmethodproperty8), inputMethodSubtype: [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)) => void  | 是 | 回调函数,返回输入法属性对象及子类型对象。 |
3158
3159**示例:**
3160
3161```ts
3162import { InputMethodSubtype } from '@kit.IMEKit';
3163
3164inputMethod.getSetting()
3165  .on('imeChange', (inputMethodProperty: inputMethod.InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => {
3166    console.info(`Succeeded in subscribing imeChange: inputMethodProperty.name: ${inputMethodProperty.name} ` +
3167      `, inputMethodSubtype.id: ${inputMethodSubtype.id}`);
3168  });
3169```
3170
3171### off('imeChange')<sup>9+</sup>
3172
3173off(type: 'imeChange', callback?: (inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => void): void
3174
3175取消订阅输入法及子类型变化监听事件。使用callback异步回调。
3176
3177**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3178
3179**参数:**
3180
3181| 参数名   | 类型    | 必填 | 说明          |
3182| -------- | --------- | ---- | --------------- |
3183| type     | string    | 是   | 设置监听类型,固定取值为'imeChange'。 |
3184| callback | (inputMethodProperty: [InputMethodProperty](#inputmethodproperty8), inputMethodSubtype: [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)) => void  | 否 | 回调函数,返回取消订阅的输入法属性对象及子类型对象。 |
3185
3186**示例:**
3187
3188```ts
3189inputMethod.getSetting().off('imeChange');
3190```
3191
3192### listInputMethodSubtype<sup>9+</sup>
3193
3194listInputMethodSubtype(inputMethodProperty: InputMethodProperty, callback: AsyncCallback&lt;Array&lt;InputMethodSubtype&gt;&gt;): void
3195
3196获取指定输入法应用的所有子类型。使用callback异步回调。
3197
3198**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3199
3200**参数:**
3201
3202| 参数名   | 类型                                               | 必填 | 说明                   |
3203| -------- | -------------------------------------------------- | ---- | ---------------------- |
3204| inputMethodProperty | [InputMethodProperty](#inputmethodproperty8)| 是 | 输入法应用。 |
3205| callback | AsyncCallback&lt;Array<[InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)>&gt; | 是 | 回调函数,返回指定输入法应用的所有子类型。 |
3206
3207**错误码:**
3208
3209以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
3210
3211| 错误码ID | 错误信息                             |
3212| -------- | -------------------------------------- |
3213| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
3214| 12800001 | bundle manager error.                 |
3215| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3216
3217**示例:**
3218
3219```ts
3220import { InputMethodSubtype } from '@kit.IMEKit';
3221import { BusinessError } from '@kit.BasicServicesKit';
3222
3223let inputMethodProperty: inputMethod.InputMethodProperty = {
3224  name: 'com.example.keyboard',
3225  id: 'propertyId',
3226  packageName: 'com.example.keyboard',
3227  methodId: 'propertyId',
3228}
3229let inputMethodSetting: inputMethod.InputMethodSetting = inputMethod.getSetting();
3230
3231inputMethodSetting.listInputMethodSubtype(inputMethodProperty,
3232  (err: BusinessError, data: Array<InputMethodSubtype>) => {
3233    if (err) {
3234      console.error(`Failed to listInputMethodSubtype, code: ${err.code}, message: ${err.message}`);
3235      return;
3236    }
3237    console.info('Succeeded in listing inputMethodSubtype.');
3238  });
3239```
3240
3241### listInputMethodSubtype<sup>9+</sup>
3242
3243listInputMethodSubtype(inputMethodProperty: InputMethodProperty): Promise&lt;Array&lt;InputMethodSubtype&gt;&gt;
3244
3245获取指定输入法应用的所有子类型。使用promise异步回调。
3246
3247**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3248
3249**参数:**
3250
3251| 参数名   | 类型                                               | 必填 | 说明                   |
3252| -------- | -------------------------------------------------- | ---- | ---------------------- |
3253| inputMethodProperty | [InputMethodProperty](#inputmethodproperty8)| 是 | 输入法应用。 |
3254
3255**返回值:**
3256
3257| 类型                                                        | 说明                   |
3258| ----------------------------------------------------------- | ---------------------- |
3259| Promise<Array<[InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)>> | Promise对象,返回指定输入法应用的所有子类型。 |
3260
3261**错误码:**
3262
3263以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
3264
3265| 错误码ID | 错误信息                             |
3266| -------- | -------------------------------------- |
3267| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.           |
3268| 12800001 | bundle manager error.                 |
3269| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3270
3271**示例:**
3272
3273```ts
3274import { InputMethodSubtype } from '@kit.IMEKit';
3275import { BusinessError } from '@kit.BasicServicesKit';
3276
3277let inputMethodProperty: inputMethod.InputMethodProperty = {
3278  name: 'com.example.keyboard',
3279  id: 'propertyId',
3280  packageName: 'com.example.keyboard',
3281  methodId: 'propertyId',
3282}
3283let inputMethodSetting: inputMethod.InputMethodSetting = inputMethod.getSetting();
3284
3285inputMethodSetting.listInputMethodSubtype(inputMethodProperty).then((data: Array<InputMethodSubtype>) => {
3286  console.info('Succeeded in listing inputMethodSubtype.');
3287}).catch((err: BusinessError) => {
3288  console.error(`Failed to listInputMethodSubtype, code: ${err.code}, message: ${err.message}`);
3289})
3290```
3291
3292### listCurrentInputMethodSubtype<sup>9+</sup>
3293
3294listCurrentInputMethodSubtype(callback: AsyncCallback&lt;Array&lt;InputMethodSubtype&gt;&gt;): void
3295
3296查询当前输入法应用的所有子类型。使用callback异步回调。
3297
3298**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3299
3300**参数:**
3301
3302| 参数名   | 类型                                               | 必填 | 说明                   |
3303| -------- | -------------------------------------------------- | ---- | ---------------------- |
3304| callback | AsyncCallback&lt;Array<[InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)>&gt; | 是   | 回调函数,返回当前输入法应用的所有子类型。 |
3305
3306**错误码:**
3307
3308以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
3309
3310| 错误码ID | 错误信息                             |
3311| -------- | -------------------------------------- |
3312| 12800001 | bundle manager error.                 |
3313| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3314
3315**示例:**
3316
3317```ts
3318import { InputMethodSubtype } from '@kit.IMEKit';
3319import { BusinessError } from '@kit.BasicServicesKit';
3320
3321let inputMethodSetting: inputMethod.InputMethodSetting = inputMethod.getSetting();
3322inputMethodSetting.listCurrentInputMethodSubtype((err: BusinessError, data: Array<InputMethodSubtype>) => {
3323  if (err) {
3324    console.error(`Failed to listCurrentInputMethodSubtype, code: ${err.code}, message: ${err.message}`);
3325    return;
3326  }
3327  console.info('Succeeded in listing currentInputMethodSubtype.');
3328});
3329```
3330
3331### listCurrentInputMethodSubtype<sup>9+</sup>
3332
3333listCurrentInputMethodSubtype(): Promise&lt;Array&lt;InputMethodSubtype&gt;&gt;
3334
3335查询当前输入法应用的所有子类型。使用promise异步回调。
3336
3337**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3338
3339**返回值:**
3340
3341| 类型                                                        | 说明                   |
3342| ----------------------------------------------------------- | ---------------------- |
3343| Promise<Array<[InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)>> | Promise对象,返回当前输入法应用的所有子类型。 |
3344
3345**错误码:**
3346
3347以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
3348
3349| 错误码ID | 错误信息                             |
3350| -------- | -------------------------------------- |
3351| 12800001 | bundle manager error.                 |
3352| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3353
3354**示例:**
3355
3356```ts
3357import { InputMethodSubtype } from '@kit.IMEKit';
3358import { BusinessError } from '@kit.BasicServicesKit';
3359
3360let inputMethodSetting: inputMethod.InputMethodSetting = inputMethod.getSetting();
3361
3362inputMethodSetting.listCurrentInputMethodSubtype().then((data: Array<InputMethodSubtype>) => {
3363  console.info('Succeeded in listing currentInputMethodSubtype.');
3364}).catch((err: BusinessError) => {
3365  console.error(`Failed to listCurrentInputMethodSubtype, code: ${err.code}, message: ${err.message}`);
3366})
3367
3368```
3369
3370### getInputMethods<sup>9+</sup>
3371
3372getInputMethods(enable: boolean, callback: AsyncCallback&lt;Array&lt;InputMethodProperty&gt;&gt;): void
3373
3374获取已激活/未激活的输入法应用列表。使用callback异步回调。
3375
3376> **说明:**
3377>
3378> 已激活输入法为使能的输入法应用。默认输入法默认使能,其他输入法可被设置为使能或非使能。
3379>
3380> 已激活输入法列表包括默认输入法和已被设置为使能的输入法应用,未激活输入法列表包括除使能输入法以外的其他已安装的输入法。
3381
3382**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3383
3384**参数:**
3385
3386| 参数名   | 类型                                                | 必填 | 说明                          |
3387| -------- | --------------------------------------------------- | ---- | ----------------------------- |
3388| enable   | boolean                                             | 是   |true表示返回已激活输入法列表,false表示返回未激活输入法列表。 |
3389| callback | AsyncCallback&lt;Array<[InputMethodProperty](#inputmethodproperty8)>&gt; |  是  | 回调函数,返回已激活/未激活输入法列表。 |
3390
3391**错误码:**
3392
3393以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
3394
3395| 错误码ID | 错误信息                            |
3396| -------- | ----------------------------------- |
3397| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
3398| 12800001 | bundle manager error.               |
3399| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3400
3401**示例:**
3402
3403```ts
3404import { BusinessError } from '@kit.BasicServicesKit';
3405
3406inputMethod.getSetting().getInputMethods(true, (err: BusinessError, data: Array<inputMethod.InputMethodProperty>) => {
3407  if (err) {
3408    console.error(`Failed to getInputMethods, code: ${err.code}, message: ${err.message}`);
3409    return;
3410  }
3411  console.info('Succeeded in getting inputMethods.');
3412});
3413```
3414
3415### getInputMethods<sup>9+</sup>
3416
3417getInputMethods(enable: boolean): Promise&lt;Array&lt;InputMethodProperty&gt;&gt;
3418
3419获取已激活/未激活的输入法应用列表。使用promise异步回调。
3420
3421> **说明:**
3422>
3423> 已激活输入法为使能的输入法应用。默认输入法默认使能,其他输入法可被设置为使能或非使能。
3424>
3425> 已激活输入法列表包括默认输入法和已被设置为使能的输入法应用,未激活输入法列表包括除使能输入法以外的其他已安装的输入法。
3426
3427**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3428
3429**参数:**
3430
3431| 参数名 | 类型    | 必填 | 说明                    |
3432| ------ | ------- | ---- | ----------------------- |
3433| enable | boolean | 是   |- true表示返回已激活输入法列表,false表示返回未激活输入法列表。 |
3434
3435**返回值:**
3436
3437| 类型                                                         | 说明                                       |
3438| ------------------------------------------------------------ | ------------------------------------------ |
3439| Promise\<Array\<[InputMethodProperty](#inputmethodproperty8)>> | Promise对象,返回已激活/未激活输入法列表。 |
3440
3441**错误码:**
3442
3443以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
3444
3445| 错误码ID | 错误信息                            |
3446| -------- | ----------------------------------- |
3447| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
3448| 12800001 | bundle manager error.               |
3449| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3450
3451**示例:**
3452
3453```ts
3454import { BusinessError } from '@kit.BasicServicesKit';
3455
3456inputMethod.getSetting().getInputMethods(true).then((data: Array<inputMethod.InputMethodProperty>) => {
3457  console.info('Succeeded in getting inputMethods.');
3458}).catch((err: BusinessError) => {
3459  console.error(`Failed to getInputMethods, code: ${err.code}, message: ${err.message}`);
3460})
3461
3462```
3463
3464### getInputMethodsSync<sup>11+</sup>
3465
3466getInputMethodsSync(enable: boolean): Array&lt;InputMethodProperty&gt;
3467
3468获取已激活/未激活的输入法应用列表。同步接口。
3469
3470> **说明:**
3471>
3472> 已激活输入法为使能的输入法应用。默认输入法默认使能,其他输入法可被设置为使能或非使能。
3473>
3474> 已激活输入法列表包括默认输入法和已被设置为使能的输入法应用,未激活输入法列表包括除使能输入法以外的其他已安装的输入法。
3475
3476**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3477
3478**参数:**
3479
3480| 参数名 | 类型    | 必填 | 说明                    |
3481| ------ | ------- | ---- | ----------------------- |
3482| enable | boolean | 是   |- true表示返回已激活输入法列表,false表示返回未激活输入法列表。 |
3483
3484**返回值:**
3485
3486| 类型                                                 | 说明                          |
3487| ---------------------------------------------------- | ----------------------------- |
3488| Array\<[InputMethodProperty](#inputmethodproperty8)> | 返回已激活/未激活输入法列表。 |
3489
3490**错误码:**
3491
3492以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
3493
3494| 错误码ID | 错误信息                             |
3495| -------- | -------------------------------------- |
3496| 401      | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
3497| 12800001 | bundle manager error.                 |
3498| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3499
3500**示例:**
3501
3502```ts
3503let imeProperty: Array<inputMethod.InputMethodProperty> = inputMethod.getSetting().getInputMethodsSync(true);
3504```
3505
3506### getAllInputMethods<sup>11+</sup>
3507
3508getAllInputMethods(callback: AsyncCallback&lt;Array&lt;InputMethodProperty&gt;&gt;): void
3509
3510获取所有输入法应用列表。使用callback异步回调。
3511
3512**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3513
3514**参数:**
3515
3516| 参数名   | 类型                                                         | 必填 | 说明                           |
3517| -------- | ------------------------------------------------------------ | ---- | ------------------------------ |
3518| callback | AsyncCallback&lt;Array<[InputMethodProperty](#inputmethodproperty8)>&gt; | 是   | 回调函数,返回所有输入法列表。 |
3519
3520**错误码:**
3521
3522以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
3523
3524| 错误码ID | 错误信息                            |
3525| -------- | ----------------------------------- |
3526| 12800001 | bundle manager error.               |
3527| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3528
3529**示例:**
3530
3531```ts
3532import { BusinessError } from '@kit.BasicServicesKit';
3533
3534inputMethod.getSetting().getAllInputMethods((err: BusinessError, data: Array<inputMethod.InputMethodProperty>) => {
3535  if (err) {
3536    console.error(`Failed to getAllInputMethods, code: ${err.code}, message: ${err.message}`);
3537    return;
3538  }
3539  console.info('Succeeded in getting all inputMethods.');
3540});
3541```
3542
3543### getAllInputMethods<sup>11+</sup>
3544
3545getAllInputMethods(): Promise&lt;Array&lt;InputMethodProperty&gt;&gt;
3546
3547获取所有输入法应用列表。使用promise异步回调。
3548
3549**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3550
3551**返回值:**
3552
3553| 类型                                                         | 说明                              |
3554| ------------------------------------------------------------ | --------------------------------- |
3555| Promise\<Array\<[InputMethodProperty](#inputmethodproperty8)>> | Promise对象,返回所有输入法列表。 |
3556
3557**错误码:**
3558
3559以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
3560
3561| 错误码ID | 错误信息                            |
3562| -------- | ----------------------------------- |
3563| 12800001 | bundle manager error.              |
3564| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3565
3566**示例:**
3567
3568```ts
3569import { BusinessError } from '@kit.BasicServicesKit';
3570
3571inputMethod.getSetting().getAllInputMethods().then((data: Array<inputMethod.InputMethodProperty>) => {
3572  console.info('Succeeded in getting all inputMethods.');
3573}).catch((err: BusinessError) => {
3574  console.error(`Failed to getAllInputMethods, code: ${err.code}, message: ${err.message}`);
3575})
3576```
3577
3578### getAllInputMethodsSync<sup>11+</sup>
3579
3580getAllInputMethodsSync(): Array&lt;InputMethodProperty&gt;
3581
3582获取所有输入法应用列表。同步接口。
3583
3584**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3585
3586**返回值:**
3587
3588| 类型                                                 | 说明               |
3589| ---------------------------------------------------- | ------------------ |
3590| Array\<[InputMethodProperty](#inputmethodproperty8)> | 返回所有输入法列表 |
3591
3592**错误码:**
3593
3594以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
3595
3596| 错误码ID | 错误信息                            |
3597| -------- | ----------------------------------- |
3598| 12800001 | bundle manager error.              |
3599| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3600
3601**示例:**
3602
3603```ts
3604let imeProperty: Array<inputMethod.InputMethodProperty> = inputMethod.getSetting().getAllInputMethodsSync();
3605```
3606
3607### showOptionalInputMethods<sup>(deprecated)</sup>
3608
3609showOptionalInputMethods(callback: AsyncCallback&lt;boolean&gt;): void
3610
3611显示输入法选择对话框。使用callback异步回调。
3612> **说明:**
3613>
3614> 从API version 9开始支持,从API version 18开始废弃。
3615
3616**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3617
3618**参数:**
3619
3620| 参数名 | 类型 | 必填 | 说明 |
3621| -------- | -------- | -------- | -------- |
3622| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数。当输入法选择对话框显示成功,err为undefined,data为true;否则为错误对象。 |
3623
3624**错误码:**
3625
3626以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
3627
3628| 错误码ID | 错误信息                             |
3629| -------- | -------------------------------------- |
3630| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3631
3632**示例:**
3633
3634```ts
3635import { BusinessError } from '@kit.BasicServicesKit';
3636
3637inputMethod.getSetting().showOptionalInputMethods((err: BusinessError, result: boolean) => {
3638  if (err) {
3639    console.error(`Failed to showOptionalInputMethods, code: ${err.code}, message: ${err.message}`);
3640    return;
3641  }
3642  if (result) {
3643    console.info('Succeeded in showing optionalInputMethods.');
3644  } else {
3645    console.error(`Failed to showOptionalInputMethods.`);
3646  }
3647});
3648```
3649
3650### showOptionalInputMethods<sup>(deprecated)</sup>
3651
3652showOptionalInputMethods(): Promise&lt;boolean&gt;
3653
3654显示输入法选择对话框。使用promise异步回调。
3655
3656> **说明:**
3657>
3658> 从API version 9开始支持,从API version 18开始废弃。
3659
3660**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3661
3662**返回值:**
3663
3664| 类型 | 说明 |
3665| -------- | -------- |
3666| Promise&lt;boolean&gt; | Promise对象。当输入法选择对话框显示成功,err为undefined,data为true;否则为错误对象。 |
3667
3668**错误码:**
3669
3670以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。
3671
3672| 错误码ID | 错误信息                             |
3673| -------- | -------------------------------------- |
3674| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3675
3676**示例:**
3677
3678```ts
3679import { BusinessError } from '@kit.BasicServicesKit';
3680
3681inputMethod.getSetting().showOptionalInputMethods().then((result: boolean) => {
3682  if (result) {
3683    console.info('Succeeded in showing optionalInputMethods.');
3684  } else {
3685    console.error(`Failed to showOptionalInputMethods.`);
3686  }
3687}).catch((err: BusinessError) => {
3688  console.error(`Failed to showOptionalInputMethods, code: ${err.code}, message: ${err.message}`);
3689})
3690```
3691
3692### listInputMethod<sup>(deprecated)</sup>
3693
3694listInputMethod(callback: AsyncCallback&lt;Array&lt;InputMethodProperty&gt;&gt;): void
3695
3696查询已安装的输入法列表。使用callback异步回调。
3697
3698> **说明:**
3699>
3700> 从API version 8开始支持,从API version 9开始废弃,建议使用[getInputMethods](#getinputmethods9)替代。
3701
3702**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3703
3704**参数:**
3705
3706| 参数名   | 类型                                               | 必填 | 说明                   |
3707| -------- | -------------------------------------------------- | ---- | ---------------------- |
3708| callback | AsyncCallback&lt;Array<[InputMethodProperty](#inputmethodproperty8)>&gt; | 是   | 回调函数,返回已安装的输入法列表。 |
3709
3710**示例:**
3711
3712```ts
3713import { BusinessError } from '@kit.BasicServicesKit';
3714
3715inputMethod.getSetting().listInputMethod((err: BusinessError, data: Array<inputMethod.InputMethodProperty>) => {
3716  if (err) {
3717    console.error(`Failed to listInputMethod, code: ${err.code}, message: ${err.message}`);
3718    return;
3719  }
3720  console.info('Succeeded in listing inputMethod.');
3721});
3722```
3723
3724### listInputMethod<sup>(deprecated)</sup>
3725
3726listInputMethod(): Promise&lt;Array&lt;InputMethodProperty&gt;&gt;
3727
3728查询已安装的输入法列表。使用promise异步回调。
3729
3730> **说明:**
3731>
3732> 从API version 8开始支持,从API version 9开始废弃,建议使用[getInputMethods](#getinputmethods9-1)替代。
3733
3734**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3735
3736**返回值:**
3737
3738| 类型                                                        | 说明                   |
3739| ----------------------------------------------------------- | ---------------------- |
3740| Promise<Array<[InputMethodProperty](#inputmethodproperty8)>> | Promise对象,返回已安装输入法列表。 |
3741
3742**示例:**
3743
3744```ts
3745import { BusinessError } from '@kit.BasicServicesKit';
3746
3747inputMethod.getSetting().listInputMethod().then((data: Array<inputMethod.InputMethodProperty>) => {
3748  console.info('Succeeded in listing inputMethod.');
3749}).catch((err: BusinessError) => {
3750  console.error(`Failed to listInputMethod, code: ${err.code}, message: ${err.message}`);
3751})
3752```
3753
3754### displayOptionalInputMethod<sup>(deprecated)</sup>
3755
3756displayOptionalInputMethod(callback: AsyncCallback&lt;void&gt;): void
3757
3758显示输入法选择对话框。使用callback异步回调。
3759
3760> **说明:**
3761>
3762> 从API version 8开始支持,从API version 9开始废弃。
3763
3764**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3765
3766**参数:**
3767
3768| 参数名 | 类型 | 必填 | 说明 |
3769| -------- | -------- | -------- | -------- |
3770| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当输入法选择对话框显示成功。err为undefined,否则为错误对象。 |
3771
3772**示例:**
3773
3774```ts
3775import { BusinessError } from '@kit.BasicServicesKit';
3776
3777inputMethod.getSetting().displayOptionalInputMethod((err: BusinessError) => {
3778  if (err) {
3779    console.error(`Failed to displayOptionalInputMethod, code: ${err.code}, message: ${err.message}`);
3780    return;
3781  }
3782  console.info('Succeeded in displaying optionalInputMethod.');
3783});
3784```
3785
3786### displayOptionalInputMethod<sup>(deprecated)</sup>
3787
3788displayOptionalInputMethod(): Promise&lt;void&gt;
3789
3790显示输入法选择对话框。使用promise异步回调。
3791
3792> **说明:**
3793>
3794> 从API version 8开始支持,从API version 9开始废弃。
3795
3796**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3797
3798**返回值:**
3799
3800| 类型 | 说明 |
3801| -------- | -------- |
3802| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
3803
3804**示例:**
3805
3806```ts
3807import { BusinessError } from '@kit.BasicServicesKit';
3808
3809inputMethod.getSetting().displayOptionalInputMethod().then(() => {
3810  console.info('Succeeded in displaying optionalInputMethod.');
3811}).catch((err: BusinessError) => {
3812  console.error(`Failed to displayOptionalInputMethod, code: ${err.code}, message: ${err.message}`);
3813})
3814```
3815
3816### getInputMethodState<sup>15+</sup>
3817
3818getInputMethodState(): Promise&lt;EnabledState&gt;
3819
3820查询输入法的启用状态。使用promise异步回调。
3821
3822**系统能力:** SystemCapability.MiscServices.InputMethodFramework
3823
3824**返回值:**
3825
3826| 类型                                    | 说明                                                         |
3827| --------------------------------------- | ------------------------------------------------------------ |
3828| Promise\<[EnabledState](#enabledstate15)> | Promise对象,返回EnabledState.DISABLED表示未启用; 返回EnabledState.BASIC_MODE表示基础模式; 返回EnabledState.FULL_EXPERIENCE_MODE表示完整体验模式。 |
3829
3830**错误码:**
3831
3832以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。
3833
3834| 错误码ID | 错误信息                            |
3835| -------- | ----------------------------------- |
3836| 12800004 | not an input method application.    |
3837| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. |
3838
3839**示例:**
3840
3841```ts
3842import { BusinessError } from '@kit.BasicServicesKit';
3843
3844inputMethod.getSetting().getInputMethodState().then((status: inputMethod.EnabledState) => {
3845  console.info(`Succeeded in getInputMethodState, status: ${status}`);
3846}).catch((err: BusinessError) => {
3847  console.error(`Failed to getInputMethodState, code: ${err.code}, message: ${err.message}`);
3848})
3849```