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