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