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