1# @ohos.inputMethodEngine (输入法服务) 2<!--Kit: IME Kit--> 3<!--Subsystem: MiscServices--> 4<!--Owner: @illybyy--> 5<!--Designer: @andeszhang--> 6<!--Tester: @murphy1984--> 7<!--Adviser: @zhang_yixin13--> 8 9本模块面向输入法应用(包括系统输入法应用、三方输入法应用),为输入法应用提供能力,包括:创建软键盘窗口、插入/删除字符、选中文本、监听物理键盘按键事件等。 10 11> **说明:** 12> 13>本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14 15## 导入模块 16 17```ts 18import { inputMethodEngine } from '@kit.IMEKit'; 19``` 20 21## 常量 22 23功能键常量值、编辑框常量值及光标常量值。 24 25**系统能力:** SystemCapability.MiscServices.InputMethodFramework 26 27| 名称 | 类型 | 值 | 说明 | 28| -------- | -------- | -------- | -------- | 29| ENTER_KEY_TYPE_UNSPECIFIED | number | 0 | 无功能键。 | 30| ENTER_KEY_TYPE_GO | number | 2 | “前往”功能键。 | 31| ENTER_KEY_TYPE_SEARCH | number | 3 | “搜索”功能键。 | 32| ENTER_KEY_TYPE_SEND | number | 4 | “发送”功能键。 | 33| ENTER_KEY_TYPE_NEXT | number | 5 | “下一个”功能键。 | 34| ENTER_KEY_TYPE_DONE | number | 6 | “回车”功能键。 | 35| ENTER_KEY_TYPE_PREVIOUS | number | 7 | “前一个”功能键。 | 36| ENTER_KEY_TYPE_NEWLINE<sup>12+</sup> | number | 8 | “换行”功能键。 | 37| PATTERN_NULL | number | -1 | 无特殊性编辑框。 | 38| PATTERN_TEXT | number | 0 | 文本编辑框。 | 39| PATTERN_NUMBER | number | 2 | 数字编辑框。 | 40| PATTERN_PHONE | number | 3 | 电话号码编辑框。 | 41| PATTERN_DATETIME | number | 4 | 日期编辑框。 | 42| PATTERN_EMAIL | number | 5 | 邮件编辑框。 | 43| PATTERN_URI | number | 6 | 超链接编辑框。 | 44| PATTERN_PASSWORD | number | 7 | 密码编辑框。 | 45| PATTERN_PASSWORD_NUMBER<sup>11+</sup> | number | 8 | 数字密码编辑框。 | 46| PATTERN_PASSWORD_SCREEN_LOCK<sup>11+</sup> | number | 9 | 锁屏密码编辑框。 | 47| PATTERN_USER_NAME<sup>20+</sup> | number | 10 | 用户名编辑框。 | 48| PATTERN_NEW_PASSWORD<sup>20+</sup> | number | 11 | 新密码编辑框。 | 49| PATTERN_NUMBER_DECIMAL<sup>20+</sup> | number | 12 | 带小数点的数字编辑框。 | 50| PATTERN_ONE_TIME_CODE<sup>20+</sup> | number | 13 | 验证码编辑框。 | 51| OPTION_ASCII | number | 20 | 允许输入ASCII值。 | 52| OPTION_NONE | number | 0 | 不指定编辑框输入属性。 | 53| OPTION_AUTO_CAP_CHARACTERS | number | 2 | 允许输入字符。 | 54| OPTION_AUTO_CAP_SENTENCES | number | 8 | 允许输入句子。 | 55| OPTION_AUTO_WORDS | number | 4 | 允许输入单词。 | 56| OPTION_MULTI_LINE | number | 1 | 允许输入多行。 | 57| OPTION_NO_FULLSCREEN | number | 10 | 半屏样式。 | 58| FLAG_SELECTING | number | 2 | 编辑框处于选择状态。 | 59| FLAG_SINGLE_LINE | number | 1 | 编辑框为单行。 | 60| DISPLAY_MODE_PART | number | 0 | 编辑框显示为半屏。 | 61| DISPLAY_MODE_FULL | number | 1 | 编辑框显示为全屏。 | 62| CURSOR_UP<sup>9+</sup> | number | 1 | 光标上移。 | 63| CURSOR_DOWN<sup>9+</sup> | number | 2 | 光标下移。 | 64| CURSOR_LEFT<sup>9+</sup> | number | 3 | 光标左移。 | 65| CURSOR_RIGHT<sup>9+</sup> | number | 4 | 光标右移。 | 66| WINDOW_TYPE_INPUT_METHOD_FLOAT<sup>9+</sup> | number | 2105 | 输入法应用窗口风格标识。 | 67 68## inputMethodEngine.getInputMethodAbility<sup>9+</sup> 69 70getInputMethodAbility(): InputMethodAbility 71 72获取输入法应用客户端实例[InputMethodAbility](#inputmethodability),仅支持输入法应用调用。<br/>输入法应用获取该实例后,可订阅软键盘显示/隐藏请求事件、创建/销毁输入法面板等。 73 74**系统能力:** SystemCapability.MiscServices.InputMethodFramework 75 76**返回值:** 77 78| 类型 | 说明 | 79| ----------------------------------------- | ------------------ | 80| [InputMethodAbility](#inputmethodability) | 输入法应用客户端。 | 81 82**示例:** 83 84```ts 85let InputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility(); 86``` 87 88## inputMethodEngine.getKeyboardDelegate<sup>9+</sup> 89 90getKeyboardDelegate(): KeyboardDelegate 91 92获取客户端编辑事件监听代理实例[KeyboardDelegate](#keyboarddelegate)。<br/>输入法应用获取该实例后,可订阅物理键盘按键事件、选中文本变化事件等。 93 94**系统能力:** SystemCapability.MiscServices.InputMethodFramework 95 96**返回值:** 97 98| 类型 | 说明 | 99| ------------------------------------- | ------------------------ | 100| [KeyboardDelegate](#keyboarddelegate) | 客户端编辑事件监听代理。 | 101 102**示例:** 103 104```ts 105let KeyboardDelegate: inputMethodEngine.KeyboardDelegate = inputMethodEngine.getKeyboardDelegate(); 106``` 107 108## inputMethodEngine.getInputMethodEngine<sup>(deprecated)</sup> 109 110getInputMethodEngine(): InputMethodEngine 111 112获取输入法应用客户端实例[InputMethodEngine](#inputmethodengine)。<br/>输入法应用获取该实例后,可订阅软键盘显示/隐藏请求事件等。 113 114> **说明:** 115> 116> 从API version 8开始支持,API version 9开始废弃,建议使用[getInputMethodAbility()](#inputmethodenginegetinputmethodability9)替代。 117 118**系统能力:** SystemCapability.MiscServices.InputMethodFramework 119 120**返回值:** 121 122| 类型 | 说明 | 123| ----------------------------------------- | ------------------ | 124| [InputMethodEngine](#inputmethodengine) | 输入法应用客户端。 | 125 126**示例:** 127 128```ts 129let InputMethodEngine: inputMethodEngine.InputMethodEngine = inputMethodEngine.getInputMethodEngine(); 130``` 131 132## inputMethodEngine.createKeyboardDelegate<sup>(deprecated)</sup> 133 134createKeyboardDelegate(): KeyboardDelegate 135 136获取客户端编辑事件监听代理实例[KeyboardDelegate](#keyboarddelegate)。输入法应用获取该实例后,可订阅物理键盘按键事件、选中文本变化事件等。 137 138> **说明:** 139> 140>从API version 8开始支持,API version 9开始废弃,建议使用[getKeyboardDelegate()](#inputmethodenginegetkeyboarddelegate9)替代。 141 142**系统能力:** SystemCapability.MiscServices.InputMethodFramework 143 144**返回值:** 145 146| 类型 | 说明 | 147| ------------------------------------- | ------------------------ | 148| [KeyboardDelegate](#keyboarddelegate) | 客户端编辑事件监听代理。 | 149 150**示例:** 151 152```ts 153let keyboardDelegate: inputMethodEngine.KeyboardDelegate = inputMethodEngine.createKeyboardDelegate(); 154``` 155 156## CommandDataType<sup>12+</sup> 157 158type CommandDataType = number | string | boolean; 159 160表示私有数据类型,接口参数具体类型根据其功能而定。 161 162**系统能力:** SystemCapability.MiscServices.InputMethodFramework 163 164| 类型 | 说明 | 165| ------- | -------------------- | 166| string | 表示值类型为字符串。 | 167| number | 表示值类型为数字。 | 168| boolean | 表示值类型为布尔值。 | 169 170## SizeChangeCallback<sup>15+</sup> 171 172type SizeChangeCallback = (size: window.Size, keyboardArea?: KeyboardArea) => void 173 174当输入法面板大小变化时触发的回调。 175 176**系统能力:** SystemCapability.MiscServices.InputMethodFramework 177 178**参数:** 179 180| 参数名 | 类型 | 必填 | 说明 | 181| ------------ | ---------------------------------------------------- | ---- | -------------------------------- | 182| size | [window.Size](../apis-arkui/arkts-apis-window-i.md#size7) | 是 | 当前面板大小。 | 183| keyboardArea | [KeyboardArea](#keyboardarea15) | 否 | 当前面板中可作为键盘区域的大小。 | 184 185## InputMethodEngine 186 187下列API均需使用[getInputMethodEngine](#inputmethodenginegetinputmethodenginedeprecated)获取到InputMethodEngine实例后,通过实例调用。 188 189### on('inputStart') 190 191on(type: 'inputStart', callback: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void 192 193订阅输入法绑定成功事件。使用callback异步回调。 194 195**系统能力:** SystemCapability.MiscServices.InputMethodFramework 196 197**参数:** 198 199| 参数名 | 类型 | 必填 | 说明 | 200| -------- | ------------------------------- | ---- | ------------------------------------------------------------ | 201| type | string | 是 | 设置监听类型,固定取值为'inputStart'。 | 202| callback | (kbController: [KeyboardController](#keyboardcontroller), textInputClient: [TextInputClient](#textinputclientdeprecated)) => void | 是 | 回调函数,返回订阅输入法的KeyboardController和TextInputClient实例。 | 203 204**示例:** 205 206```ts 207inputMethodEngine.getInputMethodEngine() 208 .on('inputStart', 209 (kbController: inputMethodEngine.KeyboardController, textClient: inputMethodEngine.TextInputClient) => { 210 let keyboardController: inputMethodEngine.KeyboardController = kbController; 211 let textInputClient: inputMethodEngine.TextInputClient = textClient; 212 }); 213``` 214 215### off('inputStart') 216 217off(type: 'inputStart', callback?: (kbController: KeyboardController, textInputClient: TextInputClient) => void): void 218 219取消订阅输入法绑定成功事件。 220 221**系统能力:** SystemCapability.MiscServices.InputMethodFramework 222 223**参数:** 224 225| 参数名 | 类型 | 必填 | 说明 | 226| -------- | -------------------- | ---- | ------------------------ | 227| type | string | 是 | 设置监听类型,固定取值为'inputStart'。 | 228| callback | (kbController: [KeyboardController](#keyboardcontroller), textInputClient: [TextInputClient](#textinputclientdeprecated)) => void | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。| 229 230**示例:** 231 232```ts 233inputMethodEngine.getInputMethodEngine() 234 .off('inputStart', 235 (kbController: inputMethodEngine.KeyboardController, textClient: inputMethodEngine.TextInputClient) => { 236 console.info('delete inputStart notification.'); 237 }); 238``` 239 240### on('keyboardShow'|'keyboardHide') 241 242on(type: 'keyboardShow'|'keyboardHide', callback: () => void): void 243 244订阅输入法软键盘显示或隐藏事件。使用callback异步回调。 245 246**系统能力:** SystemCapability.MiscServices.InputMethodFramework 247 248**参数:** 249 250| 参数名 | 类型 | 必填 | 说明 | 251| -------- | ------ | ---- | ------------------------------------------------------------ | 252| type | string | 是 | 设置监听类型。<br/>-'keyboardShow'表示显示输入法软键盘。<br/>-'keyboardHide'表示隐藏输入法软键盘。 | 253| callback | () => void | 是 | 回调函数。 | 254 255**示例:** 256 257```ts 258inputMethodEngine.getInputMethodEngine().on('keyboardShow', () => { 259 console.info('inputMethodEngine keyboardShow.'); 260}); 261inputMethodEngine.getInputMethodEngine().on('keyboardHide', () => { 262 console.info('inputMethodEngine keyboardHide.'); 263}); 264``` 265 266### off('keyboardShow'|'keyboardHide') 267 268off(type: 'keyboardShow'|'keyboardHide', callback?: () => void): void 269 270取消订阅输入法软键盘显示或隐藏事件。使用callback异步回调。 271 272**系统能力:** SystemCapability.MiscServices.InputMethodFramework 273 274**参数:** 275 276| 参数名 | 类型 | 必填 | 说明 | 277| -------- | ------ | ---- | ------------------------------------------------------------ | 278| type | string | 是 | 要取消监听的输入法软键盘事件类型。<br/>-'keyboardShow'表示显示输入法软键盘。<br/>-'keyboardHide'表示隐藏输入法软键盘。| 279| callback | () => void | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 280 281**示例:** 282 283```ts 284inputMethodEngine.getInputMethodEngine().off('keyboardShow'); 285inputMethodEngine.getInputMethodEngine().off('keyboardHide'); 286``` 287 288## InputMethodAbility 289 290下列API均需使用[getInputMethodAbility](#inputmethodenginegetinputmethodability9)获取到InputMethodAbility实例后,通过实例调用。 291 292### on('inputStart')<sup>9+</sup> 293 294on(type: 'inputStart', callback: (kbController: KeyboardController, inputClient: InputClient) => void): void 295 296订阅输入法绑定成功事件。使用callback异步回调。 297 298**系统能力:** SystemCapability.MiscServices.InputMethodFramework 299 300**参数:** 301 302| 参数名 | 类型 | 必填 | 说明 | 303| -------- | ------------------------------- | ---- | ------------------------------------------------------------ | 304| type | string | 是 | 设置监听类型,固定取值为'inputStart'。 | 305| callback | (kbController: [KeyboardController](#keyboardcontroller), inputClient: [InputClient](#inputclient9)) => void | 是 | 回调函数,返回输入法操作相关实例。 | 306 307**示例:** 308 309```ts 310inputMethodEngine.getInputMethodAbility() 311 .on('inputStart', 312 (kbController: inputMethodEngine.KeyboardController, client: inputMethodEngine.InputClient) => { 313 let keyboardController: inputMethodEngine.KeyboardController = kbController; 314 let inputClient: inputMethodEngine.InputClient = client; 315 }); 316``` 317 318### off('inputStart')<sup>9+</sup> 319 320off(type: 'inputStart', callback?: (kbController: KeyboardController, inputClient: InputClient) => void): void 321 322取消订阅输入法绑定成功事件。使用callback异步回调。 323 324**系统能力:** SystemCapability.MiscServices.InputMethodFramework 325 326**参数:** 327 328| 参数名 | 类型 | 必填 | 说明 | 329| -------- | -------------------- | ---- | ------------------------ | 330| type | string | 是 | 设置监听类型,固定取值为'inputStart'。 | 331| callback | (kbController: [KeyboardController](#keyboardcontroller), inputClient: [InputClient](#inputclient9)) => void | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。| 332 333**示例:** 334 335```ts 336inputMethodEngine.getInputMethodAbility().off('inputStart'); 337``` 338 339### on('inputStop')<sup>9+</sup> 340 341on(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 | 是 | 回调函数。 | 353 354**示例:** 355 356```ts 357inputMethodEngine.getInputMethodAbility().on('inputStop', () => { 358 console.info('inputMethodAbility inputStop'); 359}); 360``` 361 362### off('inputStop')<sup>9+</sup> 363 364off(type: 'inputStop', callback: () => void): void 365 366取消订阅停止输入法应用事件。使用callback异步回调。 367 368**系统能力:** SystemCapability.MiscServices.InputMethodFramework 369 370**参数:** 371 372| 参数名 | 类型 | 必填 | 说明 | 373| -------- | ------ | ---- | ------------------------------------------------------------ | 374| type | string | 是 | 设置监听类型,固定取值为'inputStop'。 | 375| callback | () => void | 是 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 376 377**示例:** 378 379```ts 380inputMethodEngine.getInputMethodAbility().off('inputStop', () => { 381 console.info('inputMethodAbility delete inputStop notification.'); 382}); 383``` 384 385### on('setCallingWindow')<sup>9+</sup> 386 387on(type: 'setCallingWindow', callback: (wid: number) => void): void 388 389订阅设置调用窗口事件。使用callback异步回调。 390 391**系统能力:** SystemCapability.MiscServices.InputMethodFramework 392 393**参数:** 394 395| 参数名 | 类型 | 必填 | 说明 | 396| -------- | ------ | ---- | ------------------------------------------------------------ | 397| type | string | 是 | 设置监听类型,固定取值为'setCallingWindow'。 | 398| callback | (wid: number) => void | 是 | 回调函数,返回调用方窗口的Id。 | 399 400**示例:** 401 402```ts 403inputMethodEngine.getInputMethodAbility().on('setCallingWindow', (wid: number) => { 404 console.info('inputMethodAbility setCallingWindow'); 405}); 406``` 407 408### off('setCallingWindow')<sup>9+</sup> 409 410off(type: 'setCallingWindow', callback: (wid:number) => void): void 411 412取消订阅设置调用窗口事件。使用callback异步回调。 413 414**系统能力:** SystemCapability.MiscServices.InputMethodFramework 415 416**参数:** 417 418| 参数名 | 类型 | 必填 | 说明 | 419| -------- | ------ | ---- | ------------------------------------------------------------ | 420| type | string | 是 | 设置监听类型,固定取值为'setCallingWindow'。| 421| callback | (wid:number) => void | 是 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 422 423**示例:** 424 425```ts 426inputMethodEngine.getInputMethodAbility().off('setCallingWindow', (wid: number) => { 427 console.info('inputMethodAbility delete setCallingWindow notification.'); 428}); 429``` 430 431### on('keyboardShow'|'keyboardHide')<sup>9+</sup> 432 433on(type: 'keyboardShow'|'keyboardHide', callback: () => void): void 434 435订阅输入法软键盘显示或隐藏事件。使用callback异步回调。 436 437**系统能力:** SystemCapability.MiscServices.InputMethodFramework 438 439**参数:** 440 441| 参数名 | 类型 | 必填 | 说明 | 442| -------- | ------ | ---- | ------------------------------------------------------------ | 443| type | string | 是 | 设置监听类型。<br/>- 'keyboardShow'表示显示输入法软键盘。<br/>- 'keyboardHide'表示隐藏输入法软键盘。 | 444| callback | () => void | 是 | 回调函数。 | 445 446**示例:** 447 448```ts 449inputMethodEngine.getInputMethodAbility().on('keyboardShow', () => { 450 console.info('InputMethodAbility keyboardShow.'); 451}); 452inputMethodEngine.getInputMethodAbility().on('keyboardHide', () => { 453 console.info('InputMethodAbility keyboardHide.'); 454}); 455``` 456 457### off('keyboardShow'|'keyboardHide')<sup>9+</sup> 458 459off(type: 'keyboardShow'|'keyboardHide', callback?: () => void): void 460 461取消订阅输入法软键盘显示或隐藏事件。使用callback异步回调。 462 463**系统能力:** SystemCapability.MiscServices.InputMethodFramework 464 465**参数:** 466 467| 参数名 | 类型 | 必填 | 说明 | 468| -------- | ------ | ---- | ------------------------------------------------------------ | 469| type | string | 是 | 设置监听类型。<br/>- 'keyboardShow'表示显示键盘。<br/>- 'keyboardHide'表示隐藏键盘。 | 470| callback | () => void | 否 | 回调函数。 | 471 472**示例:** 473 474```ts 475inputMethodEngine.getInputMethodAbility().off('keyboardShow', () => { 476 console.info('InputMethodAbility delete keyboardShow notification.'); 477}); 478inputMethodEngine.getInputMethodAbility().off('keyboardHide', () => { 479 console.info('InputMethodAbility delete keyboardHide notification.'); 480}); 481``` 482 483### on('setSubtype')<sup>9+</sup> 484 485on(type: 'setSubtype', callback: (inputMethodSubtype: InputMethodSubtype) => void): void 486 487订阅设置输入法子类型事件。使用callback异步回调。 488 489**系统能力:** SystemCapability.MiscServices.InputMethodFramework 490 491**参数:** 492 493| 参数名 | 类型 | 必填 | 说明 | 494| -------- | --- | ---- | --- | 495| type | string | 是 | 设置监听类型,固定取值为'setSubtype'。 | 496| callback | (inputMethodSubtype: [InputMethodSubtype](js-apis-inputmethod-subtype.md)) => void | 是 | 回调函数,返回设置的输入法子类型。 | 497 498**示例:** 499 500```ts 501import { InputMethodSubtype } from '@kit.IMEKit'; 502 503inputMethodEngine.getInputMethodAbility().on('setSubtype', (inputMethodSubtype: InputMethodSubtype) => { 504 console.info('InputMethodAbility setSubtype.'); 505}); 506``` 507 508### off('setSubtype')<sup>9+</sup> 509 510off(type: 'setSubtype', callback?: (inputMethodSubtype: InputMethodSubtype) => void): void 511 512取消订阅设置输入法子类型事件。使用callback异步回调。 513 514**系统能力:** SystemCapability.MiscServices.InputMethodFramework 515 516**参数:** 517 518| 参数名 | 类型 | 必填 | 说明 | 519| ------- | ----- | ---- | ---- | 520| type | string | 是 | 设置监听类型,固定取值为'setSubtype'。 | 521| callback | (inputMethodSubtype: [InputMethodSubtype](js-apis-inputmethod-subtype.md)) => void | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 522 523**示例:** 524 525```ts 526inputMethodEngine.getInputMethodAbility().off('setSubtype', () => { 527 console.info('InputMethodAbility delete setSubtype notification.'); 528}); 529``` 530 531### on('securityModeChange')<sup>11+</sup> 532 533on(type: 'securityModeChange', callback: Callback< SecurityMode>): void 534 535订阅输入法安全模式改变类型事件。使用callback异步回调。 536 537**系统能力:** SystemCapability.MiscServices.InputMethodFramework 538 539**参数:** 540 541| 参数名 | 类型 | 必填 | 说明 | 542| -------- | ------------------------------------------- | ---- | ---------------------------------------------- | 543| type | string | 是 | 设置监听类型,固定取值为'securityModeChange'。 | 544| callback | Callback\<[SecurityMode](#securitymode11)> | 是 | 回调函数,返回当前输入法应用的安全模式。 | 545 546**示例:** 547 548```ts 549inputMethodEngine.getInputMethodAbility() 550 .on('securityModeChange', (securityMode: inputMethodEngine.SecurityMode) => { 551 console.info(`InputMethodAbility securityModeChange, security is ${securityMode}`); 552 }); 553``` 554 555### off('securityModeChange')<sup>11+</sup> 556 557off(type: 'securityModeChange', callback?: Callback< SecurityMode>): void 558 559取消订阅输入法安全模式改变类型事件。使用callback异步回调。 560 561**系统能力:** SystemCapability.MiscServices.InputMethodFramework 562 563**参数:** 564 565| 参数名 | 类型 | 必填 | 说明 | 566| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 567| type | string | 是 | 设置监听类型,固定取值为'securityModeChange'。 | 568| callback | Callback\<[SecurityMode](#securitymode11)> | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 569 570**示例:** 571 572```ts 573let securityChangeCallback: (securityMode: inputMethodEngine.SecurityMode) => void = 574 (securityMode: inputMethodEngine.SecurityMode) => { 575 console.info(`InputMethodAbility securityModeChange, security is ${securityMode}`); 576 }; 577let inputMethodAbility: inputMethodEngine.InputMethodAbility = inputMethodEngine.getInputMethodAbility(); 578inputMethodAbility.on('securityModeChange', securityChangeCallback); 579inputMethodAbility.off('securityModeChange', securityChangeCallback); 580``` 581 582### on('privateCommand')<sup>12+</sup> 583 584on(type: 'privateCommand', callback: Callback<Record<string, CommandDataType>>): void; 585 586订阅输入法私有数据事件。使用callback异步回调。 587 588**系统能力:** SystemCapability.MiscServices.InputMethodFramework 589 590**参数:** 591 592| 参数名 | 类型 | 必填 | 说明 | 593| -------- | --------------------------------------------- | ---- | ------------------------------------------ | 594| type | string | 是 | 设置监听类型,固定取值为'privateCommand'。 | 595| callback | Callback<Record<string, [CommandDataType](#commanddatatype12)>> | 是 | 回调函数,返回向输入法应用发送的私有数据。 | 596 597**错误码:** 598 599以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 600 601| 错误码ID | 错误信息 | 602| -------- | ---------------------------------------------- | 603| 12800010 | not the preconfigured default input method. | 604 605**示例:** 606 607```ts 608let privateCommandCallback: (record: Record<string, inputMethodEngine.CommandDataType>) => void = 609 (record: Record<string, inputMethodEngine.CommandDataType>) => { 610 for (let i :number = 0; i < record.length; i++) { 611 console.info(`private command key: ${i}, value: ${record[i]}`); 612 } 613 } 614inputMethodEngine.getInputMethodAbility().on('privateCommand', privateCommandCallback); 615``` 616 617### off('privateCommand')<sup>12+</sup> 618 619off(type: 'privateCommand', callback?: Callback<Record<string, CommandDataType>>): void 620 621取消订阅输入法私有数据事件。使用callback异步回调。 622 623**系统能力:** SystemCapability.MiscServices.InputMethodFramework 624 625**参数:** 626 627| 参数名 | 类型 | 必填 | 说明 | 628| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 629| type | string | 是 | 设置监听类型,固定取值为'privateCommand'。 | 630| callback | Callback<Record<string, [CommandDataType](#commanddatatype12)>> | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 631 632**错误码:** 633 634以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 635 636| 错误码ID | 错误信息 | 637| -------- | ---------------------------------------------- | 638| 12800010 | not the preconfigured default input method. | 639 640**示例:** 641 642```ts 643let privateCommandCallback: (record: Record<string, inputMethodEngine.CommandDataType>) => void = 644 (record: Record<string, inputMethodEngine.CommandDataType>) => { 645 for (let i: number = 0; i < record.length; i++) { 646 console.info(`private command key: ${i}, value: ${record[i]}`); 647 } 648 } 649 650inputMethodEngine.getInputMethodAbility().off('privateCommand', privateCommandCallback); 651``` 652 653### on('callingDisplayDidChange')<sup>18+</sup> 654 655on(type: 'callingDisplayDidChange', callback: Callback\<number>): void 656 657订阅编辑框对应窗口所在屏幕ID变化事件。使用callback异步回调。 658 659**系统能力:** SystemCapability.MiscServices.InputMethodFramework 660 661**参数:** 662 663| 参数名 | 类型 | 必填 | 说明 | 664| -------- | --------------------------------------------- | ---- | ------------------------------------------ | 665| type | string | 是 | 设置监听类型,固定取值为'callingDisplayDidChange'。 | 666| callback | Callback\<number> | 是 | 回调函数,返回编辑框设置对应窗口屏幕ID。 | 667 668**错误码:** 669 670以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 671 672| 错误码ID | 错误信息 | 673| -------- | ---------------------------------------------- | 674| 801 | capability not supported. | 675 676**示例:** 677 678```ts 679let callingDisplayDidChangeCallback: (num: number) => void = (num: number) => { 680 console.info(`display id: ${num}`); 681} 682inputMethodEngine.getInputMethodAbility().on('callingDisplayDidChange', callingDisplayDidChangeCallback); 683``` 684 685### off('callingDisplayDidChange')<sup>18+</sup> 686 687off(type: 'callingDisplayDidChange', callback?: Callback\<number>): void 688 689取消订阅编辑框对应窗口所在屏幕ID变化事件。使用callback异步回调。 690 691**系统能力:** SystemCapability.MiscServices.InputMethodFramework 692 693**参数:** 694 695| 参数名 | 类型 | 必填 | 说明 | 696| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 697| type | string | 是 | 设置监听类型,固定取值为'callingDisplayDidChange'。 | 698| callback | Callback\<number> | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 699 700**示例:** 701 702```ts 703inputMethodEngine.getInputMethodAbility().off('callingDisplayDidChange', (num: number) => { 704 console.info('InputMethodAbility delete calling display notification.'); 705}); 706``` 707 708### on('discardTypingText')<sup>20+</sup> 709 710on(type: 'discardTypingText', callback: Callback\<void>): void 711 712订阅编辑框应用发送“清空候选词”事件到输入法。使用callback异步回调。 713 714**系统能力:** SystemCapability.MiscServices.InputMethodFramework 715 716**参数:** 717 718| 参数名 | 类型 | 必填 | 说明 | 719| -------- | --------------------------------------------- | ---- | ------------------------------------------ | 720| type | string | 是 | 设置监听类型,固定取值为'discardTypingText'。<br/> - 'discardTypingText':表示订阅编辑框应用发送“清空候选词”事件到输入法。 | 721| callback | Callback\<void> | 是 | 回调函数。当命令发送成功,err为undefined,否则为错误对象。 | 722 723**示例:** 724 725```ts 726inputMethodEngine.getInputMethodAbility().on('discardTypingText', () => { 727 console.info('InputMethodAbility discard the typing text.'); 728}); 729``` 730 731### off('discardTypingText')<sup>20+</sup> 732 733off(type: 'discardTypingText', callback?: Callback\<void>): void 734 735取消订阅编辑框应用发送“清空候选词”事件到输入法。使用callback异步回调。 736 737**系统能力:** SystemCapability.MiscServices.InputMethodFramework 738 739**参数:** 740 741| 参数名 | 类型 | 必填 | 说明 | 742| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 743| type | string | 是 | 设置监听类型,固定取值为'discardTypingText'。<br/> - 'discardTypingText':表示取消订阅编辑框应用发送“清空候选词”事件到输入法。 | 744| callback | Callback\<void> | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 745 746**示例:** 747 748```ts 749inputMethodEngine.getInputMethodAbility().off('discardTypingText', () => { 750 console.info('InputMethodAbility discard the typing text.'); 751}); 752``` 753 754### getSecurityMode<sup>11+</sup> 755 756getSecurityMode(): SecurityMode 757 758获取输入法应用的当前安全模式。 759 760**系统能力:** SystemCapability.MiscServices.InputMethodFramework 761 762**返回值:** 763 764| 类型 | 说明 | 765| ------------------------------- | ---------- | 766| [SecurityMode](#securitymode11) | 安全模式。 | 767 768**错误码:** 769 770| 错误码ID | 错误信息 | 771| -------- | ------------------------------ | 772| 12800004 | not an input method application. | 773 774**示例:** 775 776```ts 777let security: inputMethodEngine.SecurityMode = inputMethodEngine.getInputMethodAbility().getSecurityMode(); 778console.error(`getSecurityMode, securityMode is : ${security}`); 779``` 780 781### createPanel<sup>10+</sup> 782 783createPanel(ctx: BaseContext, info: PanelInfo, callback: AsyncCallback\<Panel>): void 784 785创建输入法面板,仅支持输入法应用在[InputMethodExtensionAbility](js-apis-inputmethod-extension-ability.md#inputmethodextensionability)类中调用。使用callback异步回调。 786 787> **说明:** 788> 789> 单个输入法应用仅允许创建一个[软键盘类型](#paneltype10)和一个[状态栏类型](#paneltype10)的面板。<br> 790> 输入法面板不支持创建子窗口。例如:不支持使用[window.createWindow](../../windowmanager/application-window-fa.md#设置应用子窗口)、[bindContextMenu](../../reference/apis-arkui/arkui-ts/ts-universal-attributes-menu.md#bindcontextmenu8)、[CustomDialog](../../reference/apis-arkui/arkui-ts/ts-methods-custom-dialog-box.md)等接口创建子窗口弹窗。建议开发者采用非子窗的替代方案,如[弹出框](../../reference/apis-arkui/arkui-ts/ohos-arkui-advanced-Dialog.md)、[bindMenu](../../reference/apis-arkui/arkui-ts/ts-universal-attributes-menu.md#bindmenu)或设置showInSubwindow为false。 791 792**系统能力:** SystemCapability.MiscServices.InputMethodFramework 793 794**参数:** 795 796| 参数名 | 类型 | 必填 | 说明 | 797| ------- | ----------- | ---- | ------------------------ | 798| ctx | [BaseContext](../apis-ability-kit/js-apis-inner-application-baseContext.md) | 是 | 当前输入法应用上下文信息。 | 799| info | [PanelInfo](#panelinfo10) | 是 | 输入法应用信息。 | 800| callback | AsyncCallback\<[Panel](#panel10)> | 是 | 回调函数。当输入法面板创建成功,返回当前创建的输入法面板对象。 | 801 802**错误码:** 803 804| 错误码ID | 错误信息 | 805| ---------- | ----------------------------- | 806| 401 | parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 807| 12800004 | not an input method application. | 808 809**示例:** 810 811```ts 812import { BusinessError } from '@kit.BasicServicesKit'; 813 814let panelInfo: inputMethodEngine.PanelInfo = { 815 type: inputMethodEngine.PanelType.SOFT_KEYBOARD, 816 flag: inputMethodEngine.PanelFlag.FLG_FIXED 817} 818 819if (!this.context) { 820 inputMethodEngine.getInputMethodAbility() 821 .createPanel(this.context, panelInfo, (err: BusinessError, panel: inputMethodEngine.Panel) => { 822 if (err) { 823 console.error(`Failed to createPanel. Code is ${err.code}, message is ${err.message}`); 824 return; 825 } 826 console.info('Succeed in creating panel.'); 827 }) 828} 829``` 830 831### createPanel<sup>10+</sup> 832 833createPanel(ctx: BaseContext, info: PanelInfo): Promise\<Panel> 834 835创建输入法面板,仅支持输入法应用在[InputMethodExtensionAbility](js-apis-inputmethod-extension-ability.md#inputmethodextensionability)类中调用。使用promise异步回调。 836 837> **说明:** 838> 839> 单个输入法应用仅允许创建一个[软键盘类型](#paneltype10)和一个[状态栏类型](#paneltype10)的面板。<br> 840> 输入法面板不支持创建子窗口。例如:不支持使用[window.createWindow](../../windowmanager/application-window-fa.md#设置应用子窗口)、[bindContextMenu](../../reference/apis-arkui/arkui-ts/ts-universal-attributes-menu.md#bindcontextmenu8)、[CustomDialog](../../reference/apis-arkui/arkui-ts/ts-methods-custom-dialog-box.md)等接口创建子窗口弹窗。建议开发者采用非子窗的替代方案,如[弹出框](../../reference/apis-arkui/arkui-ts/ohos-arkui-advanced-Dialog.md)、[bindMenu](../../reference/apis-arkui/arkui-ts/ts-universal-attributes-menu.md#bindmenu)或设置showInSubwindow为false。 841 842**系统能力:** SystemCapability.MiscServices.InputMethodFramework 843 844**参数:** 845 846| 参数名 | 类型 | 必填 | 说明 | 847| ------- | ----------- | ---- | ------------------------ | 848| ctx | [BaseContext](../apis-ability-kit/js-apis-inner-application-baseContext.md) | 是 | 当前输入法应用上下文信息。 | 849| info | [PanelInfo](#panelinfo10) | 是 | 输入法面板信息。 | 850 851**返回值:** 852| 类型 | 说明 | 853| ------- | ------------------------------------------------------------------ | 854| Promise\<[Panel](#panel10)> | 回调函数。当输入法面板创建成功,返回当前创建的输入法面板对象。 | 855 856**错误码:** 857 858| 错误码ID | 错误信息 | 859| ---------- | ----------------------------- | 860| 401 | parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 861| 12800004 | not an input method application. | 862 863**示例:** 864 865```ts 866import { BusinessError } from '@kit.BasicServicesKit'; 867 868let panelInfo: inputMethodEngine.PanelInfo = { 869 type: inputMethodEngine.PanelType.SOFT_KEYBOARD, 870 flag: inputMethodEngine.PanelFlag.FLG_FIXED 871} 872 873if (this.context) { 874 inputMethodEngine.getInputMethodAbility().createPanel(this.context, panelInfo) 875 .then((panel: inputMethodEngine.Panel) => { 876 console.info('Succeed in creating panel.'); 877 }).catch((err: BusinessError) => { 878 console.error(`Failed to create panel. Code is ${err.code}, message is ${err.message}`); 879 }) 880} 881``` 882 883### destroyPanel<sup>10+</sup> 884 885destroyPanel(panel: Panel, callback: AsyncCallback\<void>): void 886 887销毁输入法面板。使用callback异步回调。 888 889**系统能力:** SystemCapability.MiscServices.InputMethodFramework 890 891**参数:** 892 893| 参数名 | 类型 | 必填 | 说明 | 894| ------- | ----------- | ---- | ------------------------ | 895| panel | [Panel](#panel10) | 是 | 要销毁的面板对象。 | 896| callback | AsyncCallback\<void> | 是 | 回调函数。当输入法面板销毁成功,err为undefined,否则为错误对象。 | 897 898**错误码:** 899 900以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 901 902| 错误码ID | 错误信息 | 903| -------- | ------------------------------------------------------- | 904| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 905 906**示例:** 907 908```ts 909import { BusinessError } from '@kit.BasicServicesKit'; 910 911let panelInfo: inputMethodEngine.PanelInfo = { 912 type: inputMethodEngine.PanelType.SOFT_KEYBOARD, 913 flag: inputMethodEngine.PanelFlag.FLG_FIXED 914} 915 916let inputPanel: inputMethodEngine.Panel | undefined = undefined; 917if (this.context) { 918 inputMethodEngine.getInputMethodAbility() 919 .createPanel(this.context, panelInfo, (err: BusinessError, panel: inputMethodEngine.Panel) => { 920 if (err) { 921 console.error(`Failed to create panel. Code is ${err.code}, message is ${err.message}`); 922 return; 923 } 924 inputPanel = panel; 925 console.info('Succeed in creating panel.'); 926 }) 927} 928 929if (inputPanel) { 930 inputMethodEngine.getInputMethodAbility().destroyPanel(inputPanel, (err: BusinessError) => { 931 if (err !== undefined) { 932 console.error(`Failed to destroy panel. Code is ${err.code}, message is ${err.message}`); 933 return; 934 } 935 console.info('Succeed in destroying panel.'); 936 }) 937} 938``` 939 940### destroyPanel<sup>10+</sup> 941 942destroyPanel(panel: Panel): Promise\<void> 943 944销毁输入法面板。使用promise异步回调。 945 946**系统能力:** SystemCapability.MiscServices.InputMethodFramework 947 948**参数:** 949 950| 参数名 | 类型 | 必填 | 说明 | 951| ---------| ----------- | ---- | ------------------------ | 952| panel | [Panel](#panel10) | 是 | 要销毁的面板对象。 | 953 954**返回值:** 955| 类型 | 说明 | 956| ------- | -------------------------------------------------------------------- | 957| Promise\<void> | 无返回结果的Promise对象。| 958 959**错误码:** 960 961以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 962 963| 错误码ID | 错误信息 | 964| -------- | ------------------------------------------------------- | 965| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 966 967**示例:** 968 969```ts 970import { BusinessError } from '@kit.BasicServicesKit'; 971 972let panelInfo: inputMethodEngine.PanelInfo = { 973 type: inputMethodEngine.PanelType.SOFT_KEYBOARD, 974 flag: inputMethodEngine.PanelFlag.FLG_FIXED 975} 976 977let inputPanel: inputMethodEngine.Panel | undefined = undefined; 978if (this.context) { 979 inputMethodEngine.getInputMethodAbility() 980 .createPanel(this.context, panelInfo, (err: BusinessError, panel: inputMethodEngine.Panel) => { 981 if (err) { 982 console.error(`Failed to create panel. Code is ${err.code}, message is ${err.message}`); 983 return; 984 } 985 inputPanel = panel; 986 console.info('Succeed in creating panel.'); 987 }) 988} 989 990if (inputPanel) { 991 inputMethodEngine.getInputMethodAbility().destroyPanel(inputPanel).then(() => { 992 console.info('Succeed in destroying panel.'); 993 }).catch((err: BusinessError) => { 994 console.error(`Failed to destroy panel. Code is ${err.code}, message is ${err.message}`); 995 }); 996} 997``` 998 999## KeyboardDelegate 1000 1001下列API均需使用[getKeyboardDelegate](#inputmethodenginegetkeyboarddelegate9)获取到KeyboardDelegate实例后,通过实例调用。 1002 1003### on('keyDown'|'keyUp') 1004 1005on(type: 'keyDown'|'keyUp', callback: (event: KeyEvent) => boolean): void 1006 1007订阅硬键盘(即物理键盘)上物理按键的按下或抬起事件。使用callback异步回调。 1008 1009**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1010 1011**参数:** 1012 1013| 参数名 | 类型 | 必填 | 说明 | 1014| -------- | ------------------------------- | ---- |-----------------------------------------------------| 1015| type | string | 是 | 设置监听类型。<br/>- 'keyDown'表示键盘按下。<br/>- 'keyUp'表示键盘抬起。 | 1016| callback | (event: [KeyEvent](#keyevent)) => boolean | 是 | 回调函数,返回按键信息。 若按键事件被事件订阅者消费,则callback应返回true,否则返回false。 | 1017 1018**示例:** 1019 1020```ts 1021inputMethodEngine.getKeyboardDelegate().on('keyUp', (keyEvent: inputMethodEngine.KeyEvent) => { 1022 console.info(`inputMethodEngine keyCode.(keyDown): ${keyEvent.keyCode}`); 1023 console.info(`inputMethodEngine keyAction.(keyDown): ${keyEvent.keyAction}`); 1024 return true; 1025}); 1026inputMethodEngine.getKeyboardDelegate().on('keyDown', (keyEvent: inputMethodEngine.KeyEvent) => { 1027 console.info(`inputMethodEngine keyCode.(keyDown): ${keyEvent.keyCode}`); 1028 console.info(`inputMethodEngine keyAction.(keyDown): ${keyEvent.keyAction}`); 1029 return true; 1030}); 1031``` 1032 1033### off('keyDown'|'keyUp') 1034 1035off(type: 'keyDown'|'keyUp', callback?: (event: KeyEvent) => boolean): void 1036 1037取消订阅硬键盘(即物理键盘)上物理按键的按下或抬起事件。使用callback异步回调。 1038 1039**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1040 1041**参数:** 1042 1043| 参数名 | 类型 | 必填 | 说明 | 1044| -------- | ------- | ---- | ----- | 1045| type | string | 是 | 设置监听类型。<br/>- 'keyDown'表示键盘按下。<br/>- 'keyUp'表示键盘抬起。 | 1046| callback | (event: [KeyEvent](#keyevent)) => boolean | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 1047 1048**示例:** 1049 1050```ts 1051inputMethodEngine.getKeyboardDelegate().off('keyUp', (keyEvent: inputMethodEngine.KeyEvent) => { 1052 console.info('delete keyUp notification.'); 1053 return true; 1054}); 1055inputMethodEngine.getKeyboardDelegate().off('keyDown', (keyEvent: inputMethodEngine.KeyEvent) => { 1056 console.info('delete keyDown notification.'); 1057 return true; 1058}); 1059``` 1060 1061### on('keyEvent')<sup>10+</sup> 1062 1063on(type: 'keyEvent', callback: (event: InputKeyEvent) => boolean): void 1064 1065订阅硬键盘(即物理键盘)事件。使用callback异步回调。 1066 1067**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1068 1069**参数:** 1070 1071| 参数名 | 类型 | 必填 | 说明 | 1072| -------- | -------- | ---- | ------------------------------------------------------------ | 1073| type | string | 是 | 设置监听类型,固定取值为'keyEvent'。 | 1074| callback | (event: [InputKeyEvent](../apis-input-kit/js-apis-keyevent.md#keyevent)) => boolean | 是 | 回调函数,入参为按键事件信息,返回值类型为布尔类型。<br/>- 入参按键事件信息的数据类型为[InputKeyEvent](../apis-input-kit/js-apis-keyevent.md#keyevent)。<br/>- 若按键事件被事件订阅者消费,则callback应返回true,否则返回false。| 1075 1076**示例:** 1077 1078```ts 1079import type { KeyEvent } from '@kit.InputKit'; 1080 1081inputMethodEngine.getKeyboardDelegate().on('keyEvent', (keyEvent: KeyEvent) => { 1082 console.info(`inputMethodEngine keyEvent.action:${ keyEvent.action}`); 1083 console.info(`inputMethodEngine keyEvent.key.code: ${keyEvent.key.code}`); 1084 console.info(`inputMethodEngine keyEvent.ctrlKey: ${keyEvent.ctrlKey}`); 1085 console.info(`inputMethodEngine keyEvent.unicodeChar: ${keyEvent.unicodeChar}`); 1086 return true; 1087}); 1088``` 1089 1090### off('keyEvent')<sup>10+</sup> 1091 1092off(type: 'keyEvent', callback?: (event: InputKeyEvent) => boolean): void 1093 1094取消订阅硬键盘(即物理键盘)事件。使用callback异步回调。 1095 1096**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1097 1098**参数:** 1099 1100| 参数名 | 类型 | 必填 | 说明 | 1101| -------- | -------- | ---- | ------------------------------------------------------------ | 1102| type | string | 是 | 设置监听类型,固定取值为'keyEvent'。 | 1103| callback | function | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。| 1104 1105**示例:** 1106 1107```ts 1108import type { KeyEvent } from '@kit.InputKit'; 1109 1110inputMethodEngine.getKeyboardDelegate().off('keyEvent', (keyEvent: KeyEvent) => { 1111 console.info('This is a callback function which will be deregistered.'); 1112 return true; 1113}); 1114inputMethodEngine.getKeyboardDelegate().off('keyEvent'); 1115``` 1116 1117### on('cursorContextChange') 1118 1119on(type: 'cursorContextChange', callback: (x: number, y:number, height:number) => void): void 1120 1121订阅光标变化事件。使用callback异步回调。 1122 1123**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1124 1125**参数:** 1126 1127| 参数名 | 类型 | 必填 | 说明 | 1128| -------- | ---- | ---- | ----- | 1129| type | string | 是 | 光标变化事件,固定取值为'cursorContextChange'。 | 1130| callback | (x: number, y: number, height: number) => void | 是 | 回调函数,返回光标信息。<br/>-x为光标上端的的x坐标值,y为光标上端的y坐标值,height为光标的高度值。 | 1131 1132**示例:** 1133 1134```ts 1135inputMethodEngine.getKeyboardDelegate().on('cursorContextChange', (x: number, y: number, height: number) => { 1136 console.info('inputMethodEngine cursorContextChange x:' + x); 1137 console.info('inputMethodEngine cursorContextChange y:' + y); 1138 console.info('inputMethodEngine cursorContextChange height:' + height); 1139}); 1140``` 1141 1142### off('cursorContextChange') 1143 1144off(type: 'cursorContextChange', callback?: (x: number, y: number, height: number) => void): void 1145 1146取消订阅光标变化事件。使用callback异步回调。 1147 1148**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1149 1150 **参数:** 1151 1152| 参数名 | 类型 | 必填 | 说明 | 1153| -------- | ---- | ---- | ------ | 1154| type | string | 是 | 光标变化事件,固定取值为'cursorContextChange'。 | 1155| callback | (x: number, y:number, height:number) => void | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 1156 1157 1158 **示例:** 1159 1160```ts 1161inputMethodEngine.getKeyboardDelegate().off('cursorContextChange', (x: number, y: number, height: number) => { 1162 console.info('delete cursorContextChange notification.'); 1163}); 1164``` 1165### on('selectionChange') 1166 1167on(type: 'selectionChange', callback: (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void): void 1168 1169订阅文本选择范围变化事件。使用callback异步回调。 1170 1171**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1172 1173**参数:** 1174 1175| 参数名 | 类型 | 必填 | 说明 | 1176| -------- | ----- | ---- | ---- | 1177| type | string | 是 | 文本选择变化事件,固定取值为'selectionChange'。 | 1178| callback | (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void | 是 | 回调函数,返回文本选择信息。<br/>- oldBegin为变化前被选中文本的起始下标,oldEnd为变化前被选中文本的终止下标。<br/>- newBegin为变化后被选中文本的起始下标,newEnd为变化后被选中文本的终止下标。| 1179 1180**示例:** 1181 1182```ts 1183inputMethodEngine.getKeyboardDelegate() 1184 .on('selectionChange', (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => { 1185 console.info('selectionChange oldBegin:' + oldBegin); 1186 console.info('selectionChange oldEnd:' + oldEnd); 1187 console.info('selectionChange newBegin:' + newBegin); 1188 console.info('selectionChange newEnd:' + newEnd); 1189 }); 1190``` 1191 1192### off('selectionChange') 1193 1194off(type: 'selectionChange', callback?: (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void): void 1195 1196取消订阅文本选择范围变化事件。使用callback异步回调。 1197 1198**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1199 1200**参数:** 1201 1202| 参数名 | 类型 | 必填 | 说明 | 1203| -------- | ------- | ---- | ------- | 1204| type | string | 是 | 文本选择变化事件,固定取值为'selectionChange'。 | 1205| callback | (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => void | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。| 1206 1207**示例:** 1208 1209```ts 1210inputMethodEngine.getKeyboardDelegate() 1211 .off('selectionChange', (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => { 1212 console.info('delete selectionChange notification.'); 1213 }); 1214``` 1215 1216 1217### on('textChange') 1218 1219on(type: 'textChange', callback: (text: string) => void): void 1220 1221订阅文本内容变化事件。使用callback异步回调。 1222 1223**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1224 1225**参数:** 1226 1227| 参数名 | 类型 | 必填 | 说明 | 1228| -------- | ------ | ---- | ------------------------------------------------------------ | 1229| type | string | 是 | 文本变化事件,固定取值为'textChange'。 | 1230| callback | (text: string) => void | 是 | 回调函数,返回订阅的文本内容。| 1231 1232**示例:** 1233 1234```ts 1235inputMethodEngine.getKeyboardDelegate().on('textChange', (text: string) => { 1236 console.info('inputMethodEngine textChange. text:' + text); 1237}); 1238``` 1239 1240### off('textChange') 1241 1242off(type: 'textChange', callback?: (text: string) => void): void 1243 1244取消订阅文本内容变化事件。使用callback异步回调。 1245 1246**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1247 1248**参数:** 1249 1250| 参数名 | 类型 | 必填 | 说明 | 1251| -------- | ------ | ---- | ------------------------------------------------------------ | 1252| type | string | 是 | 文本变化事件,固定取值为'textChange'。 | 1253| callback | (text: string) => void | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。| 1254 1255**示例:** 1256 1257```ts 1258inputMethodEngine.getKeyboardDelegate().off('textChange', (text: string) => { 1259 console.info('delete textChange notification. text:' + text); 1260}); 1261``` 1262 1263### on('editorAttributeChanged')<sup>10+</sup> 1264 1265on(type: 'editorAttributeChanged', callback: (attr: EditorAttribute) => void): void 1266 1267订阅编辑框属性变化事件。使用callback异步回调。 1268 1269**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1270 1271**参数:** 1272 1273| 参数名 | 类型 | 必填 | 说明 | 1274| -------- | ------ | ---- | ------------------------------------------------------------ | 1275| type | string | 是 | 文本变化事件,固定取值为'editorAttributeChanged'。 | 1276| callback | (attr: [EditorAttribute](#editorattribute)) => void | 是 | 回调函数,返回变化的编辑框属性。| 1277 1278**示例:** 1279 1280```ts 1281inputMethodEngine.getKeyboardDelegate() 1282 .on('editorAttributeChanged', (attr: inputMethodEngine.EditorAttribute) => { 1283 console.info(`Succeeded in receiving attribute of editor, inputPattern = ${attr.inputPattern}, enterKeyType = ${attr.enterKeyType}`); 1284 }); 1285``` 1286 1287### off('editorAttributeChanged')<sup>10+</sup> 1288 1289off(type: 'editorAttributeChanged', callback?: (attr: EditorAttribute) => void): void 1290 1291取消订阅编辑框属性变化事件。使用callback异步回调。 1292 1293**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1294 1295**参数:** 1296 1297| 参数名 | 类型 | 必填 | 说明 | 1298| -------- | ------ | ---- | ------------------------------------------------------------ | 1299| type | string | 是 | 文本变化事件,固定取值为'editorAttributeChanged'。 | 1300| callback | (attr: [EditorAttribute](#editorattribute)) => void | 否 | 所要取消订阅的回调处理函数。参数不填写时,默认取消订阅type对应的所有回调事件。 | 1301 1302**示例:** 1303 1304```ts 1305inputMethodEngine.getKeyboardDelegate().off('editorAttributeChanged'); 1306``` 1307 1308## Panel<sup>10+</sup> 1309 1310下列API均需使用[createPanel](#createpanel10)获取到Panel实例后,通过实例调用。 1311 1312### setUiContent<sup>10+</sup> 1313 1314setUiContent(path: string, callback: AsyncCallback\<void>): void 1315 1316为当前的输入法面板加载具体页面内容,使用callback异步回调。 1317 1318**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1319 1320**参数:** 1321 1322| 参数名 | 类型 | 必填 | 说明 | 1323| -------- | ---------------------- | ---- | -------- | 1324| path | string | 是 | 具体页面的路径。 | 1325| callback | AsyncCallback\<void> | 是 | 回调函数。当面板页面内容加载成功,err为undefined,否则err为错误对象。 | 1326 1327**错误码:** 1328 1329以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 1330 1331| 错误码ID | 错误信息 | 1332| -------- | ------------------------------------------------------- | 1333| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1334 1335**示例:** 1336 1337```ts 1338import { BusinessError } from '@kit.BasicServicesKit'; 1339 1340panel.setUiContent('pages/page2/page2', (err: BusinessError) => { 1341 if (err) { 1342 console.error(`Failed to setUiContent. Code is ${err.code}, message is ${err.message}`); 1343 return; 1344 } 1345 console.info('Succeeded in setting the content.'); 1346}); 1347``` 1348 1349### setUiContent<sup>10+</sup> 1350 1351setUiContent(path: string): Promise\<void> 1352 1353为当前的输入法面板加载具体页面内容,使用Promise异步回调。 1354 1355**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1356 1357**参数:** 1358 1359| 参数名 | 类型 | 必填 | 说明 | 1360| -------- | ---------------------- | ---- | -------- | 1361| path | string | 是 | 具体页面的路径。 | 1362 1363**返回值:** 1364 1365| 类型 | 说明 | 1366| ------- | ------------------------------ | 1367| Promise\<void> | 无返回结果的Promise对象。 | 1368 1369**错误码:** 1370 1371以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 1372 1373| 错误码ID | 错误信息 | 1374| -------- | ------------------------------------------------------- | 1375| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1376 1377**示例:** 1378 1379```ts 1380import { BusinessError } from '@kit.BasicServicesKit'; 1381 1382panel.setUiContent('pages/page2/page2').then(() => { 1383 console.info('Succeeded in setting the content.'); 1384}).catch((err: BusinessError) => { 1385 console.error(`Failed to setUiContent. Code is ${err.code}, message is ${err.message}`); 1386}); 1387``` 1388 1389### setUiContent<sup>10+</sup> 1390 1391setUiContent(path: string, storage: LocalStorage, callback: AsyncCallback\<void>): void 1392 1393为当前的输入法面板加载与LocalStorage相关联的具体页面内容,使用callback异步回调。 1394 1395**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1396 1397**参数:** 1398 1399| 参数名 | 类型 | 必填 | 说明 | 1400| -------- | ---------------------- | ---- | -------- | 1401| path | string | 是 | LocalStorage相关联的具体页面的路径。 | 1402| storage | [LocalStorage](../apis-arkui/arkui-ts/ts-state-management.md#localstorage9) | 是 | 存储单元,为应用程序范围内的可变和不可变状态属性提供存储。| 1403| callback | AsyncCallback\<void> | 是 | 回调函数。当面板页面内容加载成功,err为undefined,否则err为错误对象。 | 1404 1405**错误码:** 1406 1407以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 1408 1409| 错误码ID | 错误信息 | 1410| -------- | ------------------------------------------------------- | 1411| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1412 1413**示例:** 1414 1415```ts 1416import { BusinessError } from '@kit.BasicServicesKit'; 1417 1418let storage: LocalStorage = new LocalStorage(); 1419storage.setOrCreate('storageSimpleProp', 121); 1420panel.setUiContent('pages/page2/page2', storage, (err: BusinessError) => { 1421 if (err) { 1422 console.error(`Failed to setUiContent. Code is ${err.code}, message is ${err.message}`); 1423 return; 1424 } 1425 console.info('Succeeded in setting the content.'); 1426}); 1427``` 1428 1429### setUiContent<sup>10+</sup> 1430 1431setUiContent(path: string, storage: LocalStorage): Promise\<void> 1432 1433为当前面板加载与LocalStorage相关联的具体页面内容,使用Promise异步回调。 1434 1435**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1436 1437**参数:** 1438 1439| 参数名 | 类型 | 必填 | 说明 | 1440| -------- | ---------------------- | ---- | -------- | 1441| path | string | 是 | 设置加载页面的路径。 | 1442| storage | [LocalStorage](../apis-arkui/arkui-ts/ts-state-management.md#localstorage9) | 是 | 存储单元,为应用程序范围内的可变状态属性和非可变状态属性提供存储。| 1443 1444**返回值:** 1445 1446| 类型 | 说明 | 1447| ------- | ------------------------------ | 1448| Promise\<void> | 无返回结果的Promise对象。 | 1449 1450**错误码:** 1451 1452以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 1453 1454| 错误码ID | 错误信息 | 1455| -------- | ------------------------------------------------------- | 1456| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1457 1458**示例:** 1459 1460```ts 1461import { BusinessError } from '@kit.BasicServicesKit'; 1462 1463let storage: LocalStorage = new LocalStorage(); 1464storage.setOrCreate('storageSimpleProp', 121); 1465panel.setUiContent('pages/page2/page2', storage).then(() => { 1466 console.info('Succeeded in setting the content.'); 1467}).catch((err: BusinessError) => { 1468 console.error(`Failed to setUiContent. Code is ${err.code}, message is ${err.message}`); 1469}); 1470``` 1471 1472### resize<sup>10+</sup> 1473 1474resize(width: number, height: number, callback: AsyncCallback\<void>): void 1475 1476改变当前输入法面板的大小,使用callback异步回调。 1477 1478> **说明** 1479> 1480> 面板宽度不超出屏幕宽度,面板高度不高于屏幕高度的0.7倍。 1481 1482**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1483 1484**参数:** 1485 1486| 参数名 | 类型 | 必填 | 说明 | 1487| -------- | ---------------------- | ---- | -------- | 1488| width | number | 是 | 目标面板的宽度,单位为px。该参数应为大于或等于0的整数,不超出屏幕宽度。| 1489| height | number | 是 | 目标面板的高度,单位为px。该参数应为大于或等于0的整数,不高于屏幕高度的0.7倍。| 1490| callback | AsyncCallback\<void> | 是 | 回调函数。当面板大小改变成功,err为undefined,否则err为错误对象。 | 1491 1492**错误码:** 1493 1494以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 1495 1496| 错误码ID | 错误信息 | 1497| -------- | ------------------------------------------------------- | 1498| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1499 1500**示例:** 1501 1502```ts 1503import { BusinessError } from '@kit.BasicServicesKit'; 1504 1505panel.resize(500, 1000, (err: BusinessError) => { 1506 if (err) { 1507 console.error(`Failed to resize panel. Code is ${err.code}, message is ${err.message}`); 1508 return; 1509 } 1510 console.info('Succeeded in changing the panel size.'); 1511}); 1512``` 1513 1514### resize<sup>10+</sup> 1515 1516resize(width: number, height: number): Promise\<void> 1517 1518改变当前输入法面板的大小,使用Promise异步回调。 1519 1520> **说明** 1521> 1522> 面板宽度不超出屏幕宽度,面板高度不高于屏幕高度的0.7倍。 1523 1524**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1525 1526**参数:** 1527 1528| 参数名 | 类型 | 必填 | 说明 | 1529| -------- | ---------------------- | ---- | -------- | 1530| width | number | 是 | 目标面板的宽度,单位为px。该参数应为大于或等于0的整数,不超出屏幕宽度。| 1531| height | number | 是 | 目标面板的高度,单位为px。该参数应为大于或等于0的整数,不高于屏幕高度的0.7倍。| 1532 1533**返回值:** 1534 1535| 类型 | 说明 | 1536| ------- | ------------------------------ | 1537| Promise\<void> | 无返回结果的Promise对象。 | 1538 1539**错误码:** 1540 1541以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 1542 1543| 错误码ID | 错误信息 | 1544| -------- | ------------------------------------------------------- | 1545| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1546 1547**示例:** 1548 1549```ts 1550import { BusinessError } from '@kit.BasicServicesKit'; 1551 1552panel.resize(500, 1000).then(() => { 1553 console.info('Succeeded in changing the panel size.'); 1554}).catch((err: BusinessError) => { 1555 console.error(`Failed to resize panel. Code is ${err.code}, message is ${err.message}`); 1556}); 1557``` 1558 1559### moveTo<sup>10+</sup> 1560 1561moveTo(x: number, y: number, callback: AsyncCallback\<void>): void 1562 1563移动面板位置,使用callback异步回调。[面板状态](#panelflag10)为固定态时,不产生实际移动效果。 1564 1565**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1566 1567**参数:** 1568 1569| 参数名 | 类型 | 必填 | 说明 | 1570| -------- | ---------------------- | ---- | -------- | 1571| x | number | 是 | 横轴方向移动的值,值大于0表示右移,单位为px。该参数应为整数。| 1572| y | number | 是 | 纵轴方向移动的值,值大于0表示下移,单位为px。该参数应为整数。| 1573| callback | AsyncCallback\<void> | 是 | 回调函数。当面板位置移动成功,err为undefined,否则err为错误对象。 | 1574 1575**错误码:** 1576 1577以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 1578 1579| 错误码ID | 错误信息 | 1580| -------- | ------------------------------------------------------- | 1581| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1582 1583**示例:** 1584 1585```ts 1586import { BusinessError } from '@kit.BasicServicesKit'; 1587 1588panel.moveTo(300, 300, (err: BusinessError) => { 1589 if (err) { 1590 console.error(`Failed to move panel. Code is ${err.code}, message is ${err.message}`); 1591 return; 1592 } 1593 console.info('Succeeded in moving the panel.'); 1594}); 1595``` 1596 1597### moveTo<sup>10+</sup> 1598 1599moveTo(x: number, y: number): Promise\<void> 1600 1601移动面板位置,使用promise异步回调。[面板状态](#panelflag10)为固定态时,不产生实际移动效果。 1602 1603**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1604 1605**参数:** 1606 1607| 参数名 | 类型 | 必填 | 说明 | 1608| -------- | ---------------------- | ---- | -------- | 1609| x | number | 是 |横轴方向移动的值,值大于0表示右移,单位为px。该参数应为整数。| 1610| y | number | 是 |纵轴方向移动的值,值大于0表示下移,单位为px。该参数应为整数。| 1611 1612**返回值:** 1613 1614| 类型 | 说明 | 1615| ------- | ------------------------------ | 1616| Promise\<void> | 无返回结果的Promise对象。 | 1617 1618**错误码:** 1619 1620以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 1621 1622| 错误码ID | 错误信息 | 1623| -------- | ------------------------------------------------------- | 1624| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1625 1626**示例:** 1627 1628```ts 1629import { BusinessError } from '@kit.BasicServicesKit'; 1630 1631panel.moveTo(300, 300).then(() => { 1632 console.info('Succeeded in moving the panel.'); 1633}).catch((err: BusinessError) => { 1634 console.error(`Failed to move panel. Code is ${err.code}, message is ${err.message}`); 1635}); 1636``` 1637 1638### startMoving<sup>15+</sup> 1639 1640startMoving(): void 1641 1642发送移动命令给窗口,不产生实际移动效果(仅在鼠标点击作用才可以移动)。 1643 1644**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1645 1646**错误码:** 1647 1648以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 1649 1650| 错误码ID | 错误信息 | 1651| -------- | ------------------------------------------------------- | 1652| 801 | capability not supported. | 1653| 12800002 | input method engine error. Possible causes: 1.input method panel not created. 2.the input method application does not subscribe to related events. | 1654| 12800013 | window manager service error. | 1655| 12800017 | invalid panel type or panel flag. | 1656 1657**示例:** 1658 1659```ts 1660panel.startMoving(); 1661``` 1662 1663### getDisplayId<sup>15+</sup> 1664 1665getDisplayId(): Promise\<number> 1666 1667获取当前窗口的所在id,使用Promise异步回调。 1668 1669**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1670 1671**返回值:** 1672 1673| 类型 | 说明 | 1674| ------- | ------------------------------ | 1675|Promise\<number>| Promise对象。返回窗口的displayId。 | 1676 1677**错误码:** 1678 1679以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 1680 1681| 错误码ID | 错误信息 | 1682| -------- | ------------------------------------------------------- | 1683| 12800002 | input method engine error. Possible causes: 1.input method panel not created. 2.the input method application does not subscribe to related events. | 1684| 12800013 | window manager service error. | 1685 1686**示例:** 1687 1688```ts 1689import { BusinessError } from '@kit.BasicServicesKit'; 1690 1691panel.getDisplayId().then((result: number) => { 1692 console.info('get displayId:' + result); 1693}).catch((err: BusinessError) => { 1694 console.error(`Failed to get displayId. Code is ${err.code}, message is ${err.message}`); 1695}); 1696``` 1697 1698### show<sup>10+</sup> 1699 1700show(callback: AsyncCallback\<void>): void 1701 1702显示当前输入法面板,使用callback异步回调。输入法应用与编辑框绑定成功后可正常调用。 1703 1704**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1705 1706**参数:** 1707 1708| 参数名 | 类型 | 必填 | 说明 | 1709| -------- | ---------------------- | ---- | -------- | 1710| callback | AsyncCallback\<void> | 是 | 回调函数。当面板显示成功,err为undefined,否则err为错误对象。 | 1711 1712**示例:** 1713 1714```ts 1715import { BusinessError } from '@kit.BasicServicesKit'; 1716 1717panel.show((err: BusinessError) => { 1718 if (err) { 1719 console.error(`Failed to show panel. Code is ${err.code}, message is ${err.message}`); 1720 return; 1721 } 1722 console.info('Succeeded in showing the panel.'); 1723}); 1724``` 1725 1726### show<sup>10+</sup> 1727 1728show(): Promise\<void> 1729 1730显示当前输入法面板,使用promise异步回调。输入法应用与编辑框绑定成功后可正常调用。 1731 1732**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1733 1734**返回值:** 1735 1736| 类型 | 说明 | 1737| ------- | ------------------------------ | 1738| Promise\<void> | 无返回结果的Promise对象。 | 1739 1740**示例:** 1741 1742```ts 1743import { BusinessError } from '@kit.BasicServicesKit'; 1744 1745panel.show().then(() => { 1746 console.info('Succeeded in showing the panel.'); 1747}).catch((err: BusinessError) => { 1748 console.error(`Failed to show panel. Code is ${err.code}, message is ${err.message}`); 1749}); 1750``` 1751 1752### hide<sup>10+</sup> 1753 1754hide(callback: AsyncCallback\<void>): void 1755 1756隐藏当前输入法面板,使用callback异步回调。 1757 1758**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1759 1760**参数:** 1761 1762| 参数名 | 类型 | 必填 | 说明 | 1763| -------- | ---------------------- | ---- | -------- | 1764| callback | AsyncCallback\<void> | 是 | 回调函数。当面板隐藏成功,err为undefined,否则err为错误对象。 | 1765 1766**示例:** 1767 1768```ts 1769import { BusinessError } from '@kit.BasicServicesKit'; 1770 1771panel.hide((err: BusinessError) => { 1772 if (err) { 1773 console.error(`Failed to hide panel. Code is ${err.code}, message is ${err.message}`); 1774 return; 1775 } 1776 console.info('Succeeded in hiding the panel.'); 1777}); 1778``` 1779 1780### hide<sup>10+</sup> 1781 1782hide(): Promise\<void> 1783 1784隐藏当前输入法面板,使用promise异步回调。 1785 1786**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1787 1788**返回值:** 1789 1790| 类型 | 说明 | 1791| ------- | ------------------------------ | 1792| Promise\<void> | 无返回结果的Promise对象。 | 1793 1794**示例:** 1795 1796```ts 1797import { BusinessError } from '@kit.BasicServicesKit'; 1798 1799panel.hide().then(() => { 1800 console.info('Succeeded in hiding the panel.'); 1801}).catch((err: BusinessError) => { 1802 console.error(`Failed to hide panel. Code is ${err.code}, message is ${err.message}`); 1803}); 1804``` 1805 1806### adjustPanelRect<sup>12+</sup> 1807 1808adjustPanelRect(flag: PanelFlag, rect: PanelRect): void 1809 1810预设置输入法应用横竖屏大小。接口调用完毕表示adjust请求已提交到输入法框架,不表示执行完毕。 1811 1812**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1813 1814**参数:** 1815 1816| 参数名 | 类型 | 必填 | 说明 | 1817| -------- | ---------------------- | ---- | -------- | 1818| flag | [PanelFlag](#panelflag10) | 是 | 目标面板状态类型。类型为FLG_FIXED或FLG_FLOATING。 | 1819| rect | [PanelRect](#panelrect12) | 是 | 目标面板横屏状态及竖屏状态的横坐标,纵坐标,宽度以及高度。固定态:高度不能超过屏幕高度的70%,宽度不能超过屏幕宽度;悬浮态:高度不能超过屏幕高度,宽度不能超过屏幕宽度。| 1820 1821**错误码:** 1822 1823以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 1824 1825| 错误码ID | 错误信息 | 1826| -------- | ------------------------------------------------------- | 1827| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1828| 12800013 | window manager service error. | 1829 1830**示例:** 1831 1832```ts 1833import { window } from '@kit.ArkUI'; 1834 1835let landscapeRect: window.Rect = { 1836 left: 100, 1837 top: 100, 1838 width: 400, 1839 height: 400 1840}; 1841 1842let portraitRect: window.Rect = { 1843 left: 200, 1844 top: 200, 1845 width: 300, 1846 height: 300 1847}; 1848 1849let panelFlag: inputMethodEngine.PanelFlag = inputMethodEngine.PanelFlag.FLG_FIXED; 1850let panelRect: inputMethodEngine.PanelRect = { 1851 landscapeRect: landscapeRect, 1852 portraitRect: portraitRect 1853}; 1854panel.adjustPanelRect(panelFlag, panelRect); 1855``` 1856 1857### adjustPanelRect<sup>15+</sup> 1858 1859adjustPanelRect(flag: PanelFlag, rect: EnhancedPanelRect): void 1860 1861预设置输入法应用横竖屏大小、位置、自定义避让区域以及热区。 1862 1863> **说明:** 1864> 1865> 仅用于SOFT_KEYBOARD类型,状态为FLG_FIXED或FLG_FLOATING的面板。此接口兼容[adjustPanelRect](#adjustpanelrect12)的调用方法,若入参rect仅填写属性landscapeRect和portraitRect,则默认调用[adjustPanelRect](#adjustpanelrect12)。 1866> 1867> 此接口为同步接口,接口返回仅代表系统侧收到设置的请求,不代表已完成设置。 1868 1869**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1870 1871**参数:** 1872 1873| 参数名 | 类型 | 必填 | 说明 | 1874| ------ | ----------------------------------------- | ---- | ---------------------------------------------------------- | 1875| flag | [PanelFlag](#panelflag10) | 是 | 目标面板状态类型。类型为FLG_FIXED或FLG_FLOATING。 | 1876| rect | [EnhancedPanelRect](#enhancedpanelrect15) | 是 | 目标面板横屏状态及竖屏状态的位置、大小、避让区域以及热区。 | 1877 1878**错误码:** 1879 1880以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 1881 1882| 错误码ID | 错误信息 | 1883| -------- | ------------------------------------------------------------ | 1884| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1885| 12800013 | window manager service error. | 1886| 12800017 | invalid panel type or panel flag. | 1887 1888**示例:** 1889 1890```ts 1891import { window } from '@kit.ArkUI'; 1892 1893let landscapeRect1: window.Rect = { 1894 left: 300, 1895 top: 650, 1896 width: 2000, 1897 height: 500 1898}; 1899let landscapeInputRegion: Array<window.Rect> = [landscapeRect1]; 1900 1901let portraitRect1: window.Rect = { 1902 left: 0, 1903 top: 1800, 1904 width: 1200, 1905 height: 800 1906} 1907let portraitInputRegion: Array<window.Rect> = [portraitRect1]; 1908 1909let panelFlag: inputMethodEngine.PanelFlag = inputMethodEngine.PanelFlag.FLG_FIXED; 1910let panelRect: inputMethodEngine.EnhancedPanelRect = { 1911 landscapeAvoidY: 650, 1912 landscapeInputRegion: landscapeInputRegion, 1913 portraitAvoidY: 1800, 1914 portraitInputRegion: portraitInputRegion, 1915 fullScreenMode: true 1916}; 1917panel.adjustPanelRect(panelFlag, panelRect); 1918``` 1919 1920### updateRegion<sup>15+</sup> 1921 1922updateRegion(inputRegion: Array<window.Rect>): void 1923 1924更新当前状态下输入法面板内的热区。 1925 1926> **说明:** 1927> 1928> 仅用于SOFT_KEYBOARD类型,状态为FLG_FIXED或FLG_FLOATING的面板。 1929> 1930> 此接口为同步接口,接口返回仅代表系统侧收到更新热区的请求,不代表已完成热区更新。 1931 1932**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1933 1934**参数:** 1935 1936| 参数名 | 类型 | 必填 | 说明 | 1937| ----------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1938| inputRegion | Array<[window.Rect](../apis-arkui/arkts-apis-window-i.md#rect7)> | 是 | 面板内接收输入事件的区域。<br/>- 数组大小限制为[1, 4]。<br/>- 传入的热区位置是相对于输入法面板窗口左顶点的位置。 | 1939 1940**错误码:** 1941 1942以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 1943 1944| 错误码ID | 错误信息 | 1945| -------- | ------------------------------------------------------------ | 1946| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1947| 12800013 | window manager service error. | 1948| 12800017 | invalid panel type or panel flag. | 1949 1950**示例:** 1951 1952```ts 1953import { BusinessError } from '@kit.BasicServicesKit'; 1954import { window } from '@kit.ArkUI'; 1955 1956let inputRegion: Array<window.Rect> = [{ 1957 left: 300, 1958 top: 650, 1959 width: 2000, 1960 height: 500 1961}]; 1962panel.updateRegion(inputRegion); 1963``` 1964 1965### on('show')<sup>10+</sup> 1966 1967on(type: 'show', callback: () => void): void 1968 1969监听当前面板显示状态,使用callback异步回调。 1970 1971**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1972 1973**参数:** 1974 1975| 参数名 | 类型 | 必填 | 说明 | 1976| -------- | ---------------------- | ---- | -------- | 1977| type | string | 是 | 监听当前面板的状态类型,固定取值为'show'。 | 1978| callback | () => void | 是 | 回调函数。 | 1979 1980**示例:** 1981 1982```ts 1983panel.on('show', () => { 1984 console.info('Panel is showing.'); 1985}); 1986``` 1987 1988### on('hide')<sup>10+</sup> 1989 1990on(type: 'hide', callback: () => void): void 1991 1992监听当前面板隐藏状态,使用callback异步回调。 1993 1994**系统能力:** SystemCapability.MiscServices.InputMethodFramework 1995 1996**参数:** 1997 1998| 参数名 | 类型 | 必填 | 说明 | 1999| -------- | ---------------------- | ---- | -------- | 2000| type | string | 是 | 监听当前面板的状态类型,固定取值为'hide'。 | 2001| callback | () => void | 是 | 回调函数。 | 2002 2003**示例:** 2004 2005```ts 2006panel.on('hide', () => { 2007 console.info('Panel is hiding.'); 2008}); 2009``` 2010 2011### on('sizeChange')<sup>12+</sup> 2012 2013on(type: 'sizeChange', callback: SizeChangeCallback): void 2014 2015监听当前面板大小变化,使用callback异步回调。 2016 2017> **说明:** 2018> 2019> 仅用于SOFT_KEYBOARD类型,状态为FLG_FIXED或FLG_FLOATING的面板。输入法通过adjustPanelRect等接口对面板大小进行调节时,系统会根据一定规则校验计算出最终的数值(例如超出屏幕等场景),输入法应用可通过该回调获取的真实面板大小,完成最终的面板布局刷新。 2020> 2021>- 从API version 12-14开始支持,此接口回调函数中仅包含[window.Size](../apis-arkui/arkts-apis-window-i.md#size7)类型的必选参数。 2022>- 从API version 15起,调用[adjustPanelRect](#adjustpanelrect15)接口后,此接口回调函数增加[KeyboardArea](#keyboardarea15)类型的可选参数。 2023 2024**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2025 2026**参数:** 2027 2028| 参数名 | 类型 | 必填 | 说明 | 2029| -------- | ------------------------------------------- | ---- | ------------------------------------------------------ | 2030| type | string | 是 | 监听当前面板的大小是否产生变化,固定值为'sizeChange'。 | 2031| callback | [SizeChangeCallback](#sizechangecallback15) | 是 | 回调函数。返回当前软键盘面板的大小,包含宽度和高度值。 | 2032 2033**示例:** 2034 2035```ts 2036import { window } from '@kit.ArkUI'; 2037 2038panel.on('sizeChange', (windowSize: window.Size) => { 2039 console.info(`panel size changed, width: ${windowSize.width}, height: ${windowSize.height}`); 2040}); 2041 2042panel.on('sizeChange', (windowSize: window.Size, keyboardArea: inputMethodEngine.KeyboardArea) => { 2043 console.info(`panel size changed, windowSize: ${windowSize.width}, ${windowSize.height}, ` + 2044 `keyboardArea: ${keyboardArea.top}, ${keyboardArea.bottom}, ${keyboardArea.left}, ${keyboardArea.right}`); 2045}); 2046``` 2047 2048### off('show')<sup>10+</sup> 2049 2050off(type: 'show', callback?: () => void): void 2051 2052取消监听当前面板的显示状态,使用callback异步回调。 2053 2054**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2055 2056**参数:** 2057 2058| 参数名 | 类型 | 必填 | 说明 | 2059| -------- | ---------------------- | ---- | -------- | 2060| type | string | 是 | 取消监听当前面板的状态类型,固定取值为'show'。 | 2061| callback | () => void | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 2062 2063**错误码:** 2064 2065以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 2066 2067| 错误码ID | 错误信息 | 2068| -------- | ------------------------------------------------------- | 2069| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2070 2071**示例:** 2072 2073```ts 2074panel.off('show'); 2075``` 2076 2077### off('hide')<sup>10+</sup> 2078 2079off(type: 'hide', callback?: () => void): void 2080 2081取消监听当前面板的隐藏状态,使用callback异步回调。 2082 2083**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2084 2085**参数:** 2086 2087| 参数名 | 类型 | 必填 | 说明 | 2088| -------- | ---------------------- | ---- | -------- | 2089| type | string | 是 | 要取消监听的当前面板状态类型,固定取值为'hide'。 | 2090| callback | () => void | 否 | 取消订阅的回调函数。参数不填写时,取消订阅type对应的所有回调事件。 | 2091 2092**错误码:** 2093 2094以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 2095 2096| 错误码ID | 错误信息 | 2097| -------- | ------------------------------------------------------- | 2098| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2099 2100**示例:** 2101 2102```ts 2103panel.off('hide'); 2104``` 2105 2106### off('sizeChange')<sup>12+</sup> 2107 2108off(type: 'sizeChange', callback?: SizeChangeCallback): void 2109 2110取消监听当前面板大小变化,使用callback异步回调。 2111 2112> **说明:** 2113> 2114> 仅用于SOFT_KEYBOARD类型,状态为FLG_FIXED或FLG_FLOATING的面板。输入法通过adjustPanelRect等接口对面板大小进行调节时,系统会根据一定规则校验计算出最终的数值(例如超出屏幕等场景),输入法应用可通过该回调获取的真实面板大小,完成最终的面板布局刷新。 2115> 2116>- 从API version 12-14开始支持,此接口回调函数中仅包含[window.Size](../apis-arkui/arkts-apis-window-i.md#size7)类型的必选参数。 2117>- 从API version 15起,调用[adjustPanelRect](#adjustpanelrect15)接口后,此接口回调函数增加[KeyboardArea](#keyboardarea15)类型的可选参数。 2118 2119**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2120 2121**参数:** 2122 2123| 参数名 | 类型 | 必填 | 说明 | 2124| -------- | ------------------------------------------- | ---- | -------------------------------------------------------- | 2125| type | string | 是 | 监听当前面板的大小是否产生变化,固定取值为'sizeChange'。 | 2126| callback | [SizeChangeCallback](#sizechangecallback15) | 否 | 回调函数。返回当前软键盘面板的大小,包含宽度和高度值。 | 2127 2128**示例:** 2129 2130```ts 2131import { window } from '@kit.ArkUI'; 2132 2133panel.off('sizeChange', (windowSize: window.Size) => { 2134 console.info(`panel size changed, width: ${windowSize.width}, height: ${windowSize.height}`); 2135}); 2136``` 2137 2138### changeFlag<sup>10+</sup> 2139 2140changeFlag(flag: PanelFlag): void 2141 2142将输入法应用的面板状态改变为固定态或者悬浮态,仅对[SOFT_KEYBOARD](#paneltype10)生效。 2143 2144**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2145 2146**参数:** 2147 2148| 参数名 | 类型 | 必填 | 说明 | 2149| -------- | ---------------------- | ---- | -------- | 2150| flag | [PanelFlag](#panelflag10) | 是 | 目标面板状态类型。 | 2151 2152**错误码:** 2153 2154以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 2155 2156| 错误码ID | 错误信息 | 2157| -------- | ------------------------------------------------------- | 2158| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2159 2160**示例:** 2161 2162```ts 2163let panelFlag: inputMethodEngine.PanelFlag = inputMethodEngine.PanelFlag.FLG_FIXED; 2164panel.changeFlag(panelFlag); 2165``` 2166 2167### setPrivacyMode<sup>11+</sup> 2168 2169setPrivacyMode(isPrivacyMode: boolean): void 2170 2171将输入法应用的面板设置为隐私模式,隐私模式不可被录屏、截屏。 2172 2173**需要权限:** ohos.permission.PRIVACY_WINDOW 2174 2175**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2176 2177**参数:** 2178 2179| 参数名 | 类型 | 必填 | 说明 | 2180| ------------- | ------- | ---- | ------------------ | 2181| isPrivacyMode | boolean | 是 | 是否设置隐私模式。<br/>- 值为true,表示将设置为隐私模式。<br/>- 值为false,表示将设置为非隐私模式。 | 2182 2183**错误码:** 2184 2185以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)。 2186 2187| 错误码ID | 错误信息 | 2188| -------- | ------------------------------------------------------- | 2189| 201 | permissions check fails. | 2190| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2191 2192**示例:** 2193 2194```ts 2195let isPrivacyMode: boolean = true; 2196panel.setPrivacyMode(isPrivacyMode); 2197``` 2198 2199### setImmersiveMode<sup>15+</sup> 2200 2201setImmersiveMode(mode: ImmersiveMode): void 2202 2203设置输入法应用的沉浸模式。只能设置不使用沉浸模式(NONE_IMMERSIVE)、浅色沉浸模式(LIGHT_IMMERSIVE)或深色沉浸模式(DARK_IMMERSIVE)。不能设置为沉浸模式(IMMERSIVE)。 2204 2205**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2206 2207**参数:** 2208 2209| 参数名 | 类型 | 必填 | 说明 | 2210| -------- | ---------------------- | ---- | -------- | 2211| mode | [ImmersiveMode](#immersivemode15) | 是 | 沉浸模式。 | 2212 2213**错误码:** 2214 2215以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[输入法框架错误码](errorcode-inputmethod-framework.md)。 2216 2217| 错误码ID | 错误信息 | 2218| -------- | ------------------------------------------------------- | 2219| 401 | parameter error. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2220| 12800002 | input method engine error. Possible causes: 1.input method panel not created. 2.the input method application does not subscribe to related events. | 2221| 12800013 | window manager service error. | 2222 2223**示例:** 2224 2225```ts 2226panel.setImmersiveMode(inputMethodEngine.ImmersiveMode.LIGHT_IMMERSIVE); 2227``` 2228 2229### getImmersiveMode<sup>15+</sup> 2230 2231getImmersiveMode(): ImmersiveMode 2232 2233获取输入法应用的沉浸模式。 2234 2235**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2236 2237**返回值:** 2238 2239| 类型 | 说明 | 2240| ------------------------------- | ---------- | 2241| [ImmersiveMode](#immersivemode15) | 沉浸模式。 | 2242 2243**示例:** 2244 2245```ts 2246let mode: inputMethodEngine.ImmersiveMode = panel.getImmersiveMode(); 2247``` 2248 2249### setImmersiveEffect<sup>20+</sup> 2250 2251setImmersiveEffect(effect: ImmersiveEffect): void 2252 2253设置输入法应用的沉浸效果。 2254- 只有在[启用沉浸式模式](#setimmersivemode15)时,才能使用渐变模式和流光模式。 2255- 只有在启用渐变模式时,才能使用流光模式。 2256- 未启用渐变模式时,渐变高度必须为0px。 2257- 只有系统应用才能设置流光模式。 2258- 必须先调用以下任一接口,才能调用当前接口: 2259 - [adjustPanelRect](#adjustpanelrect12)(支持API version 12) 2260 - [adjustPanelRect](#adjustpanelrect15)(支持API version 15) 2261 - [resize](#resize10)(支持API version 10) 2262 2263**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2264 2265**参数:** 2266 2267| 参数名 | 类型 | 必填 | 说明 | 2268| -------- | ---------------------- | ---- | -------- | 2269| effect | [ImmersiveEffect](#immersiveeffect20) | 是 | 沉浸效果。 | 2270 2271**错误码:** 2272 2273以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[输入法框架错误码](errorcode-inputmethod-framework.md)。 2274 2275| 错误码ID | 错误信息 | 2276| -------- | ------------------------------------------------------- | 2277| 801 |capability not supported. | 2278| 12800002 |input method engine error. Possible causes:1.input method panel not created. 2.the input method application does not subscribe to related events. | 2279| 12800013 |window manager service error. | 2280| 12800020 |invalid immersive effect. 1.The gradient mode and the fluid light mode can only be used when the immersive mode is enabled. 2.The fluid light mode can only be used when the gradient mode is enabled. 3.When the gradient mode is not enabled, the gradient height can only be 0. | 2281| 12800021 |this operation is allowed only after adjustPanelRect or resize is called. | 2282 2283**示例:** 2284 2285```ts 2286let effect: inputMethodEngine.ImmersiveEffect = { 2287 gradientHeight: 100, 2288 gradientMode: inputMethodEngine.GradientMode.LINEAR_GRADIENT 2289} 2290panel.setImmersiveEffect(effect); 2291``` 2292 2293### setKeepScreenOn<sup>20+</sup> 2294 2295setKeepScreenOn(isKeepScreenOn: boolean): Promise\<void> 2296 2297设置屏幕常亮。使用Promise异步回调。 2298 2299> **说明:** 2300> 2301> - 当键盘拉起时设置常亮生效,键盘关闭则自动失效。 2302> - 规范使用该接口:必要场景(例如:语音输入)下,设置该属性为true;退出必要场景后,重置该属性为false;其他场景下,不使用该接口。 2303 2304**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2305 2306**参数:** 2307 2308| 参数名 | 类型 | 必填 | 说明 | 2309| -------- | ---------------------- | ---- | -------- | 2310| isKeepScreenOn | boolean | 是 | 是否设置屏幕常亮。true表示打开屏幕常亮,false表示关闭屏幕常亮。 | 2311 2312**返回值:** 2313 2314| 类型 | 说明 | 2315| ------- | ------------------------------ | 2316| Promise\<void> | 无返回结果的Promise对象。 | 2317 2318**错误码:** 2319 2320以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 2321 2322| 错误码ID | 错误信息 | 2323| -------- | ------------------------------------------------------- | 2324| 12800013 | window manager service error. | 2325 2326**示例:** 2327 2328```ts 2329import { BusinessError } from '@kit.BasicServicesKit'; 2330 2331panel.setKeepScreenOn(true).then(() => { 2332 console.info(`setKeepScreenOn success.`); 2333}).catch((error: BusinessError) => { 2334 console.error(`setKeepScreenOn failed, code: ${error.code}, message: ${error.message}`); 2335}) 2336``` 2337 2338## KeyboardController 2339 2340下列API均需使用[on('inputStart')](#oninputstart9)获取到KeyboardController实例后,通过实例调用。 2341 2342### hide<sup>9+</sup> 2343 2344hide(callback: AsyncCallback<void>): void 2345 2346隐藏输入法。使用callback异步回调。 2347 2348**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2349 2350**参数:** 2351 2352| 参数名 | 类型 | 必填 | 说明 | 2353| -------- | ---------------------- | ---- | -------- | 2354| callback | AsyncCallback<void> | 是 | 回调函数。当输入法隐藏成功,err为undefined,否则为错误对象。 | 2355 2356**错误码:** 2357 2358以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 2359 2360| 错误码ID | 错误信息 | 2361| -------- | -------------------------- | 2362| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 2363 2364**示例:** 2365 2366```ts 2367import { BusinessError } from '@kit.BasicServicesKit'; 2368 2369keyboardController.hide((err: BusinessError) => { 2370 if (err) { 2371 console.error(`Failed to hide. Code:${err.code}, message:${err.message}`); 2372 return; 2373 } 2374 console.info('Succeeded in hiding keyboard.'); 2375}); 2376``` 2377 2378### hide<sup>9+</sup> 2379 2380hide(): Promise<void> 2381 2382隐藏输入法。使用promise异步回调。 2383 2384**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2385 2386**返回值:** 2387 2388| 类型 | 说明 | 2389| ---------------- | ------------------------- | 2390| Promise<void> | 无返回结果的Promise对象。 | 2391 2392**错误码:** 2393 2394以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 2395 2396| 错误码ID | 错误信息 | 2397| -------- | -------------------------- | 2398| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 2399 2400**示例:** 2401 2402```ts 2403import { BusinessError } from '@kit.BasicServicesKit'; 2404 2405keyboardController.hide().then(() => { 2406 console.info('Succeeded in hiding keyboard.'); 2407}).catch((err: BusinessError) => { 2408 console.error(`Failed to hide. Code:${err.code}, message:${err.message}`); 2409}); 2410``` 2411 2412### hideKeyboard<sup>(deprecated)</sup> 2413 2414hideKeyboard(callback: AsyncCallback<void>): void 2415 2416隐藏输入法。使用callback异步回调。 2417 2418> **说明:** 2419> 2420> 从API version 8开始支持,API version 9开始废弃,建议使用[hide](#hide9)替代。 2421 2422**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2423 2424**参数:** 2425 2426| 参数名 | 类型 | 必填 | 说明 | 2427| -------- | ---------------------- | ---- | -------- | 2428| callback | AsyncCallback<void> | 是 | 回调函数。当输入法隐藏成功,err为undefined,否则为错误对象。 | 2429 2430**示例:** 2431 2432```ts 2433import { BusinessError } from '@kit.BasicServicesKit'; 2434 2435keyboardController.hideKeyboard((err: BusinessError) => { 2436 if (err) { 2437 console.error(`Failed to hideKeyboard. Code is ${err.code}, message is ${err.message}`); 2438 return; 2439 } 2440 console.info('Succeeded in hiding keyboard.'); 2441}); 2442``` 2443 2444### hideKeyboard<sup>(deprecated)</sup> 2445 2446hideKeyboard(): Promise<void> 2447 2448隐藏输入法。使用promise异步回调。 2449 2450> **说明:** 2451> 2452> 从API version 8开始支持,API version 9开始废弃,建议使用[hide](#hide9-1)替代。 2453 2454**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2455 2456**返回值:** 2457 2458| 类型 | 说明 | 2459| ---------------- | ------------------------- | 2460| Promise<void> | 无返回结果的Promise对象。 | 2461 2462**示例:** 2463 2464```ts 2465import { BusinessError } from '@kit.BasicServicesKit'; 2466 2467keyboardController.hideKeyboard().then(() => { 2468 console.info('Succeeded in hiding keyboard.'); 2469}).catch((err: BusinessError) => { 2470 console.info(`Failed to hideKeyboard. Code is ${err.code}, message is ${err.message}`); 2471}); 2472``` 2473 2474### exitCurrentInputType<sup>11+</sup> 2475 2476exitCurrentInputType(callback: AsyncCallback<void>): void 2477 2478退出当前输入类型,仅支持系统配置的默认输入法应用调用。使用callback异步回调。 2479 2480**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2481 2482**参数:** 2483 2484| 参数名 | 类型 | 必填 | 说明 | 2485| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 2486| callback | AsyncCallback<void> | 是 | 回调函数。当退出当前输入类型成功,err为undefined,否则为错误对象。 | 2487 2488**错误码:** 2489 2490以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 2491 2492| 错误码ID | 错误信息 | 2493| -------- | ---------------------------------------------- | 2494| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. | 2495| 12800010 | not the preconfigured default input method. | 2496 2497**示例:** 2498 2499```ts 2500import { BusinessError } from '@kit.BasicServicesKit'; 2501 2502keyboardController.exitCurrentInputType((err: BusinessError) => { 2503 if (err) { 2504 console.error(`Failed to exit current input type. Code:${err.code}, message:${err.message}`); 2505 return; 2506 } 2507 console.info('Succeeded in exiting current input type.'); 2508}); 2509``` 2510 2511### exitCurrentInputType<sup>11+</sup> 2512 2513exitCurrentInputType(): Promise<void> 2514 2515退出当前输入类型,仅支持系统配置的默认输入法应用调用。使用promise异步回调。 2516 2517**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2518 2519**返回值:** 2520 2521| 类型 | 说明 | 2522| ---------------- | ------------------------- | 2523| Promise<void> | 无返回结果的Promise对象。 | 2524 2525**错误码:** 2526 2527以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 2528 2529| 错误码ID | 错误信息 | 2530| -------- | ---------------------------------------------- | 2531| 12800008 | input method manager service error. Possible cause: a system error, such as null pointer, IPC exception. | 2532| 12800010 | not the preconfigured default input method. | 2533 2534**示例:** 2535 2536```ts 2537import { BusinessError } from '@kit.BasicServicesKit'; 2538 2539keyboardController.exitCurrentInputType().then(() => { 2540 console.info('Succeeded in exiting current input type.'); 2541}).catch((err: BusinessError) => { 2542 console.error(`Failed to exit current input type. Code:${err.code}, message:${err.message}`); 2543}); 2544``` 2545 2546## SecurityMode<sup>11+</sup> 2547 2548输入法的安全模式,如BASIC或FULL。 2549 2550**系统能力**: SystemCapability.MiscServices.InputMethodFramework 2551 2552| 名称 | 值 | 说明 | 2553| ----- | ---- | -------------------------------------------- | 2554| BASIC | 0 | 基础访问模式,基础打字模式,会限制网络访问。 | 2555| FULL | 1 | 完全访问模式,不做限制,可以访问网络。 | 2556 2557## ExtendAction<sup>10+</sup> 2558 2559编辑框中文本的扩展编辑操作类型,如剪切、复制等。 2560 2561**系统能力**: SystemCapability.MiscServices.InputMethodFramework 2562 2563| 名称 | 值 |说明 | 2564| -------- | -------- |-------- | 2565| SELECT_ALL | 0 |全选。 | 2566| CUT | 3 |剪切。 | 2567| COPY | 4 |复制。 | 2568| PASTE | 5 |粘贴。 | 2569 2570## Direction<sup>10+</sup> 2571 2572光标的移动方向。 2573 2574**系统能力**: SystemCapability.MiscServices.InputMethodFramework 2575 2576| 名称 | 值 |说明 | 2577| -------- | -------- |-------- | 2578| CURSOR_UP | 1 |向上。 | 2579| CURSOR_DOWN | 2 |向下。 | 2580| CURSOR_LEFT | 3 |向左。 | 2581| CURSOR_RIGHT | 4 |向右。 | 2582 2583## Range<sup>10+</sup> 2584 2585选中的文本范围。 2586 2587**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2588 2589| 名称 | 类型 | 只读 | 可选 | 说明 | 2590| -------- | -------- | -------- | -------- | -------- | 2591| start | number | 否 | 否 | 选中文本的首字符在编辑框的索引值。| 2592| end | number | 否 | 否 | 选中文本的末字符在编辑框的索引值。| 2593 2594## Movement<sup>10+</sup> 2595 2596选中文本时,光标移动的方向 2597 2598**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2599 2600| 名称 | 类型 | 只读 | 可选 | 说明 | 2601| -------- | -------- | -------- | -------- | -------- | 2602| direction | [Direction](#direction10) | 否 | 否 | 选中文本时,光标的移动方向。| 2603 2604## MessageHandler<sup>15+</sup> 2605 2606自定义通信对象。 2607 2608> **说明:** 2609> 2610> 开发者可通过注册此对象来接收已绑定当前输入法应用的编辑框应用所发送的自定义通信数据,接收到自定义通信数据时会触发此对象中[onMessage](#onmessage15)回调函数。 2611> 2612> 此对象全局唯一,多次注册仅保留最后一次注册的对象及有效性,并触发上一个已注册对象的[onTerminated](#onterminated15)回调函数。 2613> 2614> 若取消注册全局已注册的对象时,会触发被取消对象中[onTerminated](#onterminated15)回调函数。 2615 2616### onMessage<sup>15+</sup> 2617 2618onMessage(msgId: string, msgParam?: ArrayBuffer): void 2619 2620接收已绑定当前输入法应用的编辑框应用发送的自定义数据回调函数。 2621 2622> **说明:** 2623> 2624> 当已注册的[MessageHandler](#messagehandler15)接收到来自已绑定当前输入法应用的编辑框应用所发送的自定义通信数据时,会触发该回调函数。 2625> 2626> msgId为必选参数,msgParam为可选参数。存在收到仅有msgId自定义数据的可能,需与数据发送方确认自定义数据。 2627 2628**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2629 2630**参数:** 2631 2632| 参数名 | 类型 | 必填 | 说明 | 2633| -------- | ----------- | ---- | -------------------------------- | 2634| msgId | string | 是 | 接收到的自定义通信数据的标识符。 | 2635| msgParam | ArrayBuffer | 否 | 接收到的自定义通信数据的消息体。 | 2636 2637**示例:** 2638 2639```ts 2640inputMethodEngine.getInputMethodAbility() 2641 .on('inputStart', 2642 (kbController: inputMethodEngine.KeyboardController, client: inputMethodEngine.InputClient) => { 2643 let keyboardController: inputMethodEngine.KeyboardController = kbController; 2644 let inputClient: inputMethodEngine.InputClient = client; 2645 let messageHandler: inputMethodEngine.MessageHandler = { 2646 onTerminated(): void { 2647 console.info('OnTerminated.'); 2648 }, 2649 onMessage(msgId: string, msgParam?: ArrayBuffer): void { 2650 console.info('recv message.'); 2651 } 2652 } 2653 inputClient.recvMessage(messageHandler); 2654 }); 2655``` 2656 2657### onTerminated<sup>15+</sup> 2658 2659onTerminated(): void 2660 2661监听对象终止回调函数。 2662 2663> **说明:** 2664> 2665> 当应用注册新的[MessageHandler](#messagehandler15)对象时,会触发上一个已注册[MessageHandler](#messagehandler15)对象的[onTerminated](#onterminated15)回调函数。 2666> 2667> 当应用取消注册时,会触发当前已注册[MessageHandler](#messagehandler15)对象的[onTerminated](#onterminated15)回调函数。 2668 2669**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2670 2671**示例:** 2672 2673```ts 2674inputMethodEngine.getInputMethodAbility() 2675 .on('inputStart', 2676 (kbController: inputMethodEngine.KeyboardController, client: inputMethodEngine.InputClient) => { 2677 let keyboardController: inputMethodEngine.KeyboardController = kbController; 2678 let inputClient: inputMethodEngine.InputClient = client; 2679 let messageHandler: inputMethodEngine.MessageHandler = { 2680 onTerminated(): void { 2681 console.info('OnTerminated.'); 2682 }, 2683 onMessage(msgId: string, msgParam?: ArrayBuffer): void { 2684 console.info('recv message.'); 2685 } 2686 } 2687 inputClient.recvMessage(messageHandler); 2688 }); 2689``` 2690 2691## InputClient<sup>9+</sup> 2692 2693下列API均需使用[on('inputStart')](#oninputstart9)获取到InputClient实例后,通过实例调用。 2694 2695### sendKeyFunction<sup>9+</sup> 2696 2697sendKeyFunction(action:number, callback: AsyncCallback<boolean>): void 2698 2699发送功能键。使用callback异步回调。 2700 2701**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2702 2703 **参数:** 2704 2705| 参数名 | 类型 | 必填 | 说明 | 2706| -------- | -------- | -------- | -------- | 2707| action | number | 是 | 功能键键值。<br/>- 当值为0时,表示无效按键。<br/>- 当值为1时,表示确认键(即回车键)。 | 2708| callback | AsyncCallback<boolean> | 是 | 回调函数。当功能键发送成功,err为undefined,data为true;否则为错误对象。 | 2709 2710**错误码:** 2711 2712以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 2713 2714| 错误码ID | 错误信息 | 2715| -------- | -------------------------- | 2716| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2717| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 2718 2719 **示例:** 2720 2721```ts 2722import { BusinessError } from '@kit.BasicServicesKit'; 2723 2724let action: number = 1; 2725 2726inputClient.sendKeyFunction(action, (err: BusinessError, result: boolean) => { 2727 if (err) { 2728 console.error(`Failed to sendKeyFunction. Code is ${err.code}, message is ${err.message}`); 2729 return; 2730 } 2731 if (result) { 2732 console.info('Succeeded in sending key function.'); 2733 } else { 2734 console.error('Failed to sendKeyFunction.'); 2735 } 2736}); 2737``` 2738 2739### sendKeyFunction<sup>9+</sup> 2740 2741sendKeyFunction(action: number): Promise<boolean> 2742 2743发送功能键。使用promise异步回调。 2744 2745**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2746 2747**参数:** 2748 2749| 参数名 | 类型 | 必填 | 说明 | 2750| -------- | -------- | -------- | -------- | 2751| action | number | 是 | 功能键键值。<br/>当值为0时,表示无效按键;<br/>当值为1时,表示确认键(即回车键)。 | 2752 2753**返回值:** 2754 2755| 类型 | 说明 | 2756| ------------------------------- | ------------------------------------------------------------ | 2757| Promise<boolean> | Promise对象。返回true表示功能键发送成功;返回false表示功能键发送失败。| 2758 2759**错误码:** 2760 2761以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 2762 2763| 错误码ID | 错误信息 | 2764| -------- | -------------------------- | 2765| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2766| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 2767 2768**示例:** 2769 2770```ts 2771import { BusinessError } from '@kit.BasicServicesKit'; 2772 2773let action: number = 1; 2774inputClient.sendKeyFunction(action).then((result: boolean) => { 2775 if (result) { 2776 console.info('Succeeded in sending key function.'); 2777 } else { 2778 console.error('Failed to sendKeyFunction.'); 2779 } 2780}).catch((err: BusinessError) => { 2781 console.error(`Failed to sendKeyFunction. Code is ${err.code}, message is ${err.message}`); 2782}); 2783``` 2784 2785### getForward<sup>9+</sup> 2786 2787getForward(length:number, callback: AsyncCallback<string>): void 2788 2789获取光标前固定长度的文本。使用callback异步回调。 2790 2791**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2792 2793**参数:** 2794 2795| 参数名 | 类型 | 必填 | 说明 | 2796| -------- | -------- | -------- | -------- | 2797| length | number | 是 | 文本长度。不能小于0。 | 2798| callback | AsyncCallback<string> | 是 | 回调函数。当光标前固定长度的文本获取成功,err为undefined,data为获取到的文本;否则为错误对象。 | 2799 2800**错误码:** 2801 2802以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 2803 2804| 错误码ID | 错误信息 | 2805| -------- | ------------------------------ | 2806| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2807| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 2808| 12800006 | input method controller error. Possible cause: create InputMethodController object failed. | 2809 2810**示例:** 2811 2812```ts 2813import { BusinessError } from '@kit.BasicServicesKit'; 2814 2815let length: number = 1; 2816inputClient.getForward(length, (err: BusinessError, text: string) => { 2817 if (err) { 2818 console.error(`Failed to getForward. Code is ${err.code}, message is ${err.message}`); 2819 return; 2820 } 2821 console.info('Succeeded in getting forward, text: ' + text); 2822}); 2823``` 2824 2825### getForward<sup>9+</sup> 2826 2827getForward(length:number): Promise<string> 2828 2829获取光标前固定长度的文本。使用promise异步回调。 2830 2831**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2832 2833**参数:** 2834 2835| 参数名 | 类型 | 必填 | 说明 | 2836| -------- | -------- | -------- | -------- | 2837| length | number | 是 | 文本长度。不能小于0 | 2838 2839**返回值:** 2840 2841| 类型 | 说明 | 2842| ------------------------------- | ------------------------------------------------------------ | 2843| Promise<string> | Promise对象,返回光标前固定长度的文本。 | 2844 2845**错误码:** 2846 2847以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 2848 2849| 错误码ID | 错误信息 | 2850| -------- | ------------------------------ | 2851| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2852| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 2853| 12800006 | input method controller error. Possible cause: create InputMethodController object failed. | 2854 2855**示例:** 2856 2857```ts 2858import { BusinessError } from '@kit.BasicServicesKit'; 2859 2860let length: number = 1; 2861inputClient.getForward(length).then((text: string) => { 2862 console.info('Succeeded in getting forward, text: ' + text); 2863}).catch((err: BusinessError) => { 2864 console.error(`Failed to getForward. Code is ${err.code}, message is ${err.message}`); 2865}); 2866``` 2867 2868### getForwardSync<sup>10+</sup> 2869 2870getForwardSync(length:number): string 2871 2872获取光标前固定长度的文本。 2873 2874**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2875 2876**参数:** 2877 2878| 参数名 | 类型 | 必填 | 说明 | 2879| ------ | ------ | ---- | ---------- | 2880| length | number | 是 | 文本长度。不能小于0。 | 2881 2882**返回值:** 2883 2884| 类型 | 说明 | 2885| ------ | -------------------------- | 2886| string | 返回光标前固定长度的文本。 | 2887 2888**错误码:** 2889 2890以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 2891 2892| 错误码ID | 错误信息 | 2893| -------- | ------------------------------ | 2894| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2895| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 2896| 12800006 | input method controller error. Possible cause: create InputMethodController object failed. | 2897 2898**示例:** 2899 2900```ts 2901let length: number = 1; 2902let text: string = inputClient.getForwardSync(length); 2903console.info(`Succeeded in getting forward, text: ${text}`); 2904``` 2905 2906### getBackward<sup>9+</sup> 2907 2908getBackward(length:number, callback: AsyncCallback<string>): void 2909 2910获取光标后固定长度的文本。使用callback异步回调。 2911 2912**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2913 2914**参数:** 2915 2916| 参数名 | 类型 | 必填 | 说明 | 2917| -------- | -------- | -------- | -------- | 2918| length | number | 是 | 文本长度。不能小于0。 | 2919| callback | AsyncCallback<string> | 是 | 回调函数。当光标后固定长度的文本获取成功,err为undefined,data为获取到的文本;否则为错误对象。| 2920 2921**错误码:** 2922 2923以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 2924 2925| 错误码ID | 错误信息 | 2926| -------- | ------------------------------ | 2927| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2928| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 2929| 12800006 | input method controller error. Possible cause: create InputMethodController object failed. | 2930 2931**示例:** 2932 2933```ts 2934import { BusinessError } from '@kit.BasicServicesKit'; 2935 2936let length: number = 1; 2937inputClient.getBackward(length, (err: BusinessError, text: string) => { 2938 if (err) { 2939 console.error(`Failed to getBackward. Code is ${err.code}, message is ${err.message}`); 2940 return; 2941 } 2942 console.info('Succeeded in getting backward, text: ' + text); 2943}); 2944``` 2945 2946### getBackward<sup>9+</sup> 2947 2948getBackward(length:number): Promise<string> 2949 2950获取光标后固定长度的文本。使用promise异步回调。 2951 2952**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2953 2954**参数:** 2955 2956| 参数名 | 类型 | 必填 | 说明 | 2957| -------- | -------- | -------- | -------- | 2958| length | number | 是 | 文本长度。不能小于0。 | 2959 2960**返回值:** 2961 2962| 类型 | 说明 | 2963| ------------------------------- | ------------------------------------------------------------ | 2964| Promise<string> | Promise对象,返回光标后固定长度的文本。 | 2965 2966**错误码:** 2967 2968以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 2969 2970| 错误码ID | 错误信息 | 2971| -------- | ------------------------------ | 2972| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2973| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 2974| 12800006 | input method controller error. Possible cause: create InputMethodController object failed. | 2975 2976**示例:** 2977 2978```ts 2979import { BusinessError } from '@kit.BasicServicesKit'; 2980 2981let length: number = 1; 2982inputClient.getBackward(length).then((text: string) => { 2983 console.info('Succeeded in getting backward, text: ' + text); 2984}).catch((err: BusinessError) => { 2985 console.error(`Failed to getBackward. Code is ${err.code}, message is ${err.message}`); 2986}); 2987``` 2988 2989### getBackwardSync<sup>10+</sup> 2990 2991getBackwardSync(length:number): string 2992 2993获取光标后固定长度的文本。 2994 2995**系统能力:** SystemCapability.MiscServices.InputMethodFramework 2996 2997**参数:** 2998 2999| 参数名 | 类型 | 必填 | 说明 | 3000| ------ | ------ | ---- | ---------- | 3001| length | number | 是 | 文本长度。不能小于0。 | 3002 3003**返回值:** 3004 3005| 类型 | 说明 | 3006| ------ | -------------------------- | 3007| string | 返回光标后固定长度的文本。 | 3008 3009**错误码:** 3010 3011以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3012 3013| 错误码ID | 错误信息 | 3014| -------- | ------------------------------ | 3015| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3016| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3017| 12800006 | input method controller error. Possible cause: create InputMethodController object failed. | 3018 3019**示例:** 3020 3021```ts 3022let length: number = 1; 3023let text: string = inputClient.getBackwardSync(length); 3024console.info(`Succeeded in getting backward, text: ${text}`); 3025``` 3026 3027### deleteForward<sup>9+</sup> 3028 3029deleteForward(length:number, callback: AsyncCallback<boolean>): void 3030 3031删除光标前固定长度的文本。使用callback异步回调。 3032 3033**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3034 3035**参数:** 3036 3037| 参数名 | 类型 | 必填 | 说明 | 3038| -------- | -------- | -------- | -------- | 3039| length | number | 是 | 文本长度。不能小于0。 | 3040| callback | AsyncCallback<boolean> | 是 | 回调函数。当光标前固定长度的文本删除成功,err为undefined,data为true;否则为错误对象。 | 3041 3042**错误码:** 3043 3044以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3045 3046| 错误码ID | 错误信息 | 3047| -------- | -------------------------- | 3048| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3049| 12800002 | input method engine error. Possible causes: 1.input method panel not created. 2.the input method application does not subscribe to related events. | 3050| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3051 3052**示例:** 3053 3054```ts 3055import { BusinessError } from '@kit.BasicServicesKit'; 3056 3057let length: number = 1; 3058inputClient.deleteForward(length, (err: BusinessError, result: boolean) => { 3059 if (err) { 3060 console.error(`Failed to deleteForward. Code is ${err.code}, message is ${err.message}`); 3061 return; 3062 } 3063 if (result) { 3064 console.info('Succeeded in deleting forward.'); 3065 } else { 3066 console.error(`Failed to deleteForward.`); 3067 } 3068}); 3069``` 3070 3071### deleteForward<sup>9+</sup> 3072 3073deleteForward(length:number): Promise<boolean> 3074 3075删除光标前固定长度的文本。使用promise异步回调。 3076 3077**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3078 3079**参数:** 3080 3081| 参数名 | 类型 | 必填 | 说明 | 3082| ------ | ------ | ---- | ---------- | 3083| length | number | 是 | 文本长度。不能小于0。 | 3084 3085**返回值:** 3086 3087| 类型 | 说明 | 3088| ---------------------- | -------------- | 3089| Promise<boolean> | Promise对象。返回true表示删除光标前固定长度的文本成功;返回false表示删除光标前固定长度的文本失败。| 3090 3091**错误码:** 3092 3093以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3094 3095| 错误码ID | 错误信息 | 3096| -------- | -------------------------- | 3097| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3098| 12800002 | input method engine error. Possible causes: 1.input method panel not created. 2.the input method application does not subscribe to related events. | 3099| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3100 3101**示例:** 3102 3103```ts 3104import { BusinessError } from '@kit.BasicServicesKit'; 3105 3106let length: number = 1; 3107inputClient.deleteForward(length).then((result: boolean) => { 3108 if (result) { 3109 console.info('Succeeded in deleting forward.'); 3110 } else { 3111 console.error('Failed to delete Forward.'); 3112 } 3113}).catch((err: BusinessError) => { 3114 console.error(`Failed to deleteForward. Code is ${err.code}, message is ${err.message}`); 3115}); 3116``` 3117 3118### deleteForwardSync<sup>10+</sup> 3119 3120deleteForwardSync(length:number): void 3121 3122删除光标前固定长度的文本。 3123 3124**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3125 3126**参数:** 3127 3128| 参数名 | 类型 | 必填 | 说明 | 3129| ------ | ------ | ---- | ---------- | 3130| length | number | 是 | 文本长度。不能小于0。 | 3131 3132**错误码:** 3133 3134以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3135 3136| 错误码ID | 错误信息 | 3137| -------- | -------------------------- | 3138| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3139| 12800002 | input method engine error. Possible causes: 1.input method panel not created. 2.the input method application does not subscribe to related events. | 3140| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3141 3142**示例:** 3143 3144```ts 3145let length: number = 1; 3146inputClient.deleteForwardSync(length); 3147``` 3148 3149### deleteBackward<sup>9+</sup> 3150 3151deleteBackward(length:number, callback: AsyncCallback<boolean>): void 3152 3153删除光标后固定长度的文本。使用callback异步回调。 3154 3155**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3156 3157**参数:** 3158 3159| 参数名 | 类型 | 必填 | 说明 | 3160| -------- | ---------------------------- | ---- | -------------- | 3161| length | number | 是 | 文本长度。不能小于0。 | 3162| callback | AsyncCallback<boolean> | 是 | 回调函数。当光标后固定长度的文本删除成功,err为undefined,data为true;否则为错误对象。 | 3163 3164**错误码:** 3165 3166以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3167 3168| 错误码ID | 错误信息 | 3169| -------- | -------------------------- | 3170| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3171| 12800002 | input method engine error. Possible causes: 1.input method panel not created. 2.the input method application does not subscribe to related events. | 3172| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3173 3174**示例:** 3175 3176```ts 3177import { BusinessError } from '@kit.BasicServicesKit'; 3178 3179let length: number = 1; 3180inputClient.deleteBackward(length, (err: BusinessError, result: boolean) => { 3181 if (err) { 3182 console.error(`Failed to deleteBackward. Code is ${err.code}, message is ${err.message}`); 3183 return; 3184 } 3185 if (result) { 3186 console.info('Succeeded in deleting backward.'); 3187 } else { 3188 console.error(`Failed to deleteBackward.`); 3189 } 3190}); 3191``` 3192 3193### deleteBackward<sup>9+</sup> 3194 3195deleteBackward(length:number): Promise<boolean> 3196 3197删除光标后固定长度的文本。使用promise异步回调。 3198 3199**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3200 3201**参数:** 3202 3203| 参数名 | 类型 | 必填 | 说明 | 3204| -------- | -------- | -------- | -------- | 3205| length | number | 是 | 文本长度。不能小于0。 | 3206 3207**返回值:** 3208 3209| 类型 | 说明 | 3210| ------------------------------- | ------------------------------------------------------------ | 3211| Promise<boolean> | Promise对象。返回true表示删除光标后固定长度的文本成功;返回false表示删除光标后固定长度的文本失败。 | 3212 3213**错误码:** 3214 3215以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3216 3217| 错误码ID | 错误信息 | 3218| -------- | -------------------------- | 3219| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3220| 12800002 | input method engine error. Possible causes: 1.input method panel not created. 2.the input method application does not subscribe to related events. | 3221| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3222 3223**示例:** 3224 3225```ts 3226import { BusinessError } from '@kit.BasicServicesKit'; 3227 3228let length: number = 1; 3229inputClient.deleteBackward(length).then((result: boolean) => { 3230 if (result) { 3231 console.info('Succeeded in deleting backward.'); 3232 } else { 3233 console.error('Failed to deleteBackward.'); 3234 } 3235}).catch((err: BusinessError) => { 3236 console.error(`Failed to deleteBackward. Code is ${err.code}, message is ${err.message}`); 3237}); 3238``` 3239 3240### deleteBackwardSync<sup>10+</sup> 3241 3242deleteBackwardSync(length:number): void 3243 3244删除光标后固定长度的文本。 3245 3246**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3247 3248**参数:** 3249 3250| 参数名 | 类型 | 必填 | 说明 | 3251| ------ | ------ | ---- | ---------- | 3252| length | number | 是 | 文本长度。不能小于0。 | 3253 3254**错误码:** 3255 3256以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3257 3258| 错误码ID | 错误信息 | 3259| -------- | -------------------------- | 3260| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3261| 12800002 | input method engine error. Possible causes: 1.input method panel not created. 2.the input method application does not subscribe to related events. | 3262| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3263 3264**示例:** 3265 3266```ts 3267let length: number = 1; 3268inputClient.deleteBackwardSync(length); 3269``` 3270 3271### insertText<sup>9+</sup> 3272 3273insertText(text:string, callback: AsyncCallback<boolean>): void 3274 3275插入文本。使用callback异步回调。 3276 3277**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3278 3279**参数:** 3280 3281| 参数名 | 类型 | 必填 | 说明 | 3282| -------- | -------- | -------- | -------- | 3283| text | string | 是 | 文本内容。 | 3284| callback | AsyncCallback<boolean> | 是 | 回调函数。当文本插入成功,err为undefined,data为true;否则为错误对象。 | 3285 3286**错误码:** 3287 3288以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3289 3290| 错误码ID | 错误信息 | 3291| -------- | -------------------------- | 3292| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3293| 12800002 | input method engine error. Possible causes: 1.input method panel not created. 2.the input method application does not subscribe to related events. | 3294| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3295 3296**示例:** 3297 3298```ts 3299import { BusinessError } from '@kit.BasicServicesKit'; 3300 3301 3302inputClient.insertText('test', (err: BusinessError, result: boolean) => { 3303 if (err) { 3304 console.error(`Failed to insertText. Code is ${err.code}, message is ${err.message}`); 3305 return; 3306 } 3307 if (result) { 3308 console.info('Succeeded in inserting text.'); 3309 } else { 3310 console.error('Failed to insertText.'); 3311 } 3312}); 3313``` 3314 3315### insertText<sup>9+</sup> 3316 3317insertText(text:string): Promise<boolean> 3318 3319插入文本。使用promise异步回调。 3320 3321**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3322 3323**参数:** 3324 3325| 参数名 | 类型 | 必填 | 说明 | 3326| -------- | -------- | -------- | -------- | 3327| text | string | 是 | 文本。 | 3328 3329**返回值:** 3330 3331| 类型 | 说明 | 3332| ------------------------------- | ------------------------------------------------------------ | 3333| Promise<boolean> | Promise对象。返回true表示插入文本成功;返回false表示插入文本失败。 | 3334 3335**错误码:** 3336 3337以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3338 3339| 错误码ID | 错误信息 | 3340| -------- | -------------------------- | 3341| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3342| 12800002 | input method engine error. Possible causes: 1.input method panel not created. 2.the input method application does not subscribe to related events. | 3343| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3344 3345**示例:** 3346 3347```ts 3348import { BusinessError } from '@kit.BasicServicesKit'; 3349 3350inputClient.insertText('test').then((result: boolean) => { 3351 if (result) { 3352 console.info('Succeeded in inserting text.'); 3353 } else { 3354 console.error('Failed to insertText.'); 3355 } 3356}).catch((err: BusinessError) => { 3357 console.error(`Failed to insertText. Code is ${err.code}, message is ${err.message}`); 3358}); 3359``` 3360 3361### insertTextSync<sup>10+</sup> 3362 3363insertTextSync(text: string): void 3364 3365插入文本。 3366 3367**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3368 3369**参数:** 3370 3371| 参数名 | 类型 | 必填 | 说明 | 3372| ------ | ------ | ---- | ---------- | 3373| text | string | 是 | 文本内容。 | 3374 3375**错误码:** 3376 3377以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3378 3379| 错误码ID | 错误信息 | 3380| -------- | -------------------------- | 3381| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3382| 12800002 | input method engine error. Possible causes: 1.input method panel not created. 2.the input method application does not subscribe to related events. | 3383| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3384 3385**示例:** 3386 3387```ts 3388inputClient.insertTextSync('test'); 3389``` 3390 3391### getEditorAttribute<sup>9+</sup> 3392 3393getEditorAttribute(callback: AsyncCallback<EditorAttribute>): void 3394 3395获取编辑框属性值。使用callback异步回调。 3396 3397**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3398 3399**参数:** 3400 3401| 参数名 | 类型 | 必填 | 说明 | 3402| ------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | 3403| callback | AsyncCallback<[EditorAttribute](#editorattribute)> | 是 | 回调函数。当编辑框属性值获取成功,err为undefined,data为编辑框属性值;否则为错误对象。| 3404 3405**错误码:** 3406 3407以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 3408 3409| 错误码ID | 错误信息 | 3410| -------- | -------------------------- | 3411| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3412 3413**示例:** 3414 3415```ts 3416import { BusinessError } from '@kit.BasicServicesKit'; 3417 3418inputClient.getEditorAttribute((err: BusinessError, editorAttribute: inputMethodEngine.EditorAttribute) => { 3419 if (err) { 3420 console.error(`Failed to getEditorAttribute. Code is ${err.code}, message is ${err.message}`); 3421 return; 3422 } 3423 console.info(`editorAttribute.inputPattern: ${editorAttribute.inputPattern}`); 3424 console.info(`editorAttribute.enterKeyType: ${editorAttribute.enterKeyType}`); 3425}); 3426``` 3427 3428### getEditorAttribute<sup>9+</sup> 3429 3430getEditorAttribute(): Promise<EditorAttribute> 3431 3432获取编辑框属性值。使用promise异步回调。 3433 3434**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3435 3436**返回值:** 3437 3438| 类型 | 说明 | 3439| ------------------------------- | ------------------------------------------------------------ | 3440| Promise<[EditorAttribute](#editorattribute)> | Promise对象,返回编辑框属性值。 | 3441 3442**错误码:** 3443 3444以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 3445 3446| 错误码ID | 错误信息 | 3447| -------- | -------------------------- | 3448| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3449 3450**示例:** 3451 3452```ts 3453import { BusinessError } from '@kit.BasicServicesKit'; 3454 3455inputClient.getEditorAttribute().then((editorAttribute: inputMethodEngine.EditorAttribute) => { 3456 console.info(`editorAttribute.inputPattern: ${editorAttribute.inputPattern}`); 3457 console.info(`editorAttribute.enterKeyType: ${editorAttribute.enterKeyType}`); 3458}).catch((err: BusinessError) => { 3459 console.error(`Failed to getEditorAttribute. Code is ${err.code}, message is ${err.message}`); 3460}); 3461``` 3462 3463### getEditorAttributeSync<sup>10+</sup> 3464 3465getEditorAttributeSync(): EditorAttribute 3466 3467获取编辑框属性值。 3468 3469**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3470 3471**返回值:** 3472 3473| 类型 | 说明 | 3474| ----------------------------------- | -------------- | 3475| [EditorAttribute](#editorattribute) | 编辑框属性对象。 | 3476 3477**错误码:** 3478 3479以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 3480 3481| 错误码ID | 错误信息 | 3482| -------- | -------------------------- | 3483| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3484 3485**示例:** 3486 3487```ts 3488let editorAttribute: inputMethodEngine.EditorAttribute = inputClient.getEditorAttributeSync(); 3489console.info(`editorAttribute.inputPattern: ${editorAttribute.inputPattern}`); 3490console.info(`editorAttribute.enterKeyType: ${editorAttribute.enterKeyType}`); 3491``` 3492 3493### moveCursor<sup>9+</sup> 3494 3495moveCursor(direction: number, callback: AsyncCallback<void>): void 3496 3497移动光标。使用callback异步回调。 3498 3499**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3500 3501**参数:** 3502 3503| 参数名 | 类型 | 必填 | 说明 | 3504| --------- | ------------------------- | ---- | -------------- | 3505| direction | number | 是 | 光标移动方向。<br/>- 当值为1时,表示向上。<br/>- 当值为2时,表示向下。<br/>- 当值为3时,表示向左。<br/>- 当值为4时,表示向右。不能小于0。 | 3506| callback | AsyncCallback<void> | 是 | 回调函数。当光标移动成功,err为undefined,否则为错误对象。 | 3507 3508**错误码:** 3509 3510以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3511 3512| 错误码ID | 错误信息 | 3513| -------- | -------------------------- | 3514| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3515| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3516 3517**示例:** 3518 3519```ts 3520import { BusinessError } from '@kit.BasicServicesKit'; 3521 3522inputClient.moveCursor(inputMethodEngine.Direction.CURSOR_UP, (err: BusinessError) => { 3523 if (err) { 3524 console.error(`Failed to moveCursor. Code is ${err.code}, message is ${err.message}`); 3525 return; 3526 } 3527 console.info('Succeeded in moving cursor.'); 3528}); 3529``` 3530 3531### moveCursor<sup>9+</sup> 3532 3533moveCursor(direction: number): Promise<void> 3534 3535移动光标。使用promise异步回调。 3536 3537**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3538 3539**参数:** 3540 3541| 参数名 | 类型 | 必填 | 说明 | 3542| --------- | ------ | ---- | ------------------------------------------------------------ | 3543| direction | number | 是 | 光标移动方向。<br/>- 当值为1时,表示向上。<br/>- 当值为2时,表示向下。<br/>- 当值为3时,表示向左。<br/>- 当值为4时,表示向右。不能小于0。 | 3544 3545**返回值:** 3546 3547| 类型 | 说明 | 3548| ------------------- | ------------------------- | 3549| Promise<void> | 无返回结果的Promise对象。 | 3550 3551**错误码:** 3552 3553以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3554 3555| 错误码ID | 错误信息 | 3556| -------- | -------------------------- | 3557| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3558| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3559 3560**示例:** 3561 3562```ts 3563import { BusinessError } from '@kit.BasicServicesKit'; 3564 3565inputClient.moveCursor(inputMethodEngine.Direction.CURSOR_UP).then(() => { 3566 console.info('Succeeded in moving cursor.'); 3567}).catch((err: BusinessError) => { 3568 console.error(`Failed to moveCursor. Code is ${err.code}, message is ${err.message}`); 3569}); 3570``` 3571 3572### moveCursorSync<sup>10+</sup> 3573 3574moveCursorSync(direction: number): void 3575 3576移动光标。 3577 3578**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3579 3580**参数:** 3581 3582| 参数名 | 类型 | 必填 | 说明 | 3583| --------- | ------ | ---- | ------------------------------------------------------------ | 3584| direction | number | 是 | 光标移动方向。<br/>- 当值为1时,表示向上。<br/>- 当值为2时,表示向下。<br/>- 当值为3时,表示向左。<br/>- 当值为4时,表示向右。不能小于0。 | 3585 3586**错误码:** 3587 3588以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3589 3590| 错误码ID | 错误信息 | 3591| -------- | -------------------------- | 3592| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3593| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3594 3595**示例:** 3596 3597```ts 3598inputClient.moveCursorSync(inputMethodEngine.Direction.CURSOR_UP); 3599``` 3600 3601### selectByRange<sup>10+</sup> 3602 3603selectByRange(range: Range, callback: AsyncCallback<void>): void 3604 3605根据索引范围选中文本。使用callback异步回调。 3606 3607**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3608 3609**参数:** 3610 3611| 参数名 | 类型 | 必填 | 说明 | 3612| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ | 3613| range | [Range](#range10) | 是 | 选中文本的范围。 | 3614| callback | AsyncCallback<void> | 是 | 回调函数。当成功发送选中事件后,err为undefined,否则为错误对象。 | 3615 3616**错误码:** 3617 3618以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3619 3620| 错误码ID | 错误信息 | 3621| -------- | -------------------------- | 3622| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3623| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3624 3625**示例:** 3626 3627```ts 3628import { BusinessError } from '@kit.BasicServicesKit'; 3629 3630let range: inputMethodEngine.Range = { start: 0, end: 1 }; 3631inputClient.selectByRange(range, (err: BusinessError) => { 3632 if (err) { 3633 console.error(`Failed to selectByRange. Code is ${err.code}, message is ${err.message}`); 3634 return; 3635 } 3636 console.info('Succeeded in selecting by range.'); 3637}); 3638``` 3639 3640### selectByRange<sup>10+</sup> 3641 3642selectByRange(range: Range): Promise<void> 3643 3644根据索引范围选中文本。使用promise异步回调。 3645 3646**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3647 3648**参数:** 3649 3650| 参数名 | 类型 | 必填 | 说明 | 3651| ------ | --------------------------------------------------------- | ---- | ---------------- | 3652| range | [Range](#range10) | 是 | 选中文本的范围。 | 3653 3654**返回值:** 3655 3656| 类型 | 说明 | 3657| ------------------- | ------------------------- | 3658| Promise<void> | 无返回结果的Promise对象。 | 3659 3660**错误码:** 3661 3662以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3663 3664| 错误码ID | 错误信息 | 3665| -------- | -------------------------- | 3666| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3667| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3668 3669**示例:** 3670 3671```ts 3672import { BusinessError } from '@kit.BasicServicesKit'; 3673 3674let range: inputMethodEngine.Range = { start: 0, end: 1 }; 3675inputClient.selectByRange(range).then(() => { 3676 console.info('Succeeded in selecting by range.'); 3677}).catch((err: BusinessError) => { 3678 console.error(`Failed to selectByRange. Code is ${err.code}, message is ${err.message}`); 3679}); 3680``` 3681 3682### selectByRangeSync<sup>10+</sup> 3683 3684selectByRangeSync(range: Range): void 3685 3686根据索引范围选中文本。 3687 3688**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3689 3690**参数:** 3691 3692| 参数名 | 类型 | 必填 | 说明 | 3693| ------ | ----------------- | ---- | ---------------- | 3694| range | [Range](#range10) | 是 | 选中文本的范围。 | 3695 3696**错误码:** 3697 3698以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3699 3700| 错误码ID | 错误信息 | 3701| -------- | -------------------------- | 3702| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3703| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3704 3705**示例:** 3706 3707```ts 3708let range: inputMethodEngine.Range = { start: 0, end: 1 }; 3709inputClient.selectByRangeSync(range); 3710``` 3711 3712### selectByMovement<sup>10+</sup> 3713 3714selectByMovement(movement: Movement, callback: AsyncCallback<void>): void 3715 3716根据光标移动方向选中文本。使用callback异步回调。 3717 3718**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3719 3720**参数:** 3721 3722| 参数名 | 类型 | 必填 | 说明 | 3723| -------- | ------ | ---- | ------ | 3724| movement | [Movement](#movement10) | 是 | 选中时光标移动的方向。 | 3725| callback | AsyncCallback<void> | 是 | 回调函数。当成功发送选中事件后,err为undefined,否则为错误对象。 | 3726 3727**错误码:** 3728 3729以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3730 3731| 错误码ID | 错误信息 | 3732| -------- | -------------------------- | 3733| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3734| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3735 3736**示例:** 3737 3738```ts 3739import { BusinessError } from '@kit.BasicServicesKit'; 3740 3741let movement: inputMethodEngine.Movement = { direction: 1 }; 3742inputClient.selectByMovement(movement, (err: BusinessError) => { 3743 if (err) { 3744 console.error(`Failed to selectByMovement. Code is ${err.code}, message is ${err.message}`); 3745 return; 3746 } 3747 console.info('Succeeded in selecting by movement.'); 3748}); 3749``` 3750 3751### selectByMovement<sup>10+</sup> 3752 3753selectByMovement(movement: Movement): Promise<void> 3754 3755根据光标移动方向选中文本。使用promise异步回调。 3756 3757**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3758 3759**参数:** 3760 3761| 参数名 | 类型 | 必填 | 说明 | 3762| -------- | ------------------------------------------------------------ | ---- | ---------------------- | 3763| movement | [Movement](#movement10) | 是 | 选中时光标移动的方向。 | 3764 3765**返回值:** 3766 3767| 类型 | 说明 | 3768| ------------------- | ------------------------- | 3769| Promise<void> | 无返回结果的Promise对象。 | 3770 3771**错误码:** 3772 3773以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3774 3775| 错误码ID | 错误信息 | 3776| -------- | -------------------------- | 3777| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3778| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3779 3780**示例:** 3781 3782```ts 3783import { BusinessError } from '@kit.BasicServicesKit'; 3784 3785let movement: inputMethodEngine.Movement = { direction: 1 }; 3786inputClient.selectByMovement(movement).then(() => { 3787 console.info('Succeeded in selecting by movement.'); 3788}).catch((err: BusinessError) => { 3789 console.error(`Failed to selectByMovement. Code is ${err.code}, message is ${err.message}`); 3790}); 3791``` 3792 3793### selectByMovementSync<sup>10+</sup> 3794 3795selectByMovementSync(movement: Movement): void 3796 3797根据光标移动方向选中文本。 3798 3799**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3800 3801**参数:** 3802 3803| 参数名 | 类型 | 必填 | 说明 | 3804| -------- | ----------------------- | ---- | ---------------------- | 3805| movement | [Movement](#movement10) | 是 | 选中时光标移动的方向。 | 3806 3807**错误码:** 3808 3809以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3810 3811| 错误码ID | 错误信息 | 3812| -------- | -------------------------- | 3813| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3814| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3815 3816**示例:** 3817 3818```ts 3819let movement: inputMethodEngine.Movement = { direction: 1 }; 3820inputClient.selectByMovementSync(movement); 3821``` 3822 3823### getTextIndexAtCursor<sup>10+</sup> 3824 3825getTextIndexAtCursor(callback: AsyncCallback<number>): void 3826 3827获取光标所在处的文本索引。使用callback异步回调。 3828 3829**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3830 3831**参数:** 3832 3833| 参数名 | 类型 | 必填 | 说明 | 3834| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 3835| callback | AsyncCallback<number> | 是 | 回调函数。当文本索引获取成功,err为undefined,否则为错误对象。 | 3836 3837**错误码:** 3838 3839以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 3840 3841| 错误码ID | 错误信息 | 3842| -------- | ------------------------------ | 3843| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3844| 12800006 | input method controller error. Possible cause: create InputMethodController object failed. | 3845 3846**示例:** 3847 3848```ts 3849import { BusinessError } from '@kit.BasicServicesKit'; 3850 3851inputClient.getTextIndexAtCursor((err: BusinessError, index: number) => { 3852 if (err) { 3853 console.error(`Failed to getTextIndexAtCursor. Code is ${err.code}, message is ${err.message}`); 3854 return; 3855 } 3856 console.info('Succeeded in getTextIndexAtCursor: ' + index); 3857}); 3858``` 3859 3860### getTextIndexAtCursor<sup>10+</sup> 3861 3862getTextIndexAtCursor(): Promise<number> 3863 3864获取光标所在处的文本索引。使用promise异步回调。 3865 3866**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3867 3868**返回值:** 3869 3870| 类型 | 说明 | 3871| --------------------- | --------------------------------------- | 3872| Promise<number> | Promise对象,返回光标所在处的文本索引。 | 3873 3874**错误码:** 3875 3876以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 3877 3878| 错误码ID | 错误信息 | 3879| -------- | ------------------------------ | 3880| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3881| 12800006 | input method controller error. Possible cause: create InputMethodController object failed. | 3882 3883**示例:** 3884 3885```ts 3886import { BusinessError } from '@kit.BasicServicesKit'; 3887 3888inputClient.getTextIndexAtCursor().then((index: number) => { 3889 console.info('Succeeded in getTextIndexAtCursor: ' + index); 3890}).catch((err: BusinessError) => { 3891 console.error(`Failed to getTextIndexAtCursor. Code is ${err.code}, message is ${err.message}`); 3892}); 3893``` 3894 3895### getTextIndexAtCursorSync<sup>10+</sup> 3896 3897getTextIndexAtCursorSync(): number 3898 3899获取光标所在处的文本索引。 3900 3901**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3902 3903**返回值:** 3904 3905| 类型 | 说明 | 3906| ------ | -------------------------- | 3907| number | 返回光标所在处的文本索引。 | 3908 3909**错误码:** 3910 3911以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 3912 3913| 错误码ID | 错误信息 | 3914| -------- | ------------------------------ | 3915| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3916| 12800006 | input method controller error. Possible cause: create InputMethodController object failed. | 3917 3918**示例:** 3919 3920```ts 3921let index: number = inputClient.getTextIndexAtCursorSync(); 3922console.info(`Succeeded in getTextIndexAtCursorSync, index: ${index}`); 3923``` 3924 3925### sendExtendAction<sup>10+</sup> 3926 3927sendExtendAction(action: ExtendAction, callback: AsyncCallback<void>): void 3928 3929发送扩展编辑操作。使用callback异步回调。 3930 3931> **说明** 3932> 3933> 输入法应用调用该接口向编辑框发送扩展编辑操作,编辑框监听相应事件[on('handleExtendAction')](./js-apis-inputmethod.md#onhandleextendaction10),从而进一步做出处理。 3934 3935**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3936 3937**参数:** 3938 3939| 参数名 | 类型 | 必填 | 说明 | 3940| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 3941| action | [ExtendAction](#extendaction10) | 是 | 要发送的扩展操作。 | 3942| callback | AsyncCallback<void> | 是 | 回调函数。发送成功,err为undefined,否则为错误对象。 | 3943 3944**错误码:** 3945 3946以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3947 3948| 错误码ID | 错误信息 | 3949| -------- | ------------------------------ | 3950| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3951| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 3952| 12800006 | input method controller error. Possible cause: create InputMethodController object failed. | 3953 3954**示例:** 3955 3956```ts 3957import { BusinessError } from '@kit.BasicServicesKit'; 3958 3959inputClient.sendExtendAction(inputMethodEngine.ExtendAction.COPY, (err: BusinessError) => { 3960 if (err) { 3961 console.error(`Failed to sendExtendAction. Code is ${err.code}, message is ${err.message}`); 3962 return; 3963 } 3964 console.info('Succeeded in sending extend action.'); 3965}); 3966``` 3967 3968### sendExtendAction<sup>10+</sup> 3969 3970sendExtendAction(action: ExtendAction): Promise<void> 3971 3972发送扩展编辑操作。使用promise异步回调。 3973 3974>**说明** 3975> 3976> 输入法应用调用该接口向编辑框发送扩展编辑操作,编辑框监听相应事件[on('handleExtendAction')](./js-apis-inputmethod.md#onhandleextendaction10),从而进一步做出处理。 3977 3978**系统能力:** SystemCapability.MiscServices.InputMethodFramework 3979 3980**参数:** 3981 3982| 参数名 | 类型 | 必填 | 说明 | 3983| -------- | -------- | -------- | -------- | 3984| action | [ExtendAction](#extendaction10) | 是 | 要发送的扩展操作。 | 3985 3986**返回值:** 3987 3988| 类型 | 说明 | 3989| --------------------- | --------------------------------------- | 3990| Promise<void> | 无返回结果的Promise对象。 | 3991 3992**错误码:** 3993 3994以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 3995 3996| 错误码ID | 错误信息 | 3997| -------- | ------------------------------ | 3998| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3999| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 4000| 12800006 | input method controller error. Possible cause: create InputMethodController object failed. | 4001 4002**示例:** 4003 4004```ts 4005import { BusinessError } from '@kit.BasicServicesKit'; 4006 4007inputClient.sendExtendAction(inputMethodEngine.ExtendAction.COPY).then(() => { 4008 console.info('Succeeded in sending extend action.'); 4009}).catch((err: BusinessError) => { 4010 console.error(`Failed to sendExtendAction. Code is ${err.code}, message is ${err.message}`); 4011}); 4012``` 4013 4014### sendPrivateCommand<sup>12+</sup> 4015 4016sendPrivateCommand(commandData: Record<string, CommandDataType>): Promise<void> 4017 4018发送私有数据至需要与输入法应用通信的系统其他部分。 4019 4020>**说明:** 4021> 4022> - 私有数据通道是系统预置输入法应用与系统特定组件(如文本框、桌面应用等)的通信机制,常用于设备级厂商在特定设备上实现自定义的输入法功能。 4023> - 私有数据规格限制:总大小32KB,数量限制5条。 4024 4025**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4026 4027**参数:** 4028 4029| 参数名 | 类型 | 必填 | 说明 | 4030| ----------- | ------------------------------- | ---- | ---------- | 4031| commandData | Record<string, [CommandDataType](#commanddatatype12)> | 是 | 私有数据。 | 4032 4033**返回值:** 4034 4035| 类型 | 说明 | 4036| ------------------- | ------------------------- | 4037| Promise<void> | 无返回结果的Promise对象。 | 4038 4039**错误码:** 4040 4041以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 4042 4043| 错误码ID | 错误信息 | 4044| -------- | ---------------------------------------------- | 4045| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 4046| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 4047| 12800010 | not the preconfigured default input method. | 4048 4049**示例:** 4050 4051```ts 4052import { BusinessError } from '@kit.BasicServicesKit'; 4053 4054inputMethodEngine.getInputMethodAbility().on('inputStart', (kbController, textInputClient) => { 4055 let record: Record<string, inputMethodEngine.CommandDataType> = { 4056 "valueString1": "abcdefg", 4057 "valueString2": true, 4058 "valueString3": 500, 4059 } 4060 textInputClient.sendPrivateCommand(record).then(() => { 4061 }).catch((err: BusinessError) => { 4062 if (err !== undefined) { 4063 console.error(`sendPrivateCommand catch error: ${err.code} ${err.message}`); 4064 } 4065 }); 4066}) 4067``` 4068 4069### getCallingWindowInfo<sup>12+</sup> 4070 4071getCallingWindowInfo(): Promise<WindowInfo> 4072 4073获取当前拉起输入法的输入框所在应用窗口信息。使用promise异步回调。 4074 4075>**说明:** 4076> 4077>本接口仅适用于适配使用[Panel](#panel10)作为软键盘窗口的输入法应用。 4078 4079**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4080 4081**返回值:** 4082 4083| 类型 | 说明 | 4084| ------------------------------------------ | ----------------------------------------------------- | 4085| Promise<[WindowInfo](#windowinfo12)> | Promise对象,返回拉起输入法的输入框所在应用窗口信息。 | 4086 4087**错误码:** 4088 4089以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 4090 4091| 错误码ID | 错误信息 | 4092| -------- | --------------------------------- | 4093| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 4094| 12800012 | the input method panel does not exist. | 4095| 12800013 | window manager service error. | 4096 4097**示例:** 4098 4099```ts 4100import { BusinessError } from '@kit.BasicServicesKit'; 4101 4102inputClient.getCallingWindowInfo().then((windowInfo: inputMethodEngine.WindowInfo) => { 4103 console.info(`windowInfo.rect: ${windowInfo.rect.left}, ${windowInfo.rect.top}, ${windowInfo.rect.width}, ${windowInfo.rect.height}`); 4104 console.info(`windowInfo.status: ${windowInfo.status}`); 4105}).catch((err: BusinessError) => { 4106 console.error(`Failed to getCallingWindowInfo. Code is ${err.code}, message is ${err.message}`); 4107}); 4108``` 4109 4110### setPreviewText<sup>12+</sup> 4111 4112setPreviewText(text: string, range: Range): Promise<void> 4113 4114设置预上屏文本。使用promise异步回调。 4115 4116**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4117 4118**参数:** 4119 4120| 参数名 | 类型 | 必填 | 说明 | 4121| ------ | ----------------- | ---- | ------------------------------------------------------------ | 4122| text | string | 是 | 将被预上屏的文本。 | 4123| range | [Range](#range10) | 是 | 目标替换的文本范围。<br/>- 当值为{ start: -1, end: -1 }时,默认将参数text替换当前预上屏区域全部文本。<br/>- 当start等于end,默认将参数text插入start对应的光标位置。<br/>- 当start不等于end,将参数text替换range对应区域的文本。<br/>- 当start与end为其他含有负数值的组合,按照参数错误返回。<br/>- 当输入框已有预上屏文本,参数range不得超过预上屏文本范围,否则按照参数错误返回。<br/>- 当输入框无预上屏文本,参数range不得超过输入框文本范围,否则按照参数错误返回。 | 4124 4125**返回值:** 4126 4127| 类型 | 说明 | 4128| ------------------- | ------------------------- | 4129| Promise<void> | 无返回结果的Promise对象。 | 4130 4131**错误码:** 4132 4133以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 4134 4135| 错误码ID | 错误信息 | 4136| -------- | ------------------------------------------------------------ | 4137| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 4138| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 4139| 12800011 | text preview not supported. | 4140 4141**示例:** 4142 4143```ts 4144import { BusinessError } from '@kit.BasicServicesKit'; 4145 4146let range: inputMethodEngine.Range = { start: 0, end: 1 }; 4147inputClient.setPreviewText('test', range).then(() => { 4148 console.info('Succeeded in setting preview text.'); 4149}).catch((err: BusinessError) => { 4150 console.error(`Failed to setPreviewText. Code is ${err.code}, message is ${err.message}`); 4151}); 4152``` 4153 4154### setPreviewTextSync<sup>12+</sup> 4155 4156setPreviewTextSync(text: string, range: Range): void 4157 4158设置预上屏文本。 4159 4160**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4161 4162**参数:** 4163 4164| 参数名 | 类型 | 必填 | 说明 | 4165| ------ | ----------------- | ---- | ------------------------------------------------------------ | 4166| text | string | 是 | 将被预上屏的文本。 | 4167| range | [Range](#range10) | 是 | 目标替换的文本范围。<br/>- 当值为{ start: -1, end: -1 }时,默认将参数text替换当前预上屏区域全部文本。<br/>- 当start等于end,默认将参数text插入start对应的光标位置。<br/>- 当start不等于end,将参数text替换range对应区域的文本。<br/>- 当start与end为其他含有负数值的组合,按照参数错误返回。<br/>- 当输入框已有预上屏文本,参数range不得超过预上屏文本范围,否则按照参数错误返回。<br/>- 当输入框无预上屏文本,参数range不得超过输入框文本范围,否则按照参数错误返回。 | 4168 4169**错误码:** 4170 4171以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 4172 4173| 错误码ID | 错误信息 | 4174| -------- | ------------------------------------------------------------ | 4175| 401 | parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 4176| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 4177| 12800011 | text preview not supported. | 4178 4179**示例:** 4180 4181```ts 4182let range: inputMethodEngine.Range = { start: 0, end: 1 }; 4183inputClient.setPreviewTextSync('test', range); 4184``` 4185 4186### finishTextPreview<sup>12+</sup> 4187 4188finishTextPreview(): Promise<void> 4189 4190结束预上屏。使用promise异步回调。 4191 4192>**说明:** 4193> 4194>若当前输入框已有预上屏状态文本,调用此接口后,预上屏内容将被系统正式上屏。 4195 4196**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4197 4198**返回值:** 4199 4200| 类型 | 说明 | 4201| ------------------- | ------------------------- | 4202| Promise<void> | 无返回结果的Promise对象。 | 4203 4204**错误码:** 4205 4206以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 4207 4208| 错误码ID | 错误信息 | 4209| -------- | ------------------------------ | 4210| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 4211| 12800011 | text preview not supported. | 4212 4213**示例:** 4214 4215```ts 4216import { BusinessError } from '@kit.BasicServicesKit'; 4217 4218inputClient.finishTextPreview().then(() => { 4219 console.info('Succeeded in finishing text preview.'); 4220}).catch((err: BusinessError) => { 4221 console.error(`Failed to finishTextPreview. Code is ${err.code}, message is ${err.message}`); 4222}); 4223``` 4224 4225### finishTextPreviewSync<sup>12+</sup> 4226 4227finishTextPreviewSync(): void 4228 4229结束预上屏。 4230 4231>**说明:** 4232> 4233>若当前输入框已有预上屏状态文本,调用此接口后,预上屏内容将被系统正式上屏。 4234 4235**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4236 4237**错误码:** 4238 4239以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md)。 4240 4241| 错误码ID | 错误信息 | 4242| -------- | ------------------------------ | 4243| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 4244| 12800011 | text preview not supported. | 4245 4246**示例:** 4247 4248```ts 4249inputClient.finishTextPreviewSync(); 4250``` 4251 4252### sendMessage<sup>15+</sup> 4253 4254sendMessage(msgId: string, msgParam?: ArrayBuffer): Promise<void> 4255 4256发送自定义通信至已绑定当前输入法应用的编辑框应用。使用Promise异步回调。 4257 4258> **说明:** 4259> 4260> 该接口需要编辑框与输入法绑定并进入编辑状态,且输入法应用处于完整体验模式时才能调用。 4261> 4262> msgId最大限制256B,msgParam最大限制128KB。 4263 4264**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4265 4266**参数:** 4267 4268| 参数名 | 类型 | 必填 | 说明 | 4269| -------- | ----------- | ---- | ------------------------------------------------------------ | 4270| msgId | string | 是 | 需要发送至已绑定当前输入法应用的编辑框应用的自定义数据的标识符。 | 4271| msgParam | ArrayBuffer | 否 | 需要发送至已绑定当前输入法应用的编辑框应用的自定义数据的消息体。 | 4272 4273**返回值:** 4274 4275| 类型 | 说明 | 4276| ------------------- | ------------------------- | 4277| Promise<void> | 无返回结果的Promise对象。 | 4278 4279**错误码:** 4280 4281以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 4282 4283| 错误码ID | 错误信息 | 4284| -------- | ------------------------------------------- | 4285| 401 | parameter error. Possible causes: 1. Incorrect parameter types. 2. Incorrect parameter length. | 4286| 12800003 | input method client error. Possible causes: 1.the edit box is not focused. 2.no edit box is bound to current input method application. | 4287| 12800009 | input method client detached. | 4288| 12800014 | the input method is in basic mode. | 4289| 12800015 | the other side does not accept the request. | 4290| 12800016 | input method client is not editable. | 4291 4292**示例:** 4293 4294```ts 4295import { BusinessError } from '@kit.BasicServicesKit'; 4296 4297let msgId: string = "testMsgId"; 4298let msgParam: ArrayBuffer = new ArrayBuffer(128); 4299inputClient.sendMessage(msgId, msgParam).then(() => { 4300 console.info('Succeeded send message.'); 4301}).catch((err: BusinessError) => { 4302 console.error(`Failed to send message. Code is ${err.code}, message is ${err.message}`); 4303}); 4304``` 4305 4306### recvMessage<sup>15+</sup> 4307 4308recvMessage(msgHandler?: MessageHandler): void; 4309 4310注册或取消注册Messagehandler。 4311 4312> **说明:** 4313> 4314> [MessageHandler](#messagehandler15)对象全局唯一,多次注册仅保留最后一次注册的对象及有效性,并触发上一个已注册对象的[onTerminated](#onterminated15)回调函数。 4315> 4316> 未填写参数,则取消全局已注册的[MessageHandler](#messagehandler15),并会触发被取消注册对象中[onTerminated](#onterminated15)回调函数。 4317 4318**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4319 4320**参数:** 4321 4322| 参数名 | 类型 | 必填 | 说明 | 4323| ---------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 4324| msgHandler | [MessageHandler](#messagehandler15) | 否 | 该对象将通过[onMessage](#onmessage15)接收来自已绑定当前输入法应用的编辑框应用所发送的自定义通信数据,并通过[onTerminated](#onterminated15)接收终止此对象订阅的消息。<br>若不填写此参数,则取消全局已注册的[MessageHandler](#messagehandler15)对象,同时触发其[onTerminated](#onterminated15)回调函数。 | 4325 4326**错误码:** 4327 4328以下错误码的详细介绍请参见[输入法框架错误码](errorcode-inputmethod-framework.md),[通用错误码说明文档](../errorcode-universal.md)。 4329 4330| 错误码ID | 错误信息 | 4331| -------- | ---------------- | 4332| 401 | parameter error. Possible causes: 1. Incorrect parameter types. | 4333 4334**示例:** 4335 4336```ts 4337inputMethodEngine.getInputMethodAbility() 4338 .on('inputStart', 4339 (kbController: inputMethodEngine.KeyboardController, client: inputMethodEngine.InputClient) => { 4340 let keyboardController: inputMethodEngine.KeyboardController = kbController; 4341 let inputClient: inputMethodEngine.InputClient = client; 4342 let messageHandler: inputMethodEngine.MessageHandler = { 4343 onTerminated(): void { 4344 console.info('OnTerminated.'); 4345 }, 4346 onMessage(msgId: string, msgParam?: ArrayBuffer): void { 4347 console.info('recv message.'); 4348 } 4349 } 4350 inputClient.recvMessage(messageHandler); 4351 }); 4352``` 4353 4354### getAttachOptions<sup>19+</sup> 4355 4356getAttachOptions(): AttachOptions 4357 4358获取绑定输入法时的附加选项。 4359 4360**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4361 4362**返回值:** 4363 4364| 类型 | 说明 | 4365| ---- | ------------ | 4366| [AttachOptions](#attachoptions19) | 返回绑定输入法时的附加选项内容。 | 4367 4368> **注意:** 4369> 4370> 从API version 20 开始,错误码`801 Capability not supported.`被移除。 4371 4372**示例:** 4373 4374```ts 4375let attachOptions: inputMethodEngine.AttachOptions = inputClient.getAttachOptions(); 4376console.info(`Succeeded in getting AttachOptions, AttachOptions is ${attachOptions}`); 4377``` 4378 4379### on('attachOptionsDidChange')<sup>19+</sup> 4380 4381on(type: 'attachOptionsDidChange', callback: Callback\<AttachOptions>): void 4382 4383订阅绑定输入法时的附加选项变更事件。使用callback异步回调。 4384 4385**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4386 4387**参数:** 4388 4389| 参数名 | 类型 | 必填 | 说明 | 4390| -------- | ------------------------------------------- | ---- | ---------------------------------------------- | 4391| type | string | 是 | 绑定输入法时的附加选项变更事件,固定取值为'attachOptionsDidChange'。 | 4392| callback | Callback\<[AttachOptions](#attachoptions19)> | 是 | 回调函数,返回绑定输入法时的附加选项。 | 4393 4394> **注意:** 4395> 4396> 从API version 20 开始,错误码`801 Capability not supported.`被移除。 4397 4398**示例:** 4399 4400```ts 4401let attachOptionsDidChangeCallback: (attachOptions: inputMethodEngine.AttachOptions) => void = 4402 (attachOptions: inputMethodEngine.AttachOptions) => { 4403 console.info(`AttachOptionsDidChangeCallback1: attachOptionsDidChange event triggered`); 4404 }; 4405 4406inputClient.on('attachOptionsDidChange', attachOptionsDidChangeCallback); 4407console.info(`attachOptionsDidChangeCallback subscribed to attachOptionsDidChange`); 4408inputClient.off('attachOptionsDidChange', attachOptionsDidChangeCallback); 4409console.info(`attachOptionsDidChange unsubscribed from attachOptionsDidChange`); 4410``` 4411 4412### off('attachOptionsDidChange')<sup>19+</sup> 4413 4414off(type: 'attachOptionsDidChange', callback?: Callback\<AttachOptions>): void 4415 4416取消订阅绑定输入法时的附加选项变更事件。使用callback异步回调。 4417 4418**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4419 4420**参数:** 4421 4422| 参数名 | 类型 | 必填 | 说明 | 4423| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 4424| type | string | 是 | 绑定输入法时的附加选项变更事件,固定取值为'attachOptionsDidChange'。 | 4425| callback | Callback\<[AttachOptions](#attachoptions19)> | 否 | 取消订阅的回调函数。参数不填写时,默认取消订阅type对应的所有回调事件。 | 4426 4427**示例:** 4428 4429```ts 4430let attachOptionsDidChangeCallback: (attachOptions: inputMethodEngine.AttachOptions) => void = 4431 (attachOptions: inputMethodEngine.AttachOptions) => { 4432 console.info(`AttachOptionsDidChangeCallback1: attachOptionsDidChange event triggered`); 4433 }; 4434 4435inputClient.on('attachOptionsDidChange', attachOptionsDidChangeCallback); 4436console.info(`attachOptionsDidChangeCallback subscribed to attachOptionsDidChange`); 4437inputClient.off('attachOptionsDidChange', attachOptionsDidChangeCallback); 4438console.info(`attachOptionsDidChange unsubscribed from attachOptionsDidChange`); 4439``` 4440 4441### CapitalizeMode<sup>20+</sup> 4442 4443枚举,定义了文本首字母大写的不同模式。 4444 4445**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4446 4447| 名称 | 值 | 说明 | 4448| -------- | -- | -------- | 4449| NONE | 0 | 不进行任何首字母大写处理。| 4450| SENTENCES | 1 | 每个句子的首字母大写。| 4451| WORDS | 2 | 每个单词的首字母大写。| 4452| CHARACTERS | 3 | 每个字母都大写。| 4453 4454### EditorAttribute 4455 4456编辑框属性值。 4457 4458**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4459 4460| 名称 | 类型 | 只读 | 可选 | 说明 | 4461| ------------ | -------- | ---- | ---- | ------------------ | 4462| enterKeyType | number | 是 | 否 | 编辑框的功能属性,详见[常量中的功能键定义](#常量)。 | 4463| inputPattern | number | 是 | 否 | 编辑框的文本属性,详见[常量中的编辑框定义](#常量)。 | 4464| isTextPreviewSupported<sup>12+</sup> | boolean | 否 | 否 | 编辑框是否支持预上屏。<br/>- 值为true,表示支持。<br/>- 值为false,表示不支持。 | 4465| bundleName<sup>14+</sup> | string | 是 | 是 | 编辑框所属应用包名;该值可能为"",使用该属性时需要考虑为""的场景。 | 4466| immersiveMode<sup>15+</sup> | [ImmersiveMode](#immersivemode15) | 是 | 是 | 输入法沉浸模式。 | 4467| windowId<sup>18+</sup> | number | 是 | 是 | 编辑框设置所属窗口ID。 | 4468| displayId<sup>18+</sup> | number | 是 | 是 | 编辑框设置窗口对应的屏幕ID。如果没有设置windowId,取当前焦点窗口屏幕ID。| 4469| placeholder<sup>20+</sup> | string | 是 | 是 | 编辑框设置的占位符信息。| 4470| abilityName<sup>20+</sup> | string | 是 | 是 | 编辑框设置的ability名称。| 4471| capitalizeMode<sup>20+</sup> | [CapitalizeMode](#capitalizemode20) | 是 | 是 | 编辑框设置大小写模式。如果没有设置或设置非法值,默认不进行任何首字母大写处理。| 4472| gradientMode<sup>20+</sup> | [GradientMode](#gradientmode20) | 是 | 是 | 渐变模式。如果没有设置或设置非法值,默认不使用渐变模式。| 4473 4474## KeyEvent 4475 4476按键属性值。 4477 4478**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4479 4480| 名称 | 类型 | 只读 | 可选 | 说明 | 4481| --------- | -------- | ---- | ---- | ------------ | 4482| keyCode | number | 是 | 否 | 按键的键值。键码值说明参考[KeyCode](../apis-input-kit/js-apis-keycode.md#keycode)。 | 4483| keyAction | number | 是 | 否 | 按键事件类型。<br/>- 当值为2时,表示按下事件;<br/>- 当值为3时,表示抬起事件。 | 4484 4485## PanelFlag<sup>10+</sup> 4486 4487输入法面板状态类型枚举。 4488 4489**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4490 4491| 名称 | 值 | 说明 | 4492| ------------ | -- | ------------------ | 4493| FLG_FIXED | 0 | 固定态面板类型。 | 4494| FLG_FLOATING | 1 | 悬浮态面板类型。 | 4495| FLAG_CANDIDATE<sup>15+</sup> | 2 | 候选词态面板类型。 | 4496 4497## PanelType<sup>10+</sup> 4498 4499输入法面板类型枚举。 4500 4501**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4502 4503| 名称 | 值 | 说明 | 4504| ------------ | -- | ------------------ | 4505| SOFT_KEYBOARD | 0 | 软键盘类型。 | 4506| STATUS_BAR | 1 | 状态栏类型。 | 4507 4508## PanelInfo<sup>10+</sup> 4509 4510输入法面板属性。 4511 4512**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4513 4514| 名称 | 类型 | 只读 | 可选 | 说明 | 4515| --------- | -------- | ---- | ---- | ------------ | 4516| type | [PanelType](#paneltype10) | 否 | 否 | 面板的类型。 | 4517| flag | [PanelFlag](#panelflag10) | 否 | 是 | 面板的状态类型。 | 4518 4519## PanelRect<sup>12+</sup> 4520 4521输入法面板位置大小信息。 4522 4523**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4524 4525| 名称 | 类型 | 只读 | 可选 | 说明 | 4526| ------------ | -------- | ---- | ---- | ------------------ | 4527| landscapeRect | [window.Rect](../apis-arkui/arkts-apis-window-i.md#rect7) | 否 | 否 | 横屏状态时输入法面板窗口的位置大小。 | 4528| portraitRect | [window.Rect](../apis-arkui/arkts-apis-window-i.md#rect7) | 否 | 否 | 竖屏状态时输入法面板窗口的位置大小。 | 4529 4530## EnhancedPanelRect<sup>15+</sup> 4531 4532增强的输入法面板位置、大小信息,包含自定义避让区域、自定义热区。 4533 4534**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4535 4536| 名称 | 类型 | 只读 | 可选 | 说明 | 4537| -------------------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 4538| landscapeRect | [window.Rect](../apis-arkui/arkts-apis-window-i.md#rect7) | 否 | 是 | 横屏状态时输入法面板窗口的位置大小。<br/>- 当fullScreenMode不填写或值为false时,此属性为必选。 | 4539| portraitRect | [window.Rect](../apis-arkui/arkts-apis-window-i.md#rect7) | 否 | 是 | 竖屏状态时,输入法面板窗口的位置大小。<br/>- 当fullScreenMode不填写或值为false时,此属性为必选。 | 4540| landscapeAvoidY | number | 否 | 是 | 横屏状态时,面板中的避让线距离面板顶部的距离。默认值为0。<br/>- 应用内其他系统组件会对避让线以下的输入法面板区域进行避让。<br/>- 面板为固定态时,避让线到屏幕底部的高度不能超过屏幕高度的70%。 | 4541| landscapeInputRegion | Array<[window.Rect](../apis-arkui/arkts-apis-window-i.md#rect7)> | 否 | 是 | 横屏状态时,面板接收输入事件的区域。<br/>- 数组大小限制为[1, 4]。默认值为横屏时的面板大小。<br/>- 传入的热区位置是相对于输入法面板窗口左顶点的位置。 | 4542| portraitAvoidY | number | 否 | 是 | 竖屏状态时,面板中的避让线距离面板顶部的距离。默认值为0。<br/>- 应用内其他系统组件会对避让线以下的输入法面板区域进行避让。<br/>- 面板为固定态时,避让线到屏幕底部的高度不能超过屏幕高度的70%。 | 4543| portraitInputRegion | Array<[window.Rect](../apis-arkui/arkts-apis-window-i.md#rect7)> | 否 | 是 | 竖屏状态时,面板接收输入事件的区域。<br/>- 数组大小限制为[1, 4]。默认值为竖屏时的面板大小。<br/>- 传入的热区位置是相对于输入法面板窗口左顶点的位置。 | 4544| fullScreenMode | boolean | 否 | 是 | 是否开启全屏模式。默认值为false。<br/>- 值为true,landscapeRect和portraitRect可不填写。<br/>- 值为false,landscapeRect和portraitRect为必选属性。 | 4545 4546## KeyboardArea<sup>15+</sup> 4547 4548面板中的键盘区域。 4549 4550**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4551 4552| 名称 | 类型 | 只读 | 可选 | 说明 | 4553| ------ | ------ | ---- | ---- | ------------------------------------------------------------ | 4554| top | number | 是 | 否 | 键盘区域的上边界到面板区域上边界的距离,单位为px,该参数为整数。 | 4555| bottom | number | 是 | 否 | 键盘区域的下边界到面板区域下边界的距离,单位为px,该参数为整数。 | 4556| left | number | 是 | 否 | 键盘区域的左边界到面板区域左边界的距离,单位为px,该参数为整数。 | 4557| right | number | 是 | 否 | 键盘区域的右边界到面板区域右边界的距离,单位为px,该参数为整数。 | 4558 4559## AttachOptions<sup>19+</sup> 4560 4561绑定输入法时的附加选项。 4562 4563**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4564 4565| 名称 | 类型 | 只读 | 可选 | 说明 | 4566| ------ | ------ | ---- | ---- | ---------------------------------------------------------- | 4567| requestKeyboardReason | [RequestKeyboardReason](#requestkeyboardreason19) | 否 | 是 | 该属性由编辑框应用设置,如果没有设置或设置非法值,则默认没有特定的原因触发键盘请求。 | 4568| isSimpleKeyboardEnabled<sup>20+</sup> | boolean | 否 | 是 | 是否使能简单键盘,该属性由编辑框应用设置,true表示使能简单键盘,false表示不使能简单键盘。<br/> 如果没有设置或设置非法值,则默认不使能简单键盘。 | 4569 4570## WindowInfo<sup>12+</sup> 4571 4572窗口信息。 4573 4574**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4575 4576| 名称 | 类型 | 只读 | 可选 | 说明 | 4577| ------ | ------------------------------------------------------------ | ---- | ---- | -------------- | 4578| rect | [window.Rect](../apis-arkui/arkts-apis-window-i.md#rect7) | 否 | 否 | 窗口矩形区域。 | 4579| status | [window.WindowStatusType](../apis-arkui/arkts-apis-window-e.md#windowstatustype11) | 否 | 否 | 窗口模式类型。 | 4580 4581## ImmersiveMode<sup>15+</sup> 4582 4583枚举,输入法沉浸模式。 4584 4585**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4586 4587| 名称 | 值 | 说明 | 4588| ------------ | -- | ------------------ | 4589| NONE_IMMERSIVE | 0 | 不使用沉浸模式。 | 4590| IMMERSIVE | 1 | 沉浸模式,由输入法应用确定沉浸模式类型。 | 4591| LIGHT_IMMERSIVE | 2 | 浅色沉浸模式。 | 4592| DARK_IMMERSIVE | 3 | 深色沉浸模式。 | 4593 4594## RequestKeyboardReason<sup>19+</sup> 4595 4596枚举,请求键盘输入原因。 4597 4598**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4599 4600| 名称 | 值 | 说明 | 4601| ------------ | -- | ------------------ | 4602| NONE | 0 | 表示没有特定的原因触发键盘请求。 | 4603| MOUSE | 1 | 表示键盘请求是由鼠标操作触发的。 | 4604| TOUCH | 2 | 表示键盘请求是由触摸操作触发的。 | 4605| OTHER | 20 | 表示键盘请求是由其他原因触发的。 | 4606 4607## GradientMode<sup>20+</sup> 4608 4609枚举,输入法渐变模式。 4610 4611**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4612 4613| 名称 | 值 | 说明 | 4614| ------------ | -- | ------------------ | 4615| NONE | 0 | 不使用渐变模式。 | 4616| LINEAR_GRADIENT | 1 | 线性渐变。 | 4617 4618## ImmersiveEffect<sup>20+</sup> 4619 4620沉浸效果。 4621 4622**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4623 4624| 名称 | 类型 | 只读 | 可选 | 说明 | 4625| ------ | ------------------------------------ | ---- | ---- | -------------- | 4626| gradientHeight | number | 否 | 否 | 渐变高度,不能超过屏幕高度的15%。| 4627| gradientMode | [GradientMode](#gradientmode20) | 否 | 否 | 渐变模式。 | 4628 4629## TextInputClient<sup>(deprecated)</sup> 4630 4631> **说明:** 4632> 4633> 从API version 8开始支持,API version 9开始废弃,建议使用[InputClient](#inputclient9)替代。 4634 4635下列API示例中都需使用[on('inputStart')](#oninputstart)回调获取到TextInputClient实例,再通过此实例调用对应方法。 4636 4637### getForward<sup>(deprecated)</sup> 4638 4639getForward(length:number, callback: AsyncCallback<string>): void 4640 4641获取光标前固定长度的文本。使用callback异步回调。 4642 4643> **说明:** 4644> 4645> 从API version 8开始支持,API version 9开始废弃,建议使用[getForward](#getforward9)替代。 4646 4647**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4648 4649**参数:** 4650 4651| 参数名 | 类型 | 必填 | 说明 | 4652| -------- | -------- | -------- | -------- | 4653| length | number | 是 | 文本长度。不能小于0。 | 4654| callback | AsyncCallback<string> | 是 | 回调函数。当光标前固定长度的文本获取成功,err为undefined,data为获取到的文本;否则为错误对象。| 4655 4656**示例:** 4657 4658```ts 4659import { BusinessError } from '@kit.BasicServicesKit'; 4660 4661let length: number = 1; 4662textInputClient.getForward(length, (err: BusinessError, text: string) => { 4663 if (err) { 4664 console.error(`Failed to getForward. Code is ${err.code}, message is ${err.message}`); 4665 return; 4666 } 4667 console.info('Succeeded in getting forward, text: ' + text); 4668}); 4669``` 4670 4671### getForward<sup>(deprecated)</sup> 4672 4673getForward(length:number): Promise<string> 4674 4675获取光标前固定长度的文本。使用promise异步回调。 4676 4677> **说明:** 4678> 4679> 从API version 8开始支持,API version 9开始废弃,建议使用[getForward](#getforward9)替代。 4680 4681**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4682 4683**参数:** 4684 4685| 参数名 | 类型 | 必填 | 说明 | 4686| -------- | -------- | -------- | -------- | 4687| length | number | 是 | 文本长度。不能小于0。| 4688 4689**返回值:** 4690 4691| 类型 | 说明 | 4692| ------------------------------- | ------------------------------------------------------------ | 4693| Promise<string> | Promise对象,返回光标前固定长度的文本。 | 4694 4695**示例:** 4696 4697```ts 4698import { BusinessError } from '@kit.BasicServicesKit'; 4699 4700let length: number = 1; 4701textInputClient.getForward(length).then((text: string) => { 4702 console.info('Succeeded in getting forward, text: ' + text); 4703}).catch((err: BusinessError) => { 4704 console.error(`Failed to getForward. Code is ${err.code}, message is ${err.message}`); 4705}); 4706``` 4707 4708### getBackward<sup>(deprecated)</sup> 4709 4710getBackward(length:number, callback: AsyncCallback<string>): void 4711 4712获取光标后固定长度的文本。使用callback异步回调。 4713 4714> **说明:** 4715> 4716> 从API version 8开始支持,API version 9开始废弃,建议使用[getBackward](#getbackward9)替代。 4717 4718**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4719 4720**参数:** 4721 4722| 参数名 | 类型 | 必填 | 说明 | 4723| -------- | -------- | -------- | -------- | 4724| length | number | 是 | 文本长度。不能小于0。 | 4725| callback | AsyncCallback<string> | 是 | 回调函数。当光标后固定长度的文本获取成功,err为undefined,data为获取到的文本;否则为错误对象。 | 4726 4727**示例:** 4728 4729```ts 4730import { BusinessError } from '@kit.BasicServicesKit'; 4731 4732let length: number = 1; 4733textInputClient.getBackward(length, (err: BusinessError, text: string) => { 4734 if (err) { 4735 console.error(`Failed to getBackward. Code is ${err.code}, message is ${err.message}`); 4736 return; 4737 } 4738 console.info('Succeeded in getting backward, text: ' + text); 4739}); 4740``` 4741 4742### getBackward<sup>(deprecated)</sup> 4743 4744getBackward(length:number): Promise<string> 4745 4746获取光标后固定长度的文本。使用promise异步回调。 4747 4748> **说明:** 4749> 4750> 从API version 8开始支持,API version 9开始废弃,建议使用[getBackward](#getbackward9)替代。 4751 4752**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4753 4754**参数:** 4755 4756| 参数名 | 类型 | 必填 | 说明 | 4757| -------- | -------- | -------- | -------- | 4758| length | number | 是 | 文本长度。不能小于0。 | 4759 4760**返回值:** 4761 4762| 类型 | 说明 | 4763| ------------------------------- | ------------------------------------------------------------ | 4764| Promise<string> | Promise对象,返回光标后固定长度的文本。 | 4765 4766**示例:** 4767 4768```ts 4769import { BusinessError } from '@kit.BasicServicesKit'; 4770 4771let length: number = 1; 4772textInputClient.getBackward(length).then((text: string) => { 4773 console.info(`'Succeeded in getting backward: ${text}`); 4774}).catch((err: BusinessError) => { 4775 console.error(`Failed to getBackward. Code is ${err.code}, message is ${err.message}`); 4776}); 4777``` 4778 4779### deleteForward<sup>(deprecated)</sup> 4780 4781deleteForward(length:number, callback: AsyncCallback<boolean>): void 4782 4783删除光标前固定长度的文本。使用callback异步回调。 4784 4785> **说明:** 4786> 4787> 从API version 8开始支持,API version 9开始废弃,建议使用[deleteForward](#deleteforward9)替代。 4788 4789**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4790 4791**参数:** 4792 4793| 参数名 | 类型 | 必填 | 说明 | 4794| -------- | -------- | -------- | -------- | 4795| length | number | 是 | 文本长度。不能小于0。 | 4796| callback | AsyncCallback<boolean> | 是 | 回调函数。当光标前固定长度的文本删除成功,err为undefined,data为true;否则为错误对象。 | 4797 4798**示例:** 4799 4800```ts 4801import { BusinessError } from '@kit.BasicServicesKit'; 4802 4803let length: number = 1; 4804textInputClient.deleteForward(length, (err: BusinessError, result: boolean) => { 4805 if (err) { 4806 console.error(`Failed to deleteForward. Code is ${err.code}, message is ${err.message}`); 4807 return; 4808 } 4809 if (result) { 4810 console.info('Succeeded in deleting forward.'); 4811 } else { 4812 console.error('Failed to deleteForward.'); 4813 } 4814}); 4815``` 4816 4817### deleteForward<sup>(deprecated)</sup> 4818 4819deleteForward(length:number): Promise<boolean> 4820 4821删除光标前固定长度的文本。使用promise异步回调。 4822 4823> **说明:** 4824> 4825> 从API version 8开始支持,API version 9开始废弃,建议使用[deleteForward](#deleteforward9)替代。 4826 4827**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4828 4829**参数:** 4830 4831| 参数名 | 类型 | 必填 | 说明 | 4832| ------ | ------ | ---- | ---------- | 4833| length | number | 是 | 文本长度。不能小于0。 | 4834 4835**返回值:** 4836 4837| 类型 | 说明 | 4838| ---------------------- | -------------- | 4839| Promise<boolean> | Promise对象。返回true表示删除光标前固定长度的文本成功;返回false表示删除光标前固定长度的文本失败。| 4840 4841**示例:** 4842 4843```ts 4844import { BusinessError } from '@kit.BasicServicesKit'; 4845 4846let length: number = 1; 4847textInputClient.deleteForward(length).then((result: boolean) => { 4848 if (result) { 4849 console.info('Succeeded in deleting forward.'); 4850 } else { 4851 console.error('Failed to delete forward.'); 4852 } 4853}).catch((err: BusinessError) => { 4854 console.error(`Failed to deleteForward. Code is ${err.code}, message is ${err.message}`); 4855}); 4856``` 4857 4858### deleteBackward<sup>(deprecated)</sup> 4859 4860deleteBackward(length:number, callback: AsyncCallback<boolean>): void 4861 4862删除光标后固定长度的文本。使用callback异步回调。 4863 4864> **说明:** 4865> 4866> 从API version 8开始支持,API version 9开始废弃,建议使用[deleteBackward](#deletebackward9)替代。 4867 4868**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4869 4870**参数:** 4871 4872| 参数名 | 类型 | 必填 | 说明 | 4873| -------- | ---------------------------- | ---- | -------------- | 4874| length | number | 是 | 文本长度。不能小于0。 | 4875| callback | AsyncCallback<boolean> | 是 | 回调函数。当光标后固定长度的文本删除成功,err为undefined,data为true;否则为错误对象。| 4876 4877**示例:** 4878 4879```ts 4880import { BusinessError } from '@kit.BasicServicesKit'; 4881 4882let length: number = 1; 4883textInputClient.deleteBackward(length, (err: BusinessError, result: boolean) => { 4884 if (err) { 4885 console.error(`Failed to deleteBackward. Code is ${err.code}, message is ${err.message}`); 4886 return; 4887 } 4888 if (result) { 4889 console.info('Succeeded in deleting backward.'); 4890 } else { 4891 console.error('Failed to deleteBackward.'); 4892 } 4893}); 4894``` 4895 4896### deleteBackward<sup>(deprecated)</sup> 4897 4898deleteBackward(length:number): Promise<boolean> 4899 4900删除光标后固定长度的文本。使用promise异步回调。 4901 4902> **说明:** 4903> 4904> 从API version 8开始支持,API version 9开始废弃,建议使用[deleteBackward](#deletebackward9)替代。 4905 4906**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4907 4908**参数:** 4909 4910| 参数名 | 类型 | 必填 | 说明 | 4911| -------- | -------- | -------- | -------- | 4912| length | number | 是 | 文本长度。不能小于0。 | 4913 4914**返回值:** 4915 4916| 类型 | 说明 | 4917| ------------------------------- | ------------------------------------------------------------ | 4918| Promise<boolean> | Promise对象。返回true表示删除光标后固定长度的文本成功;返回false表示删除光标后固定长度的文本失败。| 4919 4920**示例:** 4921 4922```ts 4923import { BusinessError } from '@kit.BasicServicesKit'; 4924 4925let length: number = 1; 4926textInputClient.deleteBackward(length).then((result: boolean) => { 4927 if (result) { 4928 console.info('Succeeded in deleting backward.'); 4929 } else { 4930 console.error('Failed to deleteBackward.'); 4931 } 4932}).catch((err: BusinessError) => { 4933 console.error(`Failed to deleteBackward. Code is ${err.code}, message is ${err.message}`); 4934}); 4935``` 4936### sendKeyFunction<sup>(deprecated)</sup> 4937 4938sendKeyFunction(action: number, callback: AsyncCallback<boolean>): void 4939 4940发送功能键。使用callback异步回调。 4941 4942> **说明:** 4943> 4944> 从API version 8开始支持,API version 9开始废弃,建议使用[sendKeyFunction](#sendkeyfunction9)替代。 4945 4946**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4947 4948**参数:** 4949 4950| 参数名 | 类型 | 必填 | 说明 | 4951| -------- | -------- | -------- | -------- | 4952| action | number | 是 | 功能键键值。<br/>- 当值为0时,表示无效按键;<br/>- 当值为1时,表示确认键(即回车键)。 | 4953| callback | AsyncCallback<boolean> | 是 | 回调函数。当功能键发送成功,err为undefined,data为true;否则为错误对象。 | 4954 4955**示例:** 4956 4957```ts 4958import { BusinessError } from '@kit.BasicServicesKit'; 4959 4960let action: number = 1; 4961textInputClient.sendKeyFunction(action, (err: BusinessError, result: boolean) => { 4962 if (err) { 4963 console.error(`Failed to sendKeyFunction. Code is ${err.code}, message is ${err.message}`); 4964 return; 4965 } 4966 if (result) { 4967 console.info('Succeeded in sending key function.'); 4968 } else { 4969 console.error('Failed to sendKeyFunction.'); 4970 } 4971}); 4972``` 4973 4974### sendKeyFunction<sup>(deprecated)</sup> 4975 4976sendKeyFunction(action: number): Promise<boolean> 4977 4978发送功能键。使用promise异步回调。 4979 4980> **说明:** 4981> 4982> 从API version 8开始支持,API version 9开始废弃,建议使用[sendKeyFunction](#sendkeyfunction9)替代。 4983 4984**系统能力:** SystemCapability.MiscServices.InputMethodFramework 4985 4986**参数:** 4987 4988| 参数名 | 类型 | 必填 | 说明 | 4989| -------- | -------- | -------- | -------- | 4990| action | number | 是 | 功能键键值。<br/>当值为0时,表示无效按键;<br/>当值为1时,表示确认键(即回车键)。 | 4991 4992**返回值:** 4993 4994| 类型 | 说明 | 4995| ------------------------------- | ------------------------------------------------------------ | 4996| Promise<boolean> | Promise对象。返回true表示发送功能键成功;返回false表示发送功能键失败。 | 4997 4998**示例:** 4999 5000```ts 5001import { BusinessError } from '@kit.BasicServicesKit'; 5002 5003let action: number = 1; 5004textInputClient.sendKeyFunction(action).then((result: boolean) => { 5005 if (result) { 5006 console.info('Succeeded in sending key function.'); 5007 } else { 5008 console.error('Failed to sendKeyFunction.'); 5009 } 5010}).catch((err: BusinessError) => { 5011 console.error(`Failed to sendKeyFunction:. Code is ${err.code}, message is ${err.message}`); 5012}); 5013``` 5014 5015### insertText<sup>(deprecated)</sup> 5016 5017insertText(text:string, callback: AsyncCallback<boolean>): void 5018 5019插入文本。使用callback异步回调。 5020 5021> **说明:** 5022> 5023> 从API version 8开始支持,API version 9开始废弃,建议使用[insertText](#inserttext9)替代。 5024 5025**系统能力:** SystemCapability.MiscServices.InputMethodFramework 5026 5027**参数:** 5028 5029| 参数名 | 类型 | 必填 | 说明 | 5030| -------- | -------- | -------- | -------- | 5031| text | string | 是 | 文本。 | 5032| callback | AsyncCallback<boolean> | 是 | 回调函数。当文本插入成功,err为undefined,data为true;否则为错误对象。 | 5033 5034**示例:** 5035 5036```ts 5037import { BusinessError } from '@kit.BasicServicesKit'; 5038 5039textInputClient.insertText('test', (err: BusinessError, result: boolean) => { 5040 if (err) { 5041 console.error(`Failed to insertText. Code is ${err.code}, message is ${err.message}`); 5042 return; 5043 } 5044 if (result) { 5045 console.info('Succeeded in inserting text.'); 5046 } else { 5047 console.error('Failed to insertText.'); 5048 } 5049}); 5050``` 5051 5052### insertText<sup>(deprecated)</sup> 5053 5054insertText(text:string): Promise<boolean> 5055 5056插入文本。使用promise异步回调。 5057 5058> **说明:** 5059> 5060> 从API version 8开始支持,API version 9开始废弃,建议使用[insertText](#inserttext9)替代。 5061 5062**系统能力:** SystemCapability.MiscServices.InputMethodFramework 5063 5064**参数:** 5065 5066| 参数名 | 类型 | 必填 | 说明 | 5067| -------- | -------- | -------- | -------- | 5068| text | string | 是 | 文本。 | 5069 5070**返回值:** 5071 5072| 类型 | 说明 | 5073| ------------------------------- | ------------------------------------------------------------ | 5074| Promise<boolean> | Promise对象。返回true表示插入文本成功;返回false表示插入文本失败。 | 5075 5076**示例:** 5077 5078```ts 5079import { BusinessError } from '@kit.BasicServicesKit'; 5080 5081textInputClient.insertText('test').then((result: boolean) => { 5082 if (result) { 5083 console.info('Succeeded in inserting text.'); 5084 } else { 5085 console.error('Failed to insertText.'); 5086 } 5087}).catch((err: BusinessError) => { 5088 console.error(`Failed to insertText. Code is ${err.code}, message is ${err.message}`); 5089}); 5090``` 5091 5092### getEditorAttribute<sup>(deprecated)</sup> 5093 5094getEditorAttribute(callback: AsyncCallback<EditorAttribute>): void 5095 5096获取编辑框属性值。使用callback异步回调。 5097 5098> **说明:** 5099> 5100> 从API version 8开始支持,API version 9开始废弃,建议使用[getEditorAttribute](#geteditorattribute9)替代。 5101 5102**系统能力:** SystemCapability.MiscServices.InputMethodFramework 5103 5104**参数:** 5105 5106| 参数名 | 类型 | 必填 | 说明 | 5107| -------- | ----- | ----- | ----- | 5108| callback | AsyncCallback<[EditorAttribute](#editorattribute)> | 是 | 回调函数。当编辑框的属性值获取成功,err为undefined,data为编辑框属性值;否则为错误对象。| 5109 5110**示例:** 5111 5112```ts 5113import { BusinessError } from '@kit.BasicServicesKit'; 5114 5115 5116textInputClient.getEditorAttribute((err: BusinessError, 5117 editorAttribute: inputMethodEngine.EditorAttribute) => { 5118 if (err) { 5119 console.error(`Failed to getEditorAttribute. Code is ${err.code}, message is ${err.message}`); 5120 return; 5121 } 5122 console.info(`editorAttribute.inputPattern: ${editorAttribute.inputPattern}`); 5123 console.info(`editorAttribute.enterKeyType: ${editorAttribute.enterKeyType}`); 5124}); 5125``` 5126 5127### getEditorAttribute<sup>(deprecated)</sup> 5128 5129getEditorAttribute(): Promise<EditorAttribute> 5130 5131获取编辑框属性值。使用promise异步回调。 5132 5133> **说明:** 5134> 5135> 从API version 8开始支持,API version 9开始废弃,建议使用[getEditorAttribute](#geteditorattribute9)替代。 5136 5137**系统能力:** SystemCapability.MiscServices.InputMethodFramework 5138 5139**返回值:** 5140 5141| 类型 | 说明 | 5142| ------------------------------- | ------------------------------------------------------------ | 5143| Promise<[EditorAttribute](#editorattribute)> | Promise对象,返回编辑框属性值。 | 5144 5145**示例:** 5146 5147```ts 5148import { BusinessError } from '@kit.BasicServicesKit'; 5149 5150textInputClient.getEditorAttribute().then((editorAttribute: inputMethodEngine.EditorAttribute) => { 5151 console.info(`editorAttribute.inputPattern: ${editorAttribute.inputPattern}`); 5152 console.info(`editorAttribute.enterKeyType: ${editorAttribute.enterKeyType}}`); 5153}).catch((err: BusinessError) => { 5154 console.error(`Failed to getEditorAttribute. Code is ${err.code}, message is ${err.message}`); 5155}); 5156``` 5157<!--no_check-->