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