1# @ohos.inputMethod (输入法框架) 2 3本模块主要面向普通前台应用(备忘录、信息、设置等系统应用与三方应用),提供对输入法(输入法应用)的控制、管理能力,包括显示/隐藏输入法软键盘、切换输入法、获取所有输入法列表等等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9 10## 导入模块 11 12```ts 13import inputMethod from '@ohos.inputMethod'; 14``` 15 16## 常量<sup>8+</sup> 17 18常量值。 19 20**系统能力:** SystemCapability.MiscServices.InputMethodFramework 21 22| 参数名 | 类型 | 常量值 | 说明 | 23| -------- | -------- | -------- | -------- | 24| MAX_TYPE_NUM | 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 | 是 | 否 | 必填。输入法唯一标识。| 36| label<sup>9+</sup> | string | 是 | 否 | 非必填。输入法对外显示名称。| 37| labelId<sup>10+</sup> | string | 是 | 否 | 非必填。输入法对外显示名称资源号。| 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以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/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.getSetting<sup>9+</sup> 73 74getSetting(): InputMethodSetting 75 76获取客户端设置实例[InputMethodSetting](#inputmethodsetting8)。 77 78**系统能力:** SystemCapability.MiscServices.InputMethodFramework 79 80**返回值:** 81 82| 类型 | 说明 | 83| ----------------------------------------- | ---------------------------- | 84| [InputMethodSetting](#inputmethodsetting8) | 回调返回当前客户端设置实例。 | 85 86**错误码:** 87 88以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 89 90| 错误码ID | 错误信息 | 91| -------- | -------------------------------------- | 92| 12800007 | settings extension error. | 93 94**示例:** 95 96```ts 97let inputMethodSetting = inputMethod.getSetting(); 98``` 99 100## inputMethod.switchInputMethod<sup>9+</sup> 101 102switchInputMethod(target: InputMethodProperty, callback: AsyncCallback<boolean>): void 103 104切换输入法,仅系统应用可用。使用callback异步回调。 105 106**需要权限:** ohos.permission.CONNECT_IME_ABILITY 107 108**系统能力:** SystemCapability.MiscServices.InputMethodFramework 109 110**参数:** 111 112| 参数名 | 类型 | 必填 | 说明 | 113| -------- | -------- | -------- | -------- | 114| target | [InputMethodProperty](#inputmethodproperty8) | 是 | 目标输入法。 | 115| callback | AsyncCallback<boolean> | 是 | 回调函数。当输入法切换成功,err为undefined,data为true;否则为错误对象。 | 116 117**错误码:** 118 119以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 120 121| 错误码ID | 错误信息 | 122| -------- | -------------------------------------- | 123| 12800005 | configuration persisting error. | 124| 12800008 | input method manager service error. | 125 126**示例:** 127 128```ts 129let currentIme = inputMethod.getCurrentInputMethod(); 130try{ 131 inputMethod.switchInputMethod(currentIme, (err: BusinessError, result: boolean) => { 132 if (err) { 133 console.error(`Failed to switchInputMethod: ${JSON.stringify(err)}`); 134 return; 135 } 136 if (result) { 137 console.log('Succeeded in switching inputmethod.'); 138 } else { 139 console.error('Failed to switchInputMethod.'); 140 } 141 }); 142} catch(err) { 143 console.error(`Failed to switchInputMethod: ${JSON.stringify(err)}`); 144} 145``` 146## inputMethod.switchInputMethod<sup>9+</sup> 147switchInputMethod(target: InputMethodProperty): Promise<boolean> 148 149切换输入法,仅系统应用可用。使用promise异步回调。 150 151**需要权限:** ohos.permission.CONNECT_IME_ABILITY 152 153**系统能力:** SystemCapability.MiscServices.InputMethodFramework 154 155**参数:** 156 157 | 参数名 | 类型 | 必填 | 说明 | 158 | -------- | -------- | -------- | -------- | 159 |target | [InputMethodProperty](#inputmethodproperty8)| 是 | 目标输入法。 | 160 161**返回值:** 162 163 | 类型 | 说明 | 164 | ----------------------------------------- | ---------------------------- | 165 | Promise\<boolean> | Promise对象。返回true表示切换输入法成功,返回false表示切换输入法失败。 | 166 167**错误码:** 168 169以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 170 171| 错误码ID | 错误信息 | 172| -------- | -------------------------------------- | 173| 12800005 | configuration persisting error. | 174| 12800008 | input method manager service error. | 175 176**示例:** 177 178```ts 179let currentIme = inputMethod.getCurrentInputMethod(); 180try { 181 inputMethod.switchInputMethod(currentIme).then((result: boolean) => { 182 if (result) { 183 console.log('Succeeded in switching inputmethod.'); 184 } else { 185 console.error('Failed to switchInputMethod.'); 186 } 187 }).catch((err: BusinessError) => { 188 console.error(`Failed to switchInputMethod: ${JSON.stringify(err)}`); 189 }) 190} catch (err) { 191 console.error(`Failed to switchInputMethod: ${JSON.stringify(err)}`); 192} 193``` 194 195## inputMethod.getCurrentInputMethod<sup>9+</sup> 196 197getCurrentInputMethod(): InputMethodProperty 198 199使用同步方法获取当前输入法。 200 201**系统能力:** SystemCapability.MiscServices.InputMethodFramework 202 203**返回值:** 204 205| 类型 | 说明 | 206| -------------------------------------------- | ------------------------ | 207| [InputMethodProperty](#inputmethodproperty8) | 返回当前输入法属性对象。 | 208 209**示例:** 210 211```ts 212let currentIme = inputMethod.getCurrentInputMethod(); 213``` 214 215## inputMethod.switchCurrentInputMethodSubtype<sup>9+</sup> 216 217switchCurrentInputMethodSubtype(target: InputMethodSubtype, callback: AsyncCallback\<boolean>): void 218 219切换当前输入法的子类型。使用callback异步回调。 220 221> **说明:** 222> 223> 在API version 9版本,仅支持系统应用调用;API version 10版本起,支持系统应用和当前输入法应用调用。 224 225**需要权限:** ohos.permission.CONNECT_IME_ABILITY 226 227**系统能力:** SystemCapability.MiscServices.InputMethodFramework 228 229**参数:** 230 231| 参数名 | 类型 | 必填 | 说明 | 232| -------- | -------- | -------- | -------- | 233| target | [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)| 是 | 目标输入法子类型。 | 234| callback | AsyncCallback<boolean> | 是 | 回调函数。当输入法子类型切换成功,err为undefined,data为true;否则为错误对象。| 235 236**错误码:** 237 238以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 239 240| 错误码ID | 错误信息 | 241| -------- | -------------------------------------- | 242| 12800005 | configuration persisting error. | 243| 12800008 | input method manager service error. | 244 245**示例:** 246 247```ts 248try { 249 let extra: Record<string, string> = {} 250 inputMethod.switchCurrentInputMethodSubtype({ 251 id: "ServiceExtAbility", 252 label: "", 253 name: "com.example.kikakeyboard", 254 mode: "upper", 255 locale: "", 256 language: "", 257 icon: "", 258 iconId: 0, 259 extra: extra 260 }, (err: BusinessError, result: boolean) => { 261 if (err) { 262 console.error(`Failed to switchCurrentInputMethodSubtype: ${JSON.stringify(err)}`); 263 return; 264 } 265 if (result) { 266 console.log('Succeeded in switching currentInputMethodSubtype.'); 267 } else { 268 console.error('Failed to switchCurrentInputMethodSubtype'); 269 } 270 }); 271} catch(err) { 272 console.error(`Failed to switchCurrentInputMethodSubtype: ${JSON.stringify(err)}`); 273} 274``` 275 276## inputMethod.switchCurrentInputMethodSubtype<sup>9+</sup> 277 278switchCurrentInputMethodSubtype(target: InputMethodSubtype): Promise<boolean> 279 280切换当前输入法的子类型。使用promise异步回调。 281 282> **说明:** 283> 284> 在API version 9版本,仅支持系统应用调用;API version 10版本起,支持系统应用和当前输入法应用调用。 285 286**需要权限:** ohos.permission.CONNECT_IME_ABILITY 287 288**系统能力:** SystemCapability.MiscServices.InputMethodFramework 289 290**参数:** 291 292| 参数名 | 类型 | 必填 | 说明 | 293| -------- | -------- | -------- | -------- | 294|target | [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)| 是 | 目标输入法子类型。 | 295 296**返回值:** 297 298| 类型 | 说明 | 299| ----------------------------------------- | ---------------------------- | 300| Promise\<boolean> | Promise对象。返回true表示当前输入法切换子类型成功,返回false表示当前输入法切换子类型成功失败。 | 301 302**错误码:** 303 304以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 305 306| 错误码ID | 错误信息 | 307| -------- | -------------------------------------- | 308| 12800005 | configuration persisting error. | 309| 12800008 | input method manager service error. | 310 311**示例:** 312 313```ts 314try { 315 let extra: Record<string, string> = {} 316 inputMethod.switchCurrentInputMethodSubtype({ 317 id: "ServiceExtAbility", 318 label: "", 319 name: "com.example.kikakeyboard", 320 mode: "upper", 321 locale: "", 322 language: "", 323 icon: "", 324 iconId: 0, 325 extra: extra 326 }).then((result: boolean) => { 327 if (result) { 328 console.log('Succeeded in switching currentInputMethodSubtype.'); 329 } else { 330 console.error('Failed to switchCurrentInputMethodSubtype.'); 331 } 332 }).catch((err: BusinessError) => { 333 console.error(`Failed to switchCurrentInputMethodSubtype: ${JSON.stringify(err)}`); 334 }) 335} catch(err) { 336 console.error(`Failed to switchCurrentInputMethodSubtype: ${JSON.stringify(err)}`); 337} 338``` 339 340## inputMethod.getCurrentInputMethodSubtype<sup>9+</sup> 341 342getCurrentInputMethodSubtype(): InputMethodSubtype 343 344获取当前输入法的子类型。 345 346**系统能力:** SystemCapability.MiscServices.InputMethodFramework 347 348**返回值:** 349 350| 类型 | 说明 | 351| -------------------------------------------- | ------------------------ | 352| [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype) | 返回当前输入法子类型对象。 | 353 354**示例:** 355 356```ts 357let currentImeSubType = inputMethod.getCurrentInputMethodSubtype(); 358``` 359 360## inputMethod.switchCurrentInputMethodAndSubtype<sup>9+</sup> 361 362switchCurrentInputMethodAndSubtype(inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype, callback: AsyncCallback\<boolean>): void 363 364切换至指定输入法的指定子类型,适用于跨输入法切换子类型,仅系统应用可用。使用callback异步回调。 365 366**需要权限:** ohos.permission.CONNECT_IME_ABILITY 367 368**系统能力:** SystemCapability.MiscServices.InputMethodFramework 369 370**参数:** 371 372| 参数名 | 类型 | 必填 | 说明 | 373| -------- | -------- | -------- | -------- | 374|inputMethodProperty | [InputMethodProperty](#inputmethodproperty8)| 是 | 目标输入法。 | 375|inputMethodSubtype | [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)| 是 | 目标输入法子类型。 | 376| callback | AsyncCallback<boolean> | 是 | 回调函数。当输入法和子类型切换成功,err为undefined,data为获取到的切换子类型结果true;否则为错误对象。 | 377 378**错误码:** 379 380以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 381 382| 错误码ID | 错误信息 | 383| -------- | -------------------------------------- | 384| 12800005 | configuration persisting error. | 385| 12800008 | input method manager service error. | 386 387**示例:** 388 389```ts 390let currentIme = inputMethod.getCurrentInputMethod(); 391let imSubType = inputMethod.getCurrentInputMethodSubtype(); 392try { 393 inputMethod.switchCurrentInputMethodAndSubtype(currentIme, imSubType, (err: BusinessError, result: boolean) => { 394 if (err) { 395 console.error(`Failed to switchCurrentInputMethodAndSubtype: ${JSON.stringify(err)}`); 396 return; 397 } 398 if (result) { 399 console.log('Succeeded in switching currentInputMethodAndSubtype.'); 400 } else { 401 console.error('Failed to switchCurrentInputMethodAndSubtype.'); 402 } 403 }); 404} catch (err) { 405 console.error(`Failed to switchCurrentInputMethodAndSubtype: ${JSON.stringify(err)}`); 406} 407``` 408 409## inputMethod.switchCurrentInputMethodAndSubtype<sup>9+</sup> 410 411switchCurrentInputMethodAndSubtype(inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype): Promise<boolean> 412 413切换至指定输入法的指定子类型,适用于跨输入法切换子类型,仅系统应用可用。使用promise异步回调。 414 415**需要权限:** ohos.permission.CONNECT_IME_ABILITY 416 417**系统能力:** SystemCapability.MiscServices.InputMethodFramework 418 419**参数:** 420 421| 参数名 | 类型 | 必填 | 说明 | 422| -------- | -------- | -------- | -------- | 423|inputMethodProperty | [InputMethodProperty](#inputmethodproperty8)| 是 | 目标输入法。 | 424|inputMethodSubtype | [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)| 是 | 目标输入法子类型。 | 425 426**返回值:** 427 428| 类型 | 说明 | 429| ----------------------------------------- | ---------------------------- | 430| Promise\<boolean> | Promise对象。返回true表示切换至指定输入法的指定子类型成功,返回false表示切换至指定输入法的指定子类型失败。 | 431 432**错误码:** 433 434以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 435 436| 错误码ID | 错误信息 | 437| -------- | -------------------------------------- | 438| 12800005 | configuration persisting error. | 439| 12800008 | input method manager service error. | 440 441**示例:** 442 443```ts 444let currentIme = inputMethod.getCurrentInputMethod(); 445let imSubType = inputMethod.getCurrentInputMethodSubtype(); 446try { 447 inputMethod.switchCurrentInputMethodAndSubtype(currentIme, imSubType).then((result: boolean) => { 448 if (result) { 449 console.log('Succeeded in switching currentInputMethodAndSubtype.'); 450 } else { 451 console.error('Failed to switchCurrentInputMethodAndSubtype.'); 452 } 453 }).catch((err: BusinessError) => { 454 console.error(`Failed to switchCurrentInputMethodAndSubtype: ${JSON.stringify(err)}`); 455 }) 456} catch(err) { 457 console.error(`Failed to switchCurrentInputMethodAndSubtype: ${JSON.stringify(err)}`); 458} 459``` 460 461## inputMethod.getInputMethodController<sup>(deprecated)</sup> 462 463getInputMethodController(): InputMethodController 464 465获取客户端实例[InputMethodController](#inputmethodcontroller)。 466 467> **说明:** 468> 469> 从API version 6开始支持,从API version 9开始废弃,建议使用[getController()](#inputmethodgetcontroller9)替代。 470 471**系统能力:** SystemCapability.MiscServices.InputMethodFramework 472 473**返回值:** 474 475| 类型 | 说明 | 476| ----------------------------------------------- | ------------------------ | 477| [InputMethodController](#inputmethodcontroller) | 回调返回当前客户端实例。 | 478 479**示例:** 480 481```ts 482let inputMethodController = inputMethod.getInputMethodController(); 483``` 484 485## inputMethod.getInputMethodSetting<sup>(deprecated)</sup> 486 487getInputMethodSetting(): InputMethodSetting 488 489获取客户端设置实例[InputMethodSetting](#inputmethodsetting8)。 490 491> **说明:** 492> 493> 从API version 6开始支持,从API version 9开始废弃,建议使用[getSetting()](#inputmethodgetsetting9)替代。 494 495**系统能力:** SystemCapability.MiscServices.InputMethodFramework 496 497**返回值:** 498 499| 类型 | 说明 | 500| ----------------------------------------- | ---------------------------- | 501| [InputMethodSetting](#inputmethodsetting8) | 回调返回当前客户端设置实例。 | 502 503**示例:** 504 505```ts 506let inputMethodSetting = inputMethod.getInputMethodSetting(); 507``` 508 509## TextInputType<sup>10+</sup> 510 511文本输入类型。 512 513**系统能力:** SystemCapability.MiscServices.InputMethodFramework 514 515| 名称 | 值 |说明 | 516| -------- | -------- |-------- | 517| NONE | -1 |NONE。 | 518| TEXT | 0 |文本类型。 | 519| MULTILINE | 1 |多行类型。 | 520| NUMBER | 2 |数字类型。 | 521| PHONE | 3 |电话号码类型。 | 522| DATETIME | 4 |日期类型。 | 523| EMAIL_ADDRESS | 5 |邮箱地址类型。 | 524| URL | 6 |链接类型。 | 525| VISIBLE_PASSWORD | 7 |密码类型。 | 526 527## EnterKeyType<sup>10+</sup> 528 529Enter键的功能类型。 530 531**系统能力:** SystemCapability.MiscServices.InputMethodFramework 532 533| 名称 | 值 |说明 | 534| -------- | -------- |-------- | 535| UNSPECIFIED | 0 |未指定。 | 536| NONE | 1 |NONE。 | 537| GO | 2 |前往。 | 538| SEARCH | 3 |查找。 | 539| SEND | 4 |发送。 | 540| NEXT | 5 |下一步。 | 541| DONE | 6 |完成。 | 542| PREVIOUS | 7 |上一步。 | 543 544## KeyboardStatus<sup>10+</sup> 545 546输入法软键盘状态。 547 548**系统能力:** SystemCapability.MiscServices.InputMethodFramework 549 550| 名称 | 值 |说明 | 551| -------- | -------- |-------- | 552| NONE | 0 |NONE。 | 553| HIDE | 1 |隐藏状态。 | 554| SHOW | 2 |显示状态。 | 555 556## Direction<sup>10+</sup> 557 558光标移动方向。 559 560**系统能力:** SystemCapability.MiscServices.InputMethodFramework 561 562| 名称 | 值 |说明 | 563| -------- | -------- |-------- | 564| CURSOR_UP | 1 |向上。 | 565| CURSOR_DOWN | 2 |向下。 | 566| CURSOR_LEFT | 3 |向左。 | 567| CURSOR_RIGHT | 4 |向右。 | 568 569## ExtendAction<sup>10+</sup> 570 571编辑框中文本的扩展编辑操作类型,如剪切、复制等。 572 573**系统能力:** SystemCapability.MiscServices.InputMethodFramework 574 575| 名称 | 值 |说明 | 576| -------- | -------- |-------- | 577| SELECT_ALL | 0 |全选。 | 578| CUT | 3 |剪切。 | 579| COPY | 4 |复制。 | 580| PASTE | 5 |粘贴。 | 581 582## FunctionKey<sup>10+</sup> 583 584输入法功能键类型。 585 586**系统能力:** SystemCapability.MiscServices.InputMethodFramework 587 588| 名称 | 类型 | 可读 | 可写 | 说明 | 589| -------- | -------- | -------- | -------- | -------- | 590| enterKeyType<sup>10+</sup> | [EnterKeyType](#enterkeytype10) | 是 | 是 | 输入法enter键类型。| 591 592## InputAttribute<sup>10+</sup> 593 594编辑框属性,包含文本输入类型和Enter键功能类型。 595 596**系统能力:** SystemCapability.MiscServices.InputMethodFramework 597 598| 名称 | 类型 | 可读 | 可写 | 说明 | 599| -------- | -------- | -------- | -------- | -------- | 600| textInputType<sup>10+</sup> | [TextInputType](#textinputtype10) | 是 | 是 | 文本输入类型。| 601| enterKeyType<sup>10+</sup> | [EnterKeyType](#enterkeytype10) | 是 | 是 | Enter键功能类型。| 602 603## TextConfig<sup>10+</sup> 604 605编辑框的配置信息。 606 607**系统能力:** SystemCapability.MiscServices.InputMethodFramework 608 609| 名称 | 类型 | 只读 | 必填 | 说明 | 610| -------- | -------- | -------- | -------- | -------- | 611| inputAttribute<sup>10+</sup> | [InputAttribute](#inputattribute10) | 否 | 是 | 编辑框属性。| 612| cursorInfo<sup>10+</sup> | [CursorInfo](#cursorinfo10) | 否 | 否 | 光标信息。| 613| selection<sup>10+</sup> | [Range](#range10) | 否 | 否 | 文本选中的范围。| 614| windowId<sup>10+</sup> | number | 否 | 否 | 编辑框所在的窗口Id。| 615 616## CursorInfo<sup>10+</sup> 617 618光标信息。 619 620**系统能力:** SystemCapability.MiscServices.InputMethodFramework 621 622| 名称 | 类型 | 可读 | 可写 | 说明 | 623| -------- | -------- | -------- | -------- | -------- | 624| left | number | 是 | 是 | 光标的left坐标。| 625| top | number | 是 | 是 | 光标的top坐标。| 626| width | number | 是 | 是 | 光标的宽度。| 627| height | number | 是 | 是 | 光标的高度。| 628 629## Range<sup>10+</sup> 630 631文本的选中范围。 632 633**系统能力:** SystemCapability.MiscServices.InputMethodFramework 634 635| 名称 | 类型 | 可读 | 可写 | 说明 | 636| -------- | -------- | -------- | -------- | -------- | 637| start | number | 是 | 是 | 选中文本的首字符在编辑框的索引值。| 638| end | number | 是 | 是 | 选中文本的末字符在编辑框的索引值。| 639 640## Movement<sup>10+</sup> 641 642选中文本时,光标移动的方向。 643 644**系统能力:** SystemCapability.MiscServices.InputMethodFramework 645 646| 名称 | 类型 | 可读 | 可写 | 说明 | 647| -------- | -------- | -------- | -------- | -------- | 648| direction | [Direction](#direction10) | 是 | 是 | 选中文本时,光标的移动方向。| 649 650## InputWindowInfo<sup>10+</sup> 651 652输入法软键盘的窗口信息。 653 654**系统能力:** SystemCapability.MiscServices.InputMethodFramework 655 656| 名称 | 类型 | 可读 | 可写 | 说明 | 657| -------- | -------- | -------- | -------- | -------- | 658| name | string | 是 | 是 | 输入法窗口的名称。| 659| left | number | 是 | 是 | 输入法窗口左上顶点的横坐标,单位为px。| 660| top | number | 是 | 是 | 输入法窗口左上顶点的纵坐标,单位为px。| 661| width | number | 是 | 是 | 输入法窗口的宽度,单位为px。| 662| height | number | 是 | 是 | 输入法窗口的高度,单位为px。| 663 664## InputMethodController 665 666下列API示例中都需使用[getController](#inputmethodgetcontroller9)获取到InputMethodController实例,再通过实例调用对应方法。 667 668### attach<sup>10+</sup> 669 670attach(showKeyboard: boolean, textConfig: TextConfig, callback: AsyncCallback<void>): void 671 672自绘控件绑定输入法。使用callback异步回调。 673 674> **说明** 675> 676> 需要先调用此接口,完成自绘控件与输入法的绑定,才能使用以下功能:显示/隐藏键盘、更新光标信息、更改编辑框选中范围、保存配置信息、监听处理由输入法应用发送的信息或命令等。 677 678**系统能力:** SystemCapability.MiscServices.InputMethodFramework 679 680**参数:** 681 682| 参数名 | 类型 | 必填 | 说明 | 683| -------- | -------- | -------- | -------- | 684| showKeyboard | boolean | 是 | 绑定输入法成功后,是否拉起输入法键盘。<br>- ture表示拉起,false表示不拉起。 | 685| textConfig | [TextConfig](#textconfig10) | 是 | 编辑框的配置信息。 | 686| callback | AsyncCallback<void> | 是 | 回调函数。当绑定输入法成功后,err为undefined;否则为错误对象。 | 687 688**错误码:** 689 690以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 691 692| 错误码ID | 错误信息 | 693| -------- | -------------------------------------- | 694| 12800003 | input method client error. | 695| 12800008 | input method manager service error. | 696 697**示例:** 698 699```ts 700try { 701 let textConfig: inputMethod.TextConfig = { 702 inputAttribute: { 703 textInputType: 0, 704 enterKeyType: 1 705 } 706 }; 707 inputMethodController.attach(true, textConfig, (err: BusinessError) => { 708 if (err) { 709 console.error(`Failed to attach: ${JSON.stringify(err)}`); 710 return; 711 } 712 console.log('Succeeded in attaching the inputMethod.'); 713 }); 714} catch(err) { 715 console.error(`Failed to attach: ${JSON.stringify(err)}`); 716} 717``` 718 719### attach<sup>10+</sup> 720 721attach(showKeyboard: boolean, textConfig: TextConfig): Promise<void> 722 723自绘控件绑定输入法。使用promise异步回调。 724 725> **说明** 726> 727> 需要先调用此接口,完成自绘控件与输入法的绑定,才能使用以下功能:显示/隐藏键盘、更新光标信息、更改编辑框选中范围、保存配置信息、监听处理由输入法应用发送的信息或命令等。 728 729**系统能力:** SystemCapability.MiscServices.InputMethodFramework 730 731**参数:** 732 733| 参数名 | 类型 | 必填 | 说明 | 734| -------- | -------- | -------- | -------- | 735| showKeyboard | boolean | 是 | 绑定输入法成功后,是否拉起输入法键盘。<br>- ture表示拉起,false表示不拉起。| 736| textConfig | [TextConfig](#textconfig10) | 是 | 编辑框的配置信息。 | 737 738**返回值:** 739 740| 类型 | 说明 | 741| -------- | -------- | 742| Promise<void> | 无返回结果的Promise对象。 | 743 744**错误码:** 745 746以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 747 748| 错误码ID | 错误信息 | 749| -------- | -------------------------------------- | 750| 12800003 | input method client error. | 751| 12800008 | input method manager service error. | 752 753**示例:** 754 755```ts 756try { 757 let textConfig: inputMethod.TextConfig = { 758 inputAttribute: { 759 textInputType: 0, 760 enterKeyType: 1 761 } 762 }; 763 inputMethodController.attach(true, textConfig).then(() => { 764 console.log('Succeeded in attaching inputMethod.'); 765 }).catch((err: BusinessError) => { 766 console.error(`Failed to attach: ${JSON.stringify(err)}`); 767 }) 768} catch(err) { 769 console.error(`Failed to attach: ${JSON.stringify(err)}`); 770} 771``` 772 773### showTextInput<sup>10+</sup> 774 775showTextInput(callback: AsyncCallback<void>): void 776 777进入文本编辑状态。使用callback异步回调。 778 779> **说明** 780> 781> 编辑框与输入法绑定成功后,可调用该接口拉起软键盘,进入文本编辑状态。 782 783**系统能力:** SystemCapability.MiscServices.InputMethodFramework 784 785**参数:** 786 787| 参数名 | 类型 | 必填 | 说明 | 788| -------- | -------- | -------- | -------- | 789| callback | AsyncCallback<void> | 是 | 回调函数。若成功进入编辑状态,err为undefined;否则为错误对象。 | 790 791**错误码:** 792 793以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 794 795| 错误码ID | 错误信息 | 796| -------- | -------------------------------------- | 797| 12800003 | input method client error. | 798| 12800008 | input method manager service error. | 799| 12800009 | input method client is detached. | 800 801**示例:** 802 803```ts 804inputMethodController.showTextInput((err: BusinessError) => { 805 if (err) { 806 console.error(`Failed to showTextInput: ${JSON.stringify(err)}`); 807 return; 808 } 809 console.log('Succeeded in showing the inputMethod.'); 810}); 811``` 812 813### showTextInput<sup>10+</sup> 814 815showTextInput(): Promise<void> 816 817进入文本编辑状态。使用promise异步回调。 818 819> **说明** 820> 821> 编辑框与输入法绑定成功后,可调用该接口拉起软键盘,进入文本编辑状态。 822 823**系统能力:** SystemCapability.MiscServices.InputMethodFramework 824 825**返回值:** 826 827| 类型 | 说明 | 828| -------- | -------- | 829| Promise<void> | 无返回结果的Promise对象。 | 830 831**错误码:** 832 833以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 834 835| 错误码ID | 错误信息 | 836| -------- | -------------------------------------- | 837| 12800003 | input method client error. | 838| 12800008 | input method manager service error. | 839| 12800009 | input method client is detached. | 840 841**示例:** 842 843```ts 844inputMethodController.showTextInput().then(() => { 845 console.log('Succeeded in showing text input.'); 846}).catch((err: BusinessError) => { 847 console.error(`Failed to showTextInput: ${JSON.stringify(err)}`); 848}); 849``` 850 851### hideTextInput<sup>10+</sup> 852 853hideTextInput(callback: AsyncCallback<void>): void 854 855退出文本编辑状态。使用callback异步回调。 856 857> **说明** 858> 859> 调用接口时,若软键盘处于显示状态,调用接口后软键盘会被隐藏。 860> 861> 调用该接口不会解除与输入法的绑定,再次调用[showTextInput](#showtextinput10)时,可重新进入文本编辑状态。 862 863**系统能力:** SystemCapability.MiscServices.InputMethodFramework 864 865**参数:** 866 867| 参数名 | 类型 | 必填 | 说明 | 868| -------- | -------- | -------- | -------- | 869| callback | AsyncCallback<void> | 是 | 回调函数。当成功退出编辑状态时,err为undefined;否则为错误对象。 | 870 871**错误码:** 872 873以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 874 875| 错误码ID | 错误信息 | 876| -------- | -------------------------------------- | 877| 12800003 | input method client error. | 878| 12800008 | input method manager service error. | 879| 12800009 | input method client is detached. | 880 881**示例:** 882 883```ts 884inputMethodController.hideTextInput((err: BusinessError) => { 885 if (err) { 886 console.error(`Failed to hideTextInput: ${JSON.stringify(err)}`); 887 return; 888 } 889 console.log('Succeeded in hiding text input.'); 890}); 891``` 892 893### hideTextInput<sup>10+</sup> 894 895hideTextInput(): Promise<void> 896 897退出文本编辑状态。使用promise异步回调。 898 899> **说明** 900> 901> 调用接口时,若软键盘处于显示状态,调用接口后软键盘会被隐藏。 902> 903> 调用该接口不会解除与输入法的绑定,再次调用[showTextInput](#showtextinput10)时,可重新进入文本编辑状态。 904 905**系统能力:** SystemCapability.MiscServices.InputMethodFramework 906 907**返回值:** 908 909| 类型 | 说明 | 910| -------- | -------- | 911| Promise<void> | 无返回结果的Promise对象。 | 912 913**错误码:** 914 915以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 916 917| 错误码ID | 错误信息 | 918| -------- | -------------------------------------- | 919| 12800003 | input method client error. | 920| 12800008 | input method manager service error. | 921| 12800009 | input method client is detached. | 922 923**示例:** 924 925```ts 926inputMethodController.hideTextInput().then(() => { 927 console.log('Succeeded in hiding inputMethod.'); 928}).catch((err: BusinessError) => { 929 console.error(`Failed to hideTextInput: ${JSON.stringify(err)}`); 930}) 931``` 932 933### detach<sup>10+</sup> 934 935detach(callback: AsyncCallback<void>): void 936 937自绘控件解除与输入法的绑定。使用callback异步回调。 938 939**系统能力:** SystemCapability.MiscServices.InputMethodFramework 940 941**参数:** 942 943| 参数名 | 类型 | 必填 | 说明 | 944| -------- | -------- | -------- | -------- | 945| callback | AsyncCallback<void> | 是 | 回调函数。当解绑定输入法成功时,err为undefined;否则为错误对象。 | 946 947**错误码:** 948 949以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 950 951| 错误码ID | 错误信息 | 952| -------- | -------------------------------------- | 953| 12800003 | input method client error. | 954| 12800008 | input method manager service error. | 955 956**示例:** 957 958```ts 959inputMethodController.detach((err: BusinessError) => { 960 if (err) { 961 console.error(`Failed to detach: ${JSON.stringify(err)}`); 962 return; 963 } 964 console.log('Succeeded in detaching inputMethod.'); 965}); 966``` 967 968### detach<sup>10+</sup> 969 970detach(): Promise<void> 971 972自绘控件解除与输入法的绑定。使用promise异步回调。 973 974**系统能力:** SystemCapability.MiscServices.InputMethodFramework 975 976**返回值:** 977 978| 类型 | 说明 | 979| -------- | -------- | 980| Promise<void> | 无返回结果的Promise对象。 | 981 982**错误码:** 983 984以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 985 986| 错误码ID | 错误信息 | 987| -------- | -------------------------------------- | 988| 12800003 | input method client error. | 989| 12800008 | input method manager service error. | 990 991**示例:** 992 993```ts 994inputMethodController.detach().then(() => { 995 console.log('Succeeded in detaching inputMethod.'); 996}).catch((err: BusinessError) => { 997 console.error(`Failed to detach: ${JSON.stringify(err)}`); 998}); 999``` 1000 1001### setCallingWindow<sup>10+</sup> 1002 1003setCallingWindow(windowId: number, callback: AsyncCallback<void>): void 1004 1005设置要避让软键盘的窗口。使用callback异步回调。 1006 1007> **说明** 1008> 1009> 将绑定到输入法的应用程序所在的窗口Id传入,此窗口可以避让输入法窗口。 1010 1011**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1012 1013**参数:** 1014 1015| 参数名 | 类型 | 必填 | 说明 | 1016| -------- | -------- | -------- | -------- | 1017| windowId | number | 是 | 绑定输入法应用的应用程序所在的窗口Id。 | 1018| callback | AsyncCallback<void> | 是 | 回调函数。当设置成功时,err为undefined;否则为错误对象。 | 1019 1020**错误码:** 1021 1022以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1023 1024| 错误码ID | 错误信息 | 1025| -------- | -------------------------------------- | 1026| 12800003 | input method client error. | 1027| 12800008 | input method manager service error. | 1028| 12800009 | input method client is detached. | 1029 1030**示例:** 1031 1032```ts 1033try { 1034 let windowId: number = 2000; 1035 inputMethodController.setCallingWindow(windowId, (err: BusinessError) => { 1036 if (err) { 1037 console.error(`Failed to setCallingWindow: ${JSON.stringify(err)}`); 1038 return; 1039 } 1040 console.log('Succeeded in setting callingWindow.'); 1041 }); 1042} catch(err) { 1043 console.error(`Failed to setCallingWindow: ${JSON.stringify(err)}`); 1044} 1045``` 1046 1047### setCallingWindow<sup>10+</sup> 1048 1049setCallingWindow(windowId: number): Promise<void> 1050 1051设置要避让软键盘的窗口。使用promise异步回调。 1052 1053> **说明** 1054> 1055> 将绑定到输入法的应用程序所在的窗口Id传入,此窗口可以避让输入法窗口。 1056 1057**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1058 1059**参数:** 1060 1061| 参数名 | 类型 | 必填 | 说明 | 1062| -------- | -------- | -------- | -------- | 1063| windowId | number | 是 | 绑定输入法应用的应用程序所在的窗口Id。 | 1064 1065**返回值:** 1066 1067| 类型 | 说明 | 1068| -------- | -------- | 1069| Promise<void> | 无返回结果的Promise对象。 | 1070 1071**错误码:** 1072 1073以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1074 1075| 错误码ID | 错误信息 | 1076| -------- | -------------------------------------- | 1077| 12800003 | input method client error. | 1078| 12800008 | input method manager service error. | 1079| 12800009 | input method client is detached. | 1080 1081**示例:** 1082 1083```ts 1084try { 1085 let windowId: number = 2000; 1086 inputMethodController.setCallingWindow(windowId).then(() => { 1087 console.log('Succeeded in setting callingWindow.'); 1088 }).catch((err: BusinessError) => { 1089 console.error(`Failed to setCallingWindow: ${JSON.stringify(err)}`); 1090 }) 1091} catch(err) { 1092 console.error(`Failed to setCallingWindow: ${JSON.stringify(err)}`); 1093} 1094``` 1095 1096### updateCursor<sup>10+</sup> 1097 1098updateCursor(cursorInfo: CursorInfo, callback: AsyncCallback<void>): void 1099 1100更新当前编辑框内的光标信息。当光标信息发生变化时,调用该接口使输入法感知到光标变化。使用callback异步回调。 1101 1102**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1103 1104**参数:** 1105 1106| 参数名 | 类型 | 必填 | 说明 | 1107| -------- | -------- | -------- | -------- | 1108| cursorInfo | [CursorInfo](#cursorinfo10) | 是 | 光标信息。 | 1109| callback | AsyncCallback<void> | 是 | 回调函数。当光标信息更新成功时,err为undefined;否则为错误对象。 | 1110 1111**错误码:** 1112 1113以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1114 1115| 错误码ID | 错误信息 | 1116| -------- | -------------------------------------- | 1117| 12800003 | input method client error. | 1118| 12800008 | input method manager service error. | 1119| 12800009 | input method client is detached. | 1120 1121**示例:** 1122 1123```ts 1124try { 1125 let cursorInfo: inputMethod.CursorInfo = { left: 0, top: 0, width: 600, height: 800 }; 1126 inputMethodController.updateCursor(cursorInfo, (err: BusinessError) => { 1127 if (err) { 1128 console.error(`Failed to updateCursor: ${JSON.stringify(err)}`); 1129 return; 1130 } 1131 console.log('Succeeded in updating cursorInfo.'); 1132 }); 1133} catch(err) { 1134 console.error(`Failed to updateCursor: ${JSON.stringify(err)}`); 1135} 1136``` 1137 1138### updateCursor<sup>10+</sup> 1139 1140updateCursor(cursorInfo: CursorInfo): Promise<void> 1141 1142更新当前编辑框内的光标信息。当光标信息发生变化时,调用该接口使输入法感知到光标变化。使用promise异步回调。 1143 1144**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1145 1146**参数:** 1147 1148| 参数名 | 类型 | 必填 | 说明 | 1149| -------- | -------- | -------- | -------- | 1150| cursorInfo | [CursorInfo](#cursorinfo10) | 是 | 光标信息。 | 1151 1152**返回值:** 1153 1154| 类型 | 说明 | 1155| -------- | -------- | 1156| Promise<void> | 无返回结果的Promise对象。 | 1157 1158**错误码:** 1159 1160以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1161 1162| 错误码ID | 错误信息 | 1163| -------- | -------------------------------------- | 1164| 12800003 | input method client error. | 1165| 12800008 | input method manager service error. | 1166| 12800009 | input method client is detached. | 1167 1168**示例:** 1169 1170```ts 1171try { 1172 let cursorInfo: inputMethod.CursorInfo = { left: 0, top: 0, width: 600, height: 800 }; 1173 inputMethodController.updateCursor(cursorInfo).then(() => { 1174 console.log('Succeeded in updating cursorInfo.'); 1175 }).catch((err: BusinessError) => { 1176 console.error(`Failed to updateCursor: ${JSON.stringify(err)}`); 1177 }) 1178} catch(err) { 1179 console.error(`Failed to updateCursor: ${JSON.stringify(err)}`); 1180} 1181``` 1182 1183### changeSelection<sup>10+</sup> 1184 1185changeSelection(text: string, start: number, end: number, callback: AsyncCallback<void>): void 1186 1187更新当前编辑框内被选中的文本信息。当选中的文本内容或文本范围发生变化时,可调用该接口更新文本信息,使输入法应用感知到变化。使用callback异步回调。 1188 1189**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1190 1191**参数:** 1192 1193| 参数名 | 类型 | 必填 | 说明 | 1194| -------- | -------- | -------- | -------- | 1195| text | string | 是 | 整个输入文本。 | 1196| start | number | 是 | 所选文本的起始位置。 | 1197| end | number | 是 | 所选文本的结束位置。 | 1198| callback | AsyncCallback<void> | 是 | 回调函数。当文本信息更新成功时,err为undefined;否则为错误对象。 | 1199 1200**错误码:** 1201 1202以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1203 1204| 错误码ID | 错误信息 | 1205| -------- | -------------------------------------- | 1206| 12800003 | input method client error. | 1207| 12800008 | input method manager service error. | 1208| 12800009 | input method client is detached. | 1209 1210**示例:** 1211 1212```ts 1213try { 1214 inputMethodController.changeSelection('text', 0, 5, (err: BusinessError) => { 1215 if (err) { 1216 console.error(`Failed to changeSelection: ${JSON.stringify(err)}`); 1217 return; 1218 } 1219 console.log('Succeeded in changing selection.'); 1220 }); 1221} catch(err) { 1222 console.error(`Failed to changeSelection: ${JSON.stringify(err)}`); 1223} 1224``` 1225 1226### changeSelection<sup>10+</sup> 1227 1228changeSelection(text: string, start: number, end: number): Promise<void> 1229 1230更新当前编辑框内被选中的文本信息。当选中的文本内容或文本范围发生变化时,可调用该接口更新文本信息,使输入法应用感知到变化。使用promise异步回调。 1231 1232**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1233 1234**参数:** 1235 1236| 参数名 | 类型 | 必填 | 说明 | 1237| -------- | -------- | -------- | -------- | 1238| text | string | 是 | 整个输入文本。 | 1239| start | number | 是 | 所选文本的起始位置。 | 1240| end | number | 是 | 所选文本的结束位置。 | 1241 1242**返回值:** 1243 1244| 类型 | 说明 | 1245| -------- | -------- | 1246| Promise<void> | 无返回结果的Promise对象。 | 1247 1248**错误码:** 1249 1250以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1251 1252| 错误码ID | 错误信息 | 1253| -------- | -------------------------------------- | 1254| 12800003 | input method client error. | 1255| 12800008 | input method manager service error. | 1256| 12800009 | input method client is detached. | 1257 1258**示例:** 1259 1260```ts 1261try { 1262 inputMethodController.changeSelection('test', 0, 5).then(() => { 1263 console.log('Succeeded in changing selection.'); 1264 }).catch((err: BusinessError) => { 1265 console.error(`Failed to changeSelection: ${JSON.stringify(err)}`); 1266 }) 1267} catch(err) { 1268 console.error(`Failed to changeSelection: ${JSON.stringify(err)}`); 1269} 1270``` 1271 1272### updateAttribute<sup>10+</sup> 1273 1274updateAttribute(attribute: InputAttribute, callback: AsyncCallback<void>): void 1275 1276更新编辑框属性信息。使用callback异步回调。 1277 1278**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1279 1280**参数:** 1281 1282| 参数名 | 类型 | 必填 | 说明 | 1283| -------- | -------- | -------- | -------- | 1284| attribute | [InputAttribute](#inputattribute10) | 是 | 编辑框属性对象。 | 1285| callback | AsyncCallback<void> | 是 | 回调函数。当编辑框属性信息更新成功时,err为undefined;否则为错误对象。 | 1286 1287**错误码:** 1288 1289以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1290 1291| 错误码ID | 错误信息 | 1292| -------- | -------------------------------------- | 1293| 12800003 | input method client error. | 1294| 12800008 | input method manager service error. | 1295| 12800009 | input method client is detached. | 1296 1297**示例:** 1298 1299```ts 1300try { 1301 let inputAttribute: inputMethod.InputAttribute = { textInputType: 0, enterKeyType: 1 }; 1302 inputMethodController.updateAttribute(inputAttribute, (err: BusinessError) => { 1303 if (err) { 1304 console.error(`Failed to updateAttribute: ${JSON.stringify(err)}`); 1305 return; 1306 } 1307 console.log('Succeeded in updating attribute.'); 1308 }); 1309} catch(err) { 1310 console.error(`Failed to updateAttribute: ${JSON.stringify(err)}`); 1311} 1312``` 1313 1314### updateAttribute<sup>10+</sup> 1315 1316updateAttribute(attribute: InputAttribute): Promise<void> 1317 1318更新编辑框属性信息。使用promise异步回调。 1319 1320**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1321 1322**参数:** 1323 1324| 参数名 | 类型 | 必填 | 说明 | 1325| -------- | -------- | -------- | -------- | 1326| attribute | [InputAttribute](#inputattribute10) | 是 | 编辑框属性对象。 | 1327 1328**返回值:** 1329 1330| 类型 | 说明 | 1331| -------- | -------- | 1332| Promise<void> | 无返回结果的Promise对象。 | 1333 1334**错误码:** 1335 1336以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1337 1338| 错误码ID | 错误信息 | 1339| -------- | -------------------------------------- | 1340| 12800003 | input method client error. | 1341| 12800008 | input method manager service error. | 1342| 12800009 | input method client is detached. | 1343 1344**示例:** 1345 1346```ts 1347try { 1348 let inputAttribute: inputMethod.InputAttribute = { textInputType: 0, enterKeyType: 1 }; 1349 inputMethodController.updateAttribute(inputAttribute).then(() => { 1350 console.log('Succeeded in updating attribute.'); 1351 }).catch((err: BusinessError) => { 1352 console.error(`Failed to updateAttribute: ${JSON.stringify(err)}`); 1353 }) 1354} catch(err) { 1355 console.error(`Failed to updateAttribute: ${JSON.stringify(err)}`); 1356} 1357``` 1358 1359### stopInputSession<sup>9+</sup> 1360 1361stopInputSession(callback: AsyncCallback<boolean>): void 1362 1363结束输入会话。使用callback异步回调。 1364 1365> **说明:** 1366> 1367> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用该接口结束输入会话。 1368 1369**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1370 1371**参数:** 1372 1373| 参数名 | 类型 | 必填 | 说明 | 1374| -------- | -------- | -------- | -------- | 1375| callback | AsyncCallback<boolean> | 是 | 回调函数。当结束输入会话成功时,err为undefined,data为true;否则为错误对象。 | 1376 1377**错误码:** 1378 1379以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1380 1381| 错误码ID | 错误信息 | 1382| -------- | -------------------------------------- | 1383| 12800003 | input method client error. | 1384| 12800008 | input method manager service error. | 1385 1386**示例:** 1387 1388```ts 1389try { 1390 inputMethodController.stopInputSession((err: BusinessError, result: boolean) => { 1391 if (err) { 1392 console.error(`Failed to stopInputSession: ${JSON.stringify(err)}`); 1393 return; 1394 } 1395 if (result) { 1396 console.log('Succeeded in stopping inputSession.'); 1397 } else { 1398 console.error('Failed to stopInputSession.'); 1399 } 1400 }); 1401} catch(err) { 1402 console.error(`Failed to stopInputSession: ${JSON.stringify(err)}`); 1403} 1404``` 1405 1406### stopInputSession<sup>9+</sup> 1407 1408stopInputSession(): Promise<boolean> 1409 1410结束输入会话。使用promise异步回调。 1411 1412> **说明:** 1413> 1414> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用该接口结束输入会话。 1415 1416**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1417 1418**返回值:** 1419 1420| 类型 | 说明 | 1421| -------- | -------- | 1422| Promise<boolean> | Promise对象。返回true表示结束输入会话成功,返回false表示结束输入会话失败。 | 1423 1424**错误码:** 1425 1426以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1427 1428| 错误码ID | 错误信息 | 1429| -------- | -------------------------------------- | 1430| 12800003 | input method client error. | 1431| 12800008 | input method manager service error. | 1432 1433**示例:** 1434 1435```ts 1436try { 1437 inputMethodController.stopInputSession().then((result: boolean) => { 1438 if (result) { 1439 console.log('Succeeded in stopping inputSession.'); 1440 } else { 1441 console.error('Failed to stopInputSession.'); 1442 } 1443 }).catch((err: BusinessError) => { 1444 console.error(`Failed to stopInputSession: ${JSON.stringify(err)}`); 1445 }) 1446} catch(err) { 1447 console.error(`Failed to stopInputSession: ${JSON.stringify(err)}`); 1448} 1449``` 1450 1451### showSoftKeyboard<sup>9+</sup> 1452 1453showSoftKeyboard(callback: AsyncCallback<void>): void 1454 1455显示输入法软键盘。使用callback异步回调。 1456 1457> **说明:** 1458> 1459> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用显示当前输入法的软键盘。 1460 1461**需要权限:** ohos.permission.CONNECT_IME_ABILITY,仅系统应用可用。 1462 1463**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1464 1465**参数:** 1466 1467| 参数名 | 类型 | 必填 | 说明 | 1468| -------- | ------------------------- | ---- | ---------- | 1469| callback | AsyncCallback<void> | 是 | 回调函数。当软键盘显示成功。err为undefined,否则为错误对象。 | 1470 1471**错误码:** 1472 1473以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1474 1475| 错误码ID | 错误信息 | 1476| -------- | -------------------------------------- | 1477| 12800003 | input method client error. | 1478| 12800008 | input method manager service error. | 1479 1480**示例:** 1481 1482```ts 1483inputMethodController.showSoftKeyboard((err: BusinessError) => { 1484 if (!err) { 1485 console.log('Succeeded in showing softKeyboard.'); 1486 } else { 1487 console.error(`Failed to show softKeyboard: ${JSON.stringify(err)}`); 1488 } 1489}) 1490``` 1491 1492### showSoftKeyboard<sup>9+</sup> 1493 1494showSoftKeyboard(): Promise<void> 1495 1496显示输入法软键盘。使用Promise异步回调。 1497 1498> **说明:** 1499> 1500> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用显示当前输入法的软键盘。 1501 1502**需要权限:** ohos.permission.CONNECT_IME_ABILITY,仅系统应用可用。 1503 1504**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1505 1506**返回值:** 1507 1508| 类型 | 说明 | 1509| ------------------- | ------------------------- | 1510| Promise<void> | 无返回结果的Promise对象。 | 1511 1512**错误码:** 1513 1514以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1515 1516| 错误码ID | 错误信息 | 1517| -------- | -------------------------------------- | 1518| 12800003 | input method client error. | 1519| 12800008 | input method manager service error. | 1520 1521**示例:** 1522 1523```ts 1524inputMethodController.showSoftKeyboard().then(() => { 1525 console.log('Succeeded in showing softKeyboard.'); 1526}).catch((err: BusinessError) => { 1527 console.error(`Failed to show softKeyboard: ${JSON.stringify(err)}`); 1528}); 1529``` 1530 1531### hideSoftKeyboard<sup>9+</sup> 1532 1533hideSoftKeyboard(callback: AsyncCallback<void>): void 1534 1535隐藏输入法软键盘。使用callback异步回调。 1536 1537> **说明:** 1538> 1539> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用隐藏当前输入法的软键盘。 1540 1541**需要权限:** ohos.permission.CONNECT_IME_ABILITY,仅系统应用可用。 1542 1543**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1544 1545**参数:** 1546 1547| 参数名 | 类型 | 必填 | 说明 | 1548| -------- | ------------------------- | ---- | ---------- | 1549| callback | AsyncCallback<void> | 是 | 回调函数。当软键盘隐藏成功。err为undefined,否则为错误对象。 | 1550 1551**错误码:** 1552 1553以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1554 1555| 错误码ID | 错误信息 | 1556| -------- | -------------------------------------- | 1557| 12800003 | input method client error. | 1558| 12800008 | input method manager service error. | 1559 1560**示例:** 1561 1562```ts 1563inputMethodController.hideSoftKeyboard((err: BusinessError) => { 1564 if (!err) { 1565 console.log('Succeeded in hiding softKeyboard.'); 1566 } else { 1567 console.error(`Failed to hide softKeyboard: ${JSON.stringify(err)}`); 1568 } 1569}) 1570``` 1571 1572### hideSoftKeyboard<sup>9+</sup> 1573 1574hideSoftKeyboard(): Promise<void> 1575 1576隐藏输入法软键盘。使用Promise异步回调。 1577 1578> **说明:** 1579> 1580> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用隐藏当前输入法的软键盘。 1581 1582**需要权限:** ohos.permission.CONNECT_IME_ABILITY,仅系统应用可用。 1583 1584**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1585 1586**返回值:** 1587 1588| 类型 | 说明 | 1589| ------------------- | ------------------------- | 1590| Promise<void> | 无返回结果的Promise对象。 | 1591 1592**错误码:** 1593 1594以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1595 1596| 错误码ID | 错误信息 | 1597| -------- | -------------------------------------- | 1598| 12800003 | input method client error. | 1599| 12800008 | input method manager service error. | 1600 1601**示例:** 1602 1603```ts 1604inputMethodController.hideSoftKeyboard().then(() => { 1605 console.log('Succeeded in hiding softKeyboard.'); 1606}).catch((err: BusinessError) => { 1607 console.error(`Failed to hide softKeyboard: ${JSON.stringify(err)}`); 1608}); 1609``` 1610 1611### stopInput<sup>(deprecated)</sup> 1612 1613stopInput(callback: AsyncCallback<boolean>): void 1614 1615结束输入会话。使用callback异步回调。 1616 1617> **说明:** 1618> 1619> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用该接口结束输入会话。 1620> 1621> 从API version 6开始支持,从API version 9开始废弃,建议使用[stopInputSession()](#stopinputsession9)替代。 1622 1623**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1624 1625**参数:** 1626 1627| 参数名 | 类型 | 必填 | 说明 | 1628| -------- | -------- | -------- | -------- | 1629| callback | AsyncCallback<boolean> | 是 | 回调函数。当会话结束成功,err为undefined,data为true;否则为错误对象。 | 1630 1631**示例:** 1632 1633```ts 1634inputMethodController.stopInput((err: BusinessError, result: boolean) => { 1635 if (err) { 1636 console.error(`Failed to stopInput: ${JSON.stringify(err)}`); 1637 return; 1638 } 1639 if (result) { 1640 console.log('Succeeded in stopping input.'); 1641 } else { 1642 console.error('Failed to stopInput.'); 1643 } 1644}); 1645``` 1646 1647### stopInput<sup>(deprecated)</sup> 1648 1649stopInput(): Promise<boolean> 1650 1651结束输入会话。使用promise异步回调。 1652 1653> **说明:** 1654> 1655> 该接口需要编辑框与输入法绑定时才能调用,即点击编辑控件后,才可调用该接口结束输入会话。 1656> 1657> 从API version 6开始支持,从API version 9开始废弃,建议使用[stopInputSession()](#stopinputsession9)替代。 1658 1659**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1660 1661**返回值:** 1662 1663| 类型 | 说明 | 1664| -------- | -------- | 1665| Promise<boolean> | Promise对象。返回true表示会话结束成功;返回false表示会话结束失败。 | 1666 1667**示例:** 1668 1669```ts 1670inputMethodController.stopInput().then((result: boolean) => { 1671 if (result) { 1672 console.log('Succeeded in stopping input.'); 1673 } else { 1674 console.error('Failed to stopInput.'); 1675 } 1676}).catch((err: BusinessError) => { 1677 console.error(`Failed to stopInput: ${JSON.stringify(err)}`); 1678}) 1679``` 1680 1681### on('insertText')<sup>10+</sup> 1682 1683on(type: 'insertText', callback: (text: string) => void): void; 1684 1685订阅输入法应用插入文本事件。使用callback异步回调。 1686 1687**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1688 1689**参数:** 1690 1691| 参数名 | 类型 | 必填 | 说明 | 1692| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1693| type | string | 是 | 设置监听类型,固定取值为'insertText'。 | 1694| callback | (text: string) => void | 是 | 回调函数,返回需要插入的文本内容。<br/>根据传入的文本,在回调函数中操作编辑框中的内容。 | 1695 1696**错误码:** 1697 1698以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1699 1700| 错误码ID | 错误信息 | 1701| -------- | -------------------------------------- | 1702| 12800009 | input method client is detached. | 1703 1704**示例:** 1705 1706```ts 1707try { 1708 inputMethodController.on('insertText', (text: string) => { 1709 console.log(`Succeeded in subscribing insertText: ${text}`); 1710 }); 1711} catch(err) { 1712 console.error(`Failed to subscribe insertText: ${JSON.stringify(err)}`); 1713} 1714``` 1715 1716### off('insertText')<sup>10+</sup> 1717 1718off(type: 'insertText', callback?: (text: string) => void): void 1719 1720取消订阅输入法应用插入文本事件。 1721 1722**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1723 1724**参数:** 1725 1726| 参数名 | 类型 | 必填 | 说明 | 1727| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 1728| type | string | 是 | 设置监听类型,固定取值为'insertText'。 | 1729| callback | (text: string) => void | 否 | 取消订阅的回调函数,需要与on接口传入的保持一致。<br/>参数不填写时,取消订阅type对应的所有回调事件。 | 1730 1731**示例:** 1732 1733```ts 1734let onInsertTextCallback = (text: string) => { 1735 console.log(`Succeeded in subscribing insertText: ${text}`); 1736}; 1737inputMethodController.off('insertText', onInsertTextCallback); 1738inputMethodController.off('insertText'); 1739``` 1740 1741### on('deleteLeft')<sup>10+</sup> 1742 1743on(type: 'deleteLeft', callback: (length: number) => void): void 1744 1745订阅输入法应用向左删除事件。使用callback异步回调。 1746 1747**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1748 1749**参数:** 1750 1751| 参数名 | 类型 | 必填 | 说明 | 1752| -------- | ----- | ---- | ----- | 1753| type | string | 是 | 设置监听类型,固定取值为'deleteLeft'。| 1754| callback | (length: number) => void | 是 | 回调函数,返回需要向左删除的文本长度。<br/>根据传入的删除长度,在回调函数中操作编辑框中的文本。 | 1755 1756**错误码:** 1757 1758以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1759 1760| 错误码ID | 错误信息 | 1761| -------- | -------------------------------------- | 1762| 12800009 | input method client is detached. | 1763 1764**示例:** 1765 1766```ts 1767try { 1768 inputMethodController.on('deleteLeft', (length: number) => { 1769 console.log(`Succeeded in subscribing deleteLeft, length: ${length}`); 1770 }); 1771} catch(err) { 1772 console.error(`Failed to subscribe deleteLeft: ${JSON.stringify(err)}`); 1773} 1774``` 1775 1776### off('deleteLeft')<sup>10+</sup> 1777 1778off(type: 'deleteLeft', callback?: (length: number) => void): void 1779 1780取消订阅输入法应用向左删除文本事件。 1781 1782**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1783 1784**参数:** 1785 1786| 参数名 | 类型 | 必填 | 说明 | 1787| -------- | ------------------------ | ---- | ------------------------------------------------------------ | 1788| type | string | 是 | 设置监听,固定取值为'deleteLeft'。 | 1789| callback | (length: number) => void | 否 | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 | 1790 1791**示例:** 1792 1793```ts 1794let onDeleteLeftCallback = (length: number) => { 1795 console.log(`Succeeded in subscribing deleteLeft, length: ${length}`); 1796}; 1797inputMethodController.off('deleteLeft', onDeleteLeftCallback); 1798inputMethodController.off('deleteLeft'); 1799``` 1800 1801### on('deleteRight')<sup>10+</sup> 1802 1803on(type: 'deleteRight', callback: (length: number) => void): void 1804 1805订阅输入法应用向右删除事件。使用callback异步回调。 1806 1807**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1808 1809**参数:** 1810 1811| 参数名 | 类型 | 必填 | 说明 | 1812| -------- | ----- | ---- | ----- | 1813| type | string | 是 | 设置监听类型,固定取值为'deleteRight'。| 1814| callback | (length: number) => void | 是 | 回调函数,返回需要向右删除的文本长度。<br/>根据传入的删除长度,在回调函数中操作编辑框中的文本。 | 1815 1816**错误码:** 1817 1818以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1819 1820| 错误码ID | 错误信息 | 1821| -------- | -------------------------------------- | 1822| 12800009 | input method client is detached. | 1823 1824**示例:** 1825 1826```ts 1827try { 1828 inputMethodController.on('deleteRight', (length: number) => { 1829 console.log(`Succeeded in subscribing deleteRight, length: ${length}`); 1830 }); 1831} catch(err) { 1832 console.error(`Failed to subscribe deleteRight: ${JSON.stringify(err)}`); 1833} 1834``` 1835 1836### off('deleteRight')<sup>10+</sup> 1837 1838off(type: 'deleteRight', callback?: (length: number) => void): void 1839 1840取消订阅输入法应用向右删除文本事件。 1841 1842**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1843 1844**参数:** 1845 1846| 参数名 | 类型 | 必填 | 说明 | 1847| -------- | ------------------------ | ---- | ------------------------------------------------------------ | 1848| type | string | 是 | 设置监听类型,固定取值为`deleteRight`。 | 1849| callback | (length: number) => void | 否 | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 | 1850 1851**示例:** 1852 1853```ts 1854let onDeleteRightCallback = (length: number) => { 1855 console.log(`Succeeded in subscribing deleteRight, length: ${length}`); 1856}; 1857inputMethodController.off('deleteRight', onDeleteRightCallback); 1858inputMethodController.off('deleteRight'); 1859``` 1860 1861### on('sendKeyboardStatus')<sup>10+</sup> 1862 1863on(type: 'sendKeyboardStatus', callback: (keyboardStatus: KeyboardStatus) => void): void 1864 1865订阅输入法应用发送输入法软键盘状态事件。使用callback异步回调。 1866 1867**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1868 1869**参数:** 1870 1871| 参数名 | 类型 | 必填 | 说明 | 1872| -------- | ------ | ---- | ---- | 1873| type | string | 是 | 设置监听类型,固定取值为'sendKeyboardStatus'。 | 1874| callback | (keyboardStatus: [KeyboardStatus](#keyboardstatus10)) => void | 是 | 回调函数,返回软键盘状态。<br/>根据传入的软键盘状态,在回调函数中做相应操作。 | 1875 1876**错误码:** 1877 1878以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1879 1880| 错误码ID | 错误信息 | 1881| -------- | -------------------------------------- | 1882| 12800009 | input method client is detached. | 1883 1884**示例:** 1885 1886```ts 1887try { 1888 inputMethodController.on('sendKeyboardStatus', (keyboardStatus: inputMethod.KeyboardStatus) => { 1889 console.log(`Succeeded in subscribing sendKeyboardStatus, keyboardStatus: ${keyboardStatus}`); 1890 }); 1891} catch(err) { 1892 console.error(`Failed to subscribe sendKeyboardStatus: ${JSON.stringify(err)}`); 1893} 1894``` 1895 1896### off('sendKeyboardStatus')<sup>10+</sup> 1897 1898off(type: 'sendKeyboardStatus', callback?: (keyboardStatus: KeyboardStatus) => void): void 1899 1900取消订阅输入法应用发送软键盘状态事件。 1901 1902**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1903 1904**参数:** 1905 1906| 参数名 | 类型 | 必填 | 说明 | 1907| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1908| type | string | 是 | 设置监听类型,固定取值为'sendKeyboardStatus'。 | 1909| callback | (keyboardStatus: [KeyboardStatus](#keyboardstatus10)) => void | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 1910 1911**示例:** 1912 1913```ts 1914let onSendKeyboardStatus = (keyboardStatus: inputMethod.KeyboardStatus) => { 1915 console.log(`Succeeded in subscribing sendKeyboardStatus, keyboardStatus: ${keyboardStatus}`); 1916}; 1917inputMethodController.off('sendKeyboardStatus', onSendKeyboardStatus); 1918inputMethodController.off('sendKeyboardStatus'); 1919``` 1920 1921### on('sendFunctionKey')<sup>10+</sup> 1922 1923on(type: 'sendFunctionKey', callback: (functionKey: FunctionKey) => void): void 1924 1925订阅输入法应用发送功能键事件。使用callback异步回调。 1926 1927**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1928 1929**参数:** 1930 1931| 参数名 | 类型 | 必填 | 说明 | 1932| -------- | -------- | ---- | ----- | 1933| type | string | 是 | 设置监听类型,固定取值为'sendFunctionKey'。| 1934| callback | (functionKey: [FunctionKey](#functionkey10)) => void | 是 | 回调函数,返回输入法应用发送的功能键信息。<br/>根据返回的功能键信息,做相应操作。 | 1935 1936**错误码:** 1937 1938以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1939 1940| 错误码ID | 错误信息 | 1941| -------- | -------------------------------------- | 1942| 12800009 | input method client is detached. | 1943 1944**示例:** 1945 1946```ts 1947try { 1948 inputMethodController.on('sendFunctionKey', (functionKey: inputMethod.FunctionKey) => { 1949 console.log(`Succeeded in subscribing sendFunctionKey, functionKey.enterKeyType: ${functionKey.enterKeyType}`); 1950 }); 1951} catch(err) { 1952 console.error(`Failed to subscribe sendFunctionKey: ${JSON.stringify(err)}`); 1953} 1954``` 1955 1956### off('sendFunctionKey')<sup>10+</sup> 1957 1958off(type: 'sendFunctionKey', callback?: (functionKey: FunctionKey) => void): void 1959 1960取消订阅输入法应用发送功能键事件。 1961 1962**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1963 1964**参数:** 1965 1966| 参数名 | 类型 | 必填 | 说明 | 1967| -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ | 1968| type | string | 是 | 设置监听类型,固定取值为'sendFunctionKey'。 | 1969| callback | (functionKey: [FunctionKey](#functionkey10)) => void | 否 | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 | 1970 1971**示例:** 1972 1973```ts 1974let onSendFunctionKey = (functionKey: inputMethod.FunctionKey) => { 1975 console.log(`Succeeded in subscribing sendFunctionKey, functionKey: ${functionKey.enterKeyType}`); 1976}; 1977inputMethodController.off('sendFunctionKey', onSendFunctionKey); 1978inputMethodController.off('sendFunctionKey'); 1979``` 1980 1981### on('moveCursor')<sup>10+</sup> 1982 1983on(type: 'moveCursor', callback: (direction: Direction) => void): void 1984 1985订阅输入法应用移动光标事件。使用callback异步回调。 1986 1987**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1988 1989**参数:** 1990 1991| 参数名 | 类型 | 必填 | 说明 | 1992| -------- | ------ | ---- | ------ | 1993| type | string | 是 | 设置监听类型,固定取值为'moveCursor'。 | 1994| callback | callback: (direction: [Direction<sup>10+</sup>](#direction10)) => void | 是 | 回调函数,返回光标信息。<br/>根据返回的光标移动方向,改变光标位置,如光标向上或向下。 | 1995 1996**错误码:** 1997 1998以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 1999 2000| 错误码ID | 错误信息 | 2001| -------- | -------------------------------- | 2002| 12800009 | input method client is detached. | 2003 2004**示例:** 2005 2006```ts 2007try { 2008 inputMethodController.on('moveCursor', (direction: inputMethod.Direction) => { 2009 console.log(`Succeeded in subscribing moveCursor, direction: ${direction}`); 2010 }); 2011} catch(err) { 2012 console.error(`Failed to subscribe moveCursor: ${JSON.stringify(err)}`); 2013} 2014``` 2015 2016### off('moveCursor')<sup>10+</sup> 2017 2018off(type: 'moveCursor', callback?: (direction: Direction) => void): void 2019 2020取消订阅输入法应用移动光标事件。 2021 2022**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2023 2024**参数:** 2025 2026| 参数名 | 类型 | 必填 | 说明 | 2027| ------ | ------ | ---- | ---- | 2028| type | string | 是 | 设置监听类型,固定取值为'moveCursor'。 | 2029| callback | (direction: [Direction<sup>10+</sup>](#direction10)) => void | 否 | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 | 2030 2031**示例:** 2032 2033```ts 2034let onMoveCursorCallback = (direction: inputMethod.Direction) => { 2035 console.log(`Succeeded in subscribing moveCursor, direction: ${direction}`); 2036}; 2037inputMethodController.off('moveCursor', onMoveCursorCallback); 2038inputMethodController.off('moveCursor'); 2039``` 2040 2041### on('handleExtendAction')<sup>10+</sup> 2042 2043on(type: 'handleExtendAction', callback: (action: ExtendAction) => void): void 2044 2045订阅输入法应用发送扩展编辑操作事件。使用callback异步回调。 2046 2047**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2048 2049**参数:** 2050 2051| 参数名 | 类型 | 必填 | 说明 | 2052| -------- | ------ | ---- | -------- | 2053| type | string | 是 | 设置监听类型,固定取值为'handleExtendAction'。 | 2054| callback | callback: (action: [ExtendAction](#extendaction10)) => void | 是 | 回调函数,返回扩展编辑操作类型。<br/>根据传入的扩展编辑操作类型,做相应的操作,如剪切、复制等。| 2055 2056**错误码:** 2057 2058以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 2059 2060| 错误码ID | 错误信息 | 2061| -------- | -------------------------------------- | 2062| 12800009 | input method client is detached. | 2063 2064**示例:** 2065 2066```ts 2067try { 2068 inputMethodController.on('handleExtendAction', (action: inputMethod.ExtendAction) => { 2069 console.log(`Succeeded in subscribing handleExtendAction, action: ${action}`); 2070 }); 2071} catch(err) { 2072 console.error(`Failed to subscribe handleExtendAction: ${JSON.stringify(err)}`); 2073} 2074``` 2075 2076### off('handleExtendAction')<sup>10+</sup> 2077 2078off(type: 'handleExtendAction', callback?: (action: ExtendAction) => void): void 2079 2080取消订阅输入法应用发送扩展编辑操作事件。使用callback异步回调。 2081 2082**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2083 2084**参数:** 2085 2086| 参数名 | 类型 | 必填 | 说明 | 2087| ------ | ------ | ---- | ------- | 2088| type | string | 是 | 设置监听类型,固定取值为'handleExtendAction'。 | 2089| callback | (action: [ExtendAction](#extendaction10)) => void | 否 | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 | 2090 2091**示例:** 2092 2093```ts 2094let onHandleExtendActionCallback = (action: inputMethod.ExtendAction) => { 2095 console.log(`Succeeded in subscribing handleExtendAction, action: ${action}`); 2096}; 2097inputMethodController.off('handleExtendAction', onHandleExtendActionCallback); 2098inputMethodController.off('handleExtendAction'); 2099``` 2100 2101### on('selectByRange')<sup>10+</sup> 2102 2103on(type: 'selectByRange', callback: Callback<Range>): void 2104 2105订阅输入法应用按范围选中文本事件。使用callback异步回调。 2106 2107**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2108 2109**参数:** 2110 2111| 参数名 | 类型 | 必填 | 说明 | 2112| -------- | ---- | ---- | ------- | 2113| type | string | 是 | 设置监听类型,固定取值为'selectByRange'。 | 2114| callback | Callback<[Range](#range10)> | 是 | 回调函数,返回需要选中的文本范围。<br/>根据传入的文本范围,开发者在回调函数中编辑框中相应文本。| 2115 2116**示例:** 2117 2118```ts 2119inputMethodController.on('selectByRange', (range: inputMethod.Range) => { 2120 console.log(`Succeeded in subscribing selectByRange: start: ${range.start} , end: ${range.end}`); 2121}); 2122``` 2123 2124### off('selectByRange')<sup>10+</sup> 2125 2126off(type: 'selectByRange', callback?: Callback<Range>): void 2127 2128取消订阅输入法应用按范围选中文本事件。使用callback异步回调。 2129 2130**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2131 2132**参数:** 2133 2134| 参数名 | 类型 | 必填 | 说明 | 2135| -------- | --------------------------------- | ---- | ------------------------------------------------------------ | 2136| type | string | 是 | 设置监听类型,固定取值为'selectByRange'。 | 2137| callback | Callback<[Range](#range10)> | 否 | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 | 2138 2139**示例:** 2140 2141```ts 2142let onSelectByRangeCallback = (range: inputMethod.Range) => { 2143 console.log(`Succeeded in subscribing selectByRange, range: ${JSON.stringify(range)}`); 2144}; 2145inputMethodController.off('selectByRange', onSelectByRangeCallback); 2146inputMethodController.off('selectByRange'); 2147``` 2148 2149### on('selectByMovement')<sup>10+</sup> 2150 2151on(type: 'selectByMovement', callback: Callback<Movement>): void 2152 2153订阅输入法应用按光标移动方向,选中文本事件。使用callback异步回调。 2154 2155**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2156 2157**参数:** 2158 2159| 参数名 | 类型 | 必填 | 说明 | 2160| -------- | ----- | ---- | ------ | 2161| type | string | 是 | 设置监听类型,固定取值为'selectByMovement'。 | 2162| callback | Callback<[Movement](#movement10)> | 是 | 回调函数,返回光标移动的方向。<br/>根据传入的光标移动方向,选中编辑框中相应文本。 | 2163 2164**示例:** 2165 2166```ts 2167inputMethodController.on('selectByMovement', (movement: inputMethod.Movement) => { 2168 console.log('Succeeded in subscribing selectByMovement: direction: ' + movement.direction); 2169}); 2170``` 2171 2172### off('selectByMovement')<sup>10+</sup> 2173 2174off(type: 'selectByMovement', callback?: Callback<Movement>): void 2175 2176取消订阅输入法应用按光标移动方向,选中文本事件。使用callback异步回调。 2177 2178**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2179 2180**参数:** 2181 2182| 参数名 | 类型 | 必填 | 说明 | 2183| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 2184| type | string | 是 | 设置监听类型,固定取值为'selectByMovement'。 | 2185| callback | Callback<[Movement](#movement10)> | 否 | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。 | 2186 2187**示例:** 2188 2189```ts 2190let onSelectByMovementCallback = (movement: inputMethod.Movement) => { 2191 console.log(`Succeeded in subscribing selectByMovement, movement.direction: ${movement.direction}`); 2192}; 2193inputMethodController.off('selectByMovement', onSelectByMovementCallback); 2194inputMethodController.off('selectByMovement'); 2195``` 2196 2197### on('getLeftTextOfCursor')<sup>10+</sup> 2198 2199on(type: 'getLeftTextOfCursor', callback: (length: number) => string): void; 2200 2201订阅输入法应用获取光标左侧指定长度文本事件。使用callback异步回调。 2202 2203**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2204 2205**参数:** 2206 2207| 参数名 | 类型 | 必填 | 说明 | 2208| -------- | ----- | ---- | ------ | 2209| type | string | 是 | 设置监听类型,固定取值为'getLeftTextOfCursor'。 | 2210| callback | (length: number) => string | 是 | 回调函数,获取编辑框最新状态下光标左侧指定长度的文本内容并返回。 | 2211 2212**错误码:** 2213 2214以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 2215 2216| 错误码ID | 错误信息 | 2217| -------- | -------------------------------------- | 2218| 12800009 | input method client is detached. | 2219 2220**示例:** 2221 2222```ts 2223try { 2224 inputMethodController.on('getLeftTextOfCursor', (length: number) => { 2225 console.info(`Succeeded in subscribing getLeftTextOfCursor, length: ${length}`); 2226 let text:string = ""; 2227 return text; 2228 }); 2229} catch(err) { 2230 console.error(`Failed to subscribe getLeftTextOfCursor. err: ${JSON.stringify(err)}`); 2231} 2232``` 2233 2234### off('getLeftTextOfCursor')<sup>10+</sup> 2235 2236off(type: 'getLeftTextOfCursor', callback?: (length: number) => string): void; 2237 2238取消订阅输入法应用获取光标左侧指定长度文本事件。使用callback异步回调。 2239 2240**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2241 2242**参数:** 2243 2244| 参数名 | 类型 | 必填 | 说明 | 2245| ------ | ------ | ---- | ------------------------------------------------------------ | 2246| type | string | 是 | 设置监听类型,固定取值为'getLeftTextOfCursor'。 | 2247| callback | (length: number) => string | 否 | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。| 2248 2249**示例:** 2250 2251```ts 2252try { 2253 let getLeftTextOfCursorCallback = (length: number) => { 2254 console.info(`Succeeded in unsubscribing getLeftTextOfCursor, length: ${length}`); 2255 let text:string = ""; 2256 return text; 2257 }; 2258 inputMethodController.off('getLeftTextOfCursor', getLeftTextOfCursorCallback); 2259 inputMethodController.off('getLeftTextOfCursor'); 2260} catch(err) { 2261 console.error(`Failed to unsubscribing getLeftTextOfCursor. err: ${JSON.stringify(err)}`); 2262} 2263``` 2264 2265### on('getRightTextOfCursor')<sup>10+</sup> 2266 2267on(type: 'getRightTextOfCursor', callback: (length: number) => string): void; 2268 2269订阅输入法应用获取光标右侧指定长度文本事件。使用callback异步回调。 2270 2271**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2272 2273**参数:** 2274 2275| 参数名 | 类型 | 必填 | 说明 | 2276| -------- | ----- | ---- | ------ | 2277| type | string | 是 | 设置监听类型,固定取值为'getRightTextOfCursor'。 | 2278| callback | (length: number) => string | 是 | 回调函数,获取编辑框最新状态下光标右侧指定长度的文本内容并返回。 | 2279 2280**错误码:** 2281 2282以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 2283 2284| 错误码ID | 错误信息 | 2285| -------- | -------------------------------------- | 2286| 12800009 | input method client is detached. | 2287 2288**示例:** 2289 2290```ts 2291try { 2292 inputMethodController.on('getRightTextOfCursor', (length: number) => { 2293 console.info(`Succeeded in subscribing getRightTextOfCursor, length: ${length}`); 2294 let text:string = ""; 2295 return text; 2296 }); 2297} catch(err) { 2298 console.error(`Failed to subscribe getRightTextOfCursor. err: ${JSON.stringify(err)}`); 2299} 2300``` 2301 2302### off('getRightTextOfCursor')<sup>10+</sup> 2303 2304off(type: 'getRightTextOfCursor', callback?: (length: number) => string): void; 2305 2306取消订阅输入法应用获取光标右侧指定长度文本事件。使用callback异步回调。 2307 2308**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2309 2310**参数:** 2311 2312| 参数名 | 类型 | 必填 | 说明 | 2313| ------ | ------ | ---- | ------------------------------------------------------------ | 2314| type | string | 是 | 设置监听类型,固定取值为'getRightTextOfCursor'。 | 2315| callback | (length: number) => string | 否 |取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。| 2316 2317**示例:** 2318 2319```ts 2320try { 2321 let getRightTextOfCursorCallback = (length: number) => { 2322 console.info(`Succeeded in unsubscribing getRightTextOfCursor, length: ${length}`); 2323 let text:string = ""; 2324 return text; 2325 }; 2326 inputMethodController.off('getRightTextOfCursor', getRightTextOfCursorCallback); 2327 inputMethodController.off('getRightTextOfCursor'); 2328} catch(err) { 2329 console.error(`Failed to unsubscribing getRightTextOfCursor. err: ${JSON.stringify(err)}`); 2330} 2331``` 2332 2333### on('getTextIndexAtCursor')<sup>10+</sup> 2334 2335on(type: 'getTextIndexAtCursor', callback: () => number): void; 2336 2337订阅输入法应用获取光标处文本索引事件。使用callback异步回调。 2338 2339**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2340 2341**参数:** 2342 2343| 参数名 | 类型 | 必填 | 说明 | 2344| -------- | ----- | ---- | ------ | 2345| type | string | 是 | 设置监听类型,固定取值为'getTextIndexAtCursor'。 | 2346| callback | () => number | 是 | 回调函数,获取编辑框最新状态下光标处文本索引并返回。 | 2347 2348**错误码:** 2349 2350以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 2351 2352| 错误码ID | 错误信息 | 2353| -------- | -------------------------------------- | 2354| 12800009 | input method client is detached. | 2355 2356**示例:** 2357 2358```ts 2359try { 2360 inputMethodController.on('getTextIndexAtCursor', () => { 2361 console.info(`Succeeded in subscribing getTextIndexAtCursor.`); 2362 let index:number = 0; 2363 return index; 2364 }); 2365} catch(err) { 2366 console.error(`Failed to subscribe getTextIndexAtCursor. err: ${JSON.stringify(err)}`); 2367} 2368``` 2369 2370### off('getTextIndexAtCursor')<sup>10+</sup> 2371 2372off(type: 'getTextIndexAtCursor', callback?: () => number): void; 2373 2374取消订阅输入法应用获取光标处文本索引事件。使用callback异步回调。 2375 2376**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2377 2378**参数:** 2379 2380| 参数名 | 类型 | 必填 | 说明 | 2381| ------ | ------ | ---- | ------------------------------------------------------------ | 2382| type | string | 是 | 设置监听类型,固定取值为'getTextIndexAtCursor'。 | 2383| callback | () => number | 否 | 取消订阅的回调函数,需要与on接口传入的保持一致。<br>参数不填写时,取消订阅type对应的所有回调事件。| 2384 2385**示例:** 2386 2387```ts 2388try { 2389 let getTextIndexAtCursorCallback = () => { 2390 console.info(`Succeeded in unsubscribing getTextIndexAtCursor.`); 2391 let index:number = 0; 2392 return index; 2393 }; 2394 inputMethodController.off('getTextIndexAtCursor', getTextIndexAtCursorCallback); 2395 inputMethodController.off('getTextIndexAtCursor'); 2396} catch(err) { 2397 console.error(`Failed to unsubscribing getTextIndexAtCursor. err: ${JSON.stringify(err)}`); 2398} 2399``` 2400 2401## InputMethodSetting<sup>8+</sup> 2402 2403下列API均需使用[getSetting](#inputmethodgetsetting9)获取到InputMethodSetting实例后,通过实例调用。 2404 2405### on('imeChange')<sup>9+</sup> 2406 2407on(type: 'imeChange', callback: (inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => void): void 2408 2409订阅输入法及子类型变化监听事件。使用callback异步回调。 2410 2411**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2412 2413**参数:** 2414 2415| 参数名 | 类型 | 必填 | 说明 | 2416| -------- | ------------------------------- | ---- | ------------------------------------------------------------ | 2417| type | string | 是 | 设置监听类型,固定取值为'imeChange'。 | 2418| callback | (inputMethodProperty: [InputMethodProperty](#inputmethodproperty8), inputMethodSubtype: [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)) => void | 是 | 回调函数,返回输入法属性对象及子类型对象。 | 2419 2420**示例:** 2421 2422```ts 2423import InputMethodSubtype from '@ohos.InputMethodSubtype'; 2424inputMethodSetting.on('imeChange', (inputMethodProperty: inputMethod.InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => { 2425 console.log('Succeeded in subscribing imeChange: inputMethodProperty: ' + JSON.stringify(inputMethodProperty) + " , inputMethodSubtype: " + JSON.stringify(inputMethodSubtype)); 2426}); 2427``` 2428 2429### off('imeChange')<sup>9+</sup> 2430 2431off(type: 'imeChange', callback?: (inputMethodProperty: InputMethodProperty, inputMethodSubtype: InputMethodSubtype) => void): void 2432 2433取消订阅输入法及子类型变化监听事件。使用callback异步回调。 2434 2435**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2436 2437**参数:** 2438 2439| 参数名 | 类型 | 必填 | 说明 | 2440| -------- | --------- | ---- | --------------- | 2441| type | string | 是 | 设置监听类型,固定取值为'imeChange'。 | 2442| callback | (inputMethodProperty: [InputMethodProperty](#inputmethodproperty8), inputMethodSubtype: [InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)) => void | 否 | 回调函数,返回取消订阅的输入法属性对象及子类型对象。 | 2443 2444**示例:** 2445 2446```ts 2447inputMethodSetting.off('imeChange'); 2448``` 2449 2450### on('imeShow')<sup>10+</sup> 2451 2452on(type: 'imeShow', callback: (info: Array\<InputWindowInfo>) => void): void 2453 2454订阅输入法软键盘显示事件。使用callback异步回调。 2455 2456**系统接口**:此接口为系统接口。 2457 2458**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2459 2460**参数:** 2461 2462| 参数名 | 类型 | 必填 | 说明 | 2463| -------- | ---- | ---- | ---- | 2464| type | string | 是 | 设置监听类型,固定取值为'imeShow'。 | 2465| callback | (info: Array\<InputWindowInfo>) => void | 是 | 回调函数,返回输入法软键盘信息。 | 2466 2467**示例:** 2468 2469```ts 2470inputMethodSetting.on('imeShow', (info: Array<inputMethod.InputWindowInfo>) => { 2471 console.info('Succeeded in subscribing imeShow event.'); 2472}); 2473``` 2474 2475### on('imeHide')<sup>10+</sup> 2476 2477on(type: 'imeHide', callback: (info: Array\<InputWindowInfo>) => void): void 2478 2479订阅输入法软键盘隐藏事件。使用callback异步回调。 2480 2481**系统接口**:此接口为系统接口。 2482 2483**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2484 2485**参数:** 2486 2487| 参数名 | 类型 | 必填 | 说明 | 2488| -------- | ---- | ---- | ---- | 2489| type | string | 是 | 设置监听类型,固定取值为'imeHide'。 | 2490| callback | (info: Array\<InputWindowInfo>) => void | 是 | 回调函数,返回输入法软键盘信息。 | 2491 2492**示例:** 2493 2494```ts 2495inputMethodSetting.on('imeHide', (info: Array<inputMethod.InputWindowInfo>) => { 2496 console.info('Succeeded in subscribing imeHide event.'); 2497}); 2498``` 2499 2500### off('imeShow')<sup>10+</sup> 2501 2502off(type: 'imeShow', callback?: (info: Array\<InputWindowInfo>) => void): void 2503 2504取消订阅输入法软键盘显示事件。 2505 2506**系统接口**:此接口为系统接口。 2507 2508**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2509 2510**参数:** 2511 2512| 参数名 | 类型 | 必填 | 说明 | 2513| -------- | ---- | ---- | ------ | 2514| type | string | 是 | 设置监听类型,固定取值`imeShow`。 | 2515| callback | (info: Array\<InputWindowInfo>) => void | 否 | 取消订阅的回调函数。<br>参数不填写时,取消订阅type对应的所有回调事件。 | 2516 2517**示例:** 2518 2519```ts 2520inputMethodSetting.off('imeShow'); 2521``` 2522 2523### off('imeHide')<sup>10+</sup> 2524 2525off(type: 'imeHide', callback?: (info: Array\<InputWindowInfo>) => void): void 2526 2527取消订阅输入法软键盘隐藏事件。 2528 2529**系统接口**:此接口为系统接口。 2530 2531**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2532 2533**参数:** 2534 2535| 参数名 | 类型 | 必填 | 说明 | 2536| -------- | ---- | ---- | ------ | 2537| type | string | 是 | 设置监听类型,固定取值'imeHide'。 | 2538| callback | (info: Array\<InputWindowInfo>) => void | 否 | 取消订阅的回调函数。<br>参数不填写时,取消订阅type对应的所有回调事件。 | 2539 2540**示例:** 2541 2542```ts 2543inputMethodSetting.off('imeHide'); 2544``` 2545 2546### listInputMethodSubtype<sup>9+</sup> 2547 2548listInputMethodSubtype(inputMethodProperty: InputMethodProperty, callback: AsyncCallback<Array<InputMethodSubtype>>): void 2549 2550获取指定输入法应用的所有子类型。使用callback异步回调。 2551 2552**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2553 2554**参数:** 2555 2556| 参数名 | 类型 | 必填 | 说明 | 2557| -------- | -------------------------------------------------- | ---- | ---------------------- | 2558| inputMethodProperty | [InputMethodProperty](#inputmethodproperty8)| 是 | 输入法应用。 | 2559| callback | AsyncCallback<Array<[InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)>> | 是 | 回调函数,返回指定输入法应用的所有子类型。 | 2560 2561**错误码:** 2562 2563以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 2564 2565| 错误码ID | 错误信息 | 2566| -------- | -------------------------------------- | 2567| 12800001 | package manager error. | 2568| 12800008 | input method manager service error. | 2569 2570**示例:** 2571 2572```ts 2573import inputMethod from '@ohos.inputMethod'; 2574import InputMethodSubtype from '@ohos.InputMethodSubtype'; 2575import { BusinessError } from '@ohos.base'; 2576 2577let inputMethodProperty: inputMethod.InputMethodProperty = { 2578 packageName: 'com.example.kikakeyboard', 2579 name: 'InputMethodExAbility', 2580 methodId: '', 2581 id: 'propertyId', 2582} 2583let inputMethodSetting = inputMethod.getSetting(); 2584try { 2585 inputMethodSetting.listInputMethodSubtype(inputMethodProperty, (err: BusinessError, data: Array<InputMethodSubtype>) => { 2586 if (err) { 2587 console.error(`Failed to listInputMethodSubtype: ${JSON.stringify(err)}`); 2588 return; 2589 } 2590 console.log('Succeeded in listing inputMethodSubtype.'); 2591 }); 2592} catch (err) { 2593 console.error(`Failed to listInputMethodSubtype: ${JSON.stringify(err)}`); 2594} 2595``` 2596 2597### listInputMethodSubtype<sup>9+</sup> 2598 2599listInputMethodSubtype(inputMethodProperty: InputMethodProperty): Promise<Array<InputMethodSubtype>> 2600 2601获取指定输入法应用的所有子类型。使用promise异步回调。 2602 2603**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2604 2605**参数:** 2606 2607| 参数名 | 类型 | 必填 | 说明 | 2608| -------- | -------------------------------------------------- | ---- | ---------------------- | 2609| inputMethodProperty | [InputMethodProperty](#inputmethodproperty8)| 是 | 输入法应用。 | 2610 2611**返回值:** 2612 2613| 类型 | 说明 | 2614| ----------------------------------------------------------- | ---------------------- | 2615| Promise<Array<[InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)>> | Promise对象,返回指定输入法应用的所有子类型。 | 2616 2617**错误码:** 2618 2619以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 2620 2621| 错误码ID | 错误信息 | 2622| -------- | -------------------------------------- | 2623| 12800001 | package manager error. | 2624| 12800008 | input method manager service error. | 2625 2626**示例:** 2627 2628```ts 2629import inputMethod from '@ohos.inputMethod'; 2630import InputMethodSubtype from '@ohos.InputMethodSubtype'; 2631import { BusinessError } from '@ohos.base'; 2632 2633let inputMethodProperty: inputMethod.InputMethodProperty = { 2634 packageName: 'com.example.kikakeyboard', 2635 name: 'InputMethodExAbility', 2636 methodId: '', 2637 id: 'propertyId', 2638} 2639let inputMethodSetting = inputMethod.getSetting(); 2640try { 2641 inputMethodSetting.listInputMethodSubtype(inputMethodProperty).then((data: Array<InputMethodSubtype>) => { 2642 console.log('Succeeded in listing inputMethodSubtype.'); 2643 }).catch((err: BusinessError) => { 2644 console.error(`Failed to listInputMethodSubtype: ${JSON.stringify(err)}`); 2645 }) 2646} catch(err) { 2647 console.error(`Failed to listInputMethodSubtype: ${JSON.stringify(err)}`); 2648} 2649``` 2650 2651### listCurrentInputMethodSubtype<sup>9+</sup> 2652 2653listCurrentInputMethodSubtype(callback: AsyncCallback<Array<InputMethodSubtype>>): void 2654 2655查询当前输入法应用的所有子类型。使用callback异步回调。 2656 2657**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2658 2659**参数:** 2660 2661| 参数名 | 类型 | 必填 | 说明 | 2662| -------- | -------------------------------------------------- | ---- | ---------------------- | 2663| callback | AsyncCallback<Array<[InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)>> | 是 | 回调函数,返回当前输入法应用的所有子类型。 | 2664 2665**错误码:** 2666 2667以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 2668 2669| 错误码ID | 错误信息 | 2670| -------- | -------------------------------------- | 2671| 12800001 | package manager error. | 2672| 12800008 | input method manager service error. | 2673 2674**示例:** 2675 2676```ts 2677import InputMethodSubtype from '@ohos.InputMethodSubtype'; 2678import { BusinessError } from '@ohos.base'; 2679 2680let inputMethodSetting = inputMethod.getSetting(); 2681try { 2682 inputMethodSetting.listCurrentInputMethodSubtype((err: BusinessError, data: Array<InputMethodSubtype>) => { 2683 if (err) { 2684 console.error(`Failed to listCurrentInputMethodSubtype: ${JSON.stringify(err)}`); 2685 return; 2686 } 2687 console.log('Succeeded in listing currentInputMethodSubtype.'); 2688 }); 2689} catch(err) { 2690 console.error(`Failed to listCurrentInputMethodSubtype: ${JSON.stringify(err)}`); 2691} 2692``` 2693 2694### listCurrentInputMethodSubtype<sup>9+</sup> 2695 2696listCurrentInputMethodSubtype(): Promise<Array<InputMethodSubtype>> 2697 2698查询当前输入法应用的所有子类型。使用promise异步回调。 2699 2700**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2701 2702**返回值:** 2703 2704| 类型 | 说明 | 2705| ----------------------------------------------------------- | ---------------------- | 2706| Promise<Array<[InputMethodSubtype](./js-apis-inputmethod-subtype.md#inputmethodsubtype)>> | Promise对象,返回当前输入法应用的所有子类型。 | 2707 2708**错误码:** 2709 2710以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 2711 2712| 错误码ID | 错误信息 | 2713| -------- | -------------------------------------- | 2714| 12800001 | package manager error. | 2715| 12800008 | input method manager service error. | 2716 2717**示例:** 2718 2719```ts 2720import InputMethodSubtype from '@ohos.InputMethodSubtype'; 2721import { BusinessError } from '@ohos.base'; 2722 2723let inputMethodSetting = inputMethod.getSetting(); 2724try { 2725 inputMethodSetting.listCurrentInputMethodSubtype().then((data: Array<InputMethodSubtype>) => { 2726 console.log('Succeeded in listing currentInputMethodSubtype.'); 2727 }).catch((err: BusinessError) => { 2728 console.error(`Failed to listCurrentInputMethodSubtype: ${JSON.stringify(err)}`); 2729 }) 2730} catch(err) { 2731 console.error(`Failed to listCurrentInputMethodSubtype: ${JSON.stringify(err)}`); 2732} 2733``` 2734 2735### getInputMethods<sup>9+</sup> 2736 2737getInputMethods(enable: boolean, callback: AsyncCallback<Array<InputMethodProperty>>): void 2738 2739获取已激活/未激活的输入法应用列表。使用callback异步回调。 2740 2741> **说明** 2742> 2743> 已激活输入法包括当前使用的输入法,未激活输入法包括当前输入法以外的其他已安装的输入法。 2744> 2745> 已激活/未激活为预留概念,暂不支持。 2746 2747**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2748 2749**参数:** 2750 2751| 参数名 | 类型 | 必填 | 说明 | 2752| -------- | --------------------------------------------------- | ---- | ----------------------------- | 2753| enable | boolean | 是 |true表示返回已激活输入法列表,false表示返回未激活输入法列表。 | 2754| callback | AsyncCallback<Array<[InputMethodProperty](#inputmethodproperty8)>> | 是 | 回调函数,返回已激活/未激活输入法列表。 | 2755 2756**错误码:** 2757 2758以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 2759 2760| 错误码ID | 错误信息 | 2761| -------- | -------------------------------------- | 2762| 12800001 | package manager error. | 2763| 12800008 | input method manager service error. | 2764 2765**示例:** 2766 2767```ts 2768try { 2769 inputMethodSetting.getInputMethods(true, (err: BusinessError, data: Array<inputMethod.InputMethodProperty>) => { 2770 if (err) { 2771 console.error(`Failed to getInputMethods: ${JSON.stringify(err)}`); 2772 return; 2773 } 2774 console.log('Succeeded in getting inputMethods.'); 2775 }); 2776} catch (err) { 2777 console.error(`Failed to getInputMethods: ${JSON.stringify(err)}`); 2778} 2779``` 2780 2781### getInputMethods<sup>9+</sup> 2782 2783getInputMethods(enable: boolean): Promise<Array<InputMethodProperty>> 2784 2785获取已激活/未激活的输入法应用列表。使用promise异步回调。 2786 2787> **说明** 2788> 2789> 已激活输入法包括当前使用的输入法,未激活输入法包括当前输入法以外的其他已安装的输入法。 2790> 2791> 已激活/未激活为预留概念,暂不支持。 2792 2793**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2794 2795**参数:** 2796 2797| 参数名 | 类型 | 必填 | 说明 | 2798| ------ | ------- | ---- | ----------------------- | 2799| enable | boolean | 是 |- true表示返回已激活输入法列表,false表示返回未激活输入法列表。 | 2800 2801**错误码:** 2802 2803以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 2804 2805| 错误码ID | 错误信息 | 2806| -------- | -------------------------------------- | 2807| 12800001 | package manager error. | 2808| 12800008 |input method manager service error. | 2809 2810**返回值:** 2811 2812| 类型 | 说明 | 2813| ------------------------------------------------------------ | ----------------------------- | 2814| Promise\<Array\<[InputMethodProperty](#inputmethodproperty8)>> | Promise对象,返回已激活/未激活输入法列表。 | 2815 2816**示例:** 2817 2818```ts 2819try { 2820 inputMethodSetting.getInputMethods(true).then((data: Array<inputMethod.InputMethodProperty>) => { 2821 console.log('Succeeded in getting inputMethods.'); 2822 }).catch((err: BusinessError) => { 2823 console.error(`Failed to getInputMethods: ${JSON.stringify(err)}`); 2824 }) 2825} catch(err) { 2826 console.error(`Failed to getInputMethods: ${JSON.stringify(err)}`); 2827} 2828``` 2829 2830### showOptionalInputMethods<sup>9+</sup> 2831 2832showOptionalInputMethods(callback: AsyncCallback<boolean>): void 2833 2834显示输入法选择对话框。使用callback异步回调。 2835 2836**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2837 2838**参数:** 2839 2840| 参数名 | 类型 | 必填 | 说明 | 2841| -------- | -------- | -------- | -------- | 2842| callback | AsyncCallback<boolean> | 是 | 回调函数。当输入法选择对话框显示成功,err为undefined,data为true;否则为错误对象。 | 2843 2844**错误码:** 2845 2846以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 2847 2848| 错误码ID | 错误信息 | 2849| -------- | -------------------------------------- | 2850| 12800008 | input method manager service error. | 2851 2852**示例:** 2853 2854```ts 2855try { 2856 inputMethodSetting.showOptionalInputMethods((err: BusinessError, data: boolean) => { 2857 if (err) { 2858 console.error(`Failed to showOptionalInputMethods: ${JSON.stringify(err)}`); 2859 return; 2860 } 2861 console.log('Succeeded in showing optionalInputMethods.'); 2862 }); 2863} catch (err) { 2864 console.error(`Failed to showOptionalInputMethods: ${JSON.stringify(err)}`); 2865} 2866``` 2867 2868### showOptionalInputMethods<sup>9+</sup> 2869 2870showOptionalInputMethods(): Promise<boolean> 2871 2872显示输入法选择对话框。使用promise异步回调。 2873 2874**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2875 2876**返回值:** 2877 2878| 类型 | 说明 | 2879| -------- | -------- | 2880| Promise<boolean> | Promise对象。返回true表示输入法选择对话框显示成功,返回false表示输入法选择对话框显示失败。 | 2881 2882**错误码:** 2883 2884以下错误码的详细介绍请参见[输入法框架错误码](../errorcodes/errorcode-inputmethod-framework.md)。 2885 2886| 错误码ID | 错误信息 | 2887| -------- | -------------------------------------- | 2888| 12800008 | input method manager service error. | 2889 2890**示例:** 2891 2892```ts 2893inputMethodSetting.showOptionalInputMethods().then((data: boolean) => { 2894 console.log('Succeeded in showing optionalInputMethods.'); 2895}).catch((err: BusinessError) => { 2896 console.error(`Failed to showOptionalInputMethods: ${JSON.stringify(err)}`); 2897}) 2898``` 2899 2900### listInputMethod<sup>(deprecated)</sup> 2901 2902listInputMethod(callback: AsyncCallback<Array<InputMethodProperty>>): void 2903 2904查询已安装的输入法列表。使用callback异步回调。 2905 2906> **说明:** 2907> 2908> 从API version 8开始支持,从API version 9开始废弃,建议使用[getInputMethods](#getinputmethods9)替代。 2909 2910**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2911 2912**参数:** 2913 2914| 参数名 | 类型 | 必填 | 说明 | 2915| -------- | -------------------------------------------------- | ---- | ---------------------- | 2916| callback | AsyncCallback<Array<[InputMethodProperty](#inputmethodproperty8)>> | 是 | 回调函数,返回已安装的输入法列表。 | 2917 2918**示例:** 2919 2920```ts 2921inputMethodSetting.listInputMethod((err: BusinessError, data: Array<inputMethod.InputMethodProperty>) => { 2922 if (err) { 2923 console.error(`Failed to listInputMethod: ${JSON.stringify(err)}`); 2924 return; 2925 } 2926 console.log('Succeeded in listing inputMethod.'); 2927 }); 2928``` 2929 2930### listInputMethod<sup>(deprecated)</sup> 2931 2932listInputMethod(): Promise<Array<InputMethodProperty>> 2933 2934查询已安装的输入法列表。使用promise异步回调。 2935 2936> **说明:** 2937> 2938> 从API version 8开始支持,从API version 9开始废弃,建议使用[getInputMethods](#getinputmethods9-1)替代。 2939 2940**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2941 2942**返回值:** 2943 2944| 类型 | 说明 | 2945| ----------------------------------------------------------- | ---------------------- | 2946| Promise<Array<[InputMethodProperty](#inputmethodproperty8)>> | Promise对象,返回已安装输入法列表。 | 2947 2948**示例:** 2949 2950```ts 2951inputMethodSetting.listInputMethod().then((data: Array<inputMethod.InputMethodProperty>) => { 2952 console.log('Succeeded in listing inputMethod.'); 2953}).catch((err: BusinessError) => { 2954 console.error(`Failed to listInputMethod: ${JSON.stringify(err)}`); 2955}) 2956``` 2957 2958### displayOptionalInputMethod<sup>(deprecated)</sup> 2959 2960displayOptionalInputMethod(callback: AsyncCallback<void>): void 2961 2962显示输入法选择对话框。使用callback异步回调。 2963 2964> **说明:** 2965> 2966> 从API version 8开始支持,从API version 9开始废弃,建议使用[showOptionalInputMethods()](#showoptionalinputmethods9)替代。 2967 2968**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2969 2970**参数:** 2971 2972| 参数名 | 类型 | 必填 | 说明 | 2973| -------- | -------- | -------- | -------- | 2974| callback | AsyncCallback<void> | 是 | 回调函数。当输入法选择对话框显示成功。err为undefined,否则为错误对象。 | 2975 2976**示例:** 2977 2978```ts 2979inputMethodSetting.displayOptionalInputMethod((err: BusinessError) => { 2980 if (err) { 2981 console.error(`Failed to displayOptionalInputMethod: ${JSON.stringify(err)}`); 2982 return; 2983 } 2984 console.log('Succeeded in displaying optionalInputMethod.'); 2985}); 2986``` 2987 2988### displayOptionalInputMethod<sup>(deprecated)</sup> 2989 2990displayOptionalInputMethod(): Promise<void> 2991 2992显示输入法选择对话框。使用promise异步回调。 2993 2994> **说明:** 2995> 2996> 从API version 8开始支持,从API version 9开始废弃,建议使用[showOptionalInputMethods()](#showoptionalinputmethods9-1)替代。 2997 2998**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2999 3000**返回值:** 3001 3002| 类型 | 说明 | 3003| -------- | -------- | 3004| Promise<void> | 无返回结果的Promise对象。 | 3005 3006**示例:** 3007 3008```ts 3009inputMethodSetting.displayOptionalInputMethod().then(() => { 3010 console.log('Succeeded in displaying optionalInputMethod.'); 3011}).catch((err: BusinessError) => { 3012 console.error(`Failed to displayOptionalInputMethod: ${JSON.stringify(err)}`); 3013}) 3014```