1# @ohos.selectionInput.selectionManager (Word Selection Management) (System API) 2 3This module provides word selection management capabilities, including creating, displaying, moving, hiding, and destroying windows, listening for word selection events, and retrieving the selected text. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 20. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> - APIs of this module can be called only by applications that integrate the ExtensionAbility for word selection. 9> - The APIs provided by this module are system APIs. 10 11## Modules to Import 12 13```ts 14import { selectionManager } from '@kit.BasicServicesKit'; 15``` 16 17 18## on('selectionCompleted') 19 20on(type: 'selectionCompleted', callback: Callback\<SelectionInfo>): void 21 22Registers a callback to listen for the word selection completion event. This API uses an asynchronous callback to return the result. 23 24**System capability**: SystemCapability.SelectionInput.Selection 25 26**Parameters** 27 28| Name | Type | Mandatory| Description | 29| -------- | ------------------------------------------- | ---- | ---------------------------------------------- | 30| type | string | Yes | Event type, which is **'selectionCompleted'**.| 31| callback | Callback\<[SelectionInfo](#selectioninfo)> | Yes | Callback used to return the word selection information. | 32 33**Error codes** 34 35For details about the error codes, see [Word Selection Service Error Codes](errorcode-selection.md). 36 37| ID | Error Message | 38| ---------- | ----------------------------- | 39| 33600003 | Invalid operation. The selection app is not valid. | 40 41**Example** 42 43```ts 44import { selectionManager } from '@kit.BasicServicesKit'; 45 46try { 47 selectionManager.on('selectionCompleted', (info: selectionManager.SelectionInfo) => { 48 console.info(`SelectionInfo text: ${info.text}`); 49 }); 50} catch (err) { 51 console.error(`Failed to register selectionCompleted callback: ${JSON.stringify(err)}`); 52} 53``` 54 55## off('selectionCompleted') 56 57off(type: 'selectionCompleted', callback?: Callback\<SelectionInfo>): void 58 59Unregisters the callback used to listen for the word selection completion event. This API uses an asynchronous callback to return the result. 60 61**System capability**: SystemCapability.SelectionInput.Selection 62 63**Parameters** 64 65| Name | Type | Mandatory| Description | 66| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 67| type | string | Yes | Event type, which is **'selectionCompleted'**. | 68| callback | Callback\<[SelectionInfo](#selectioninfo)> | No | Callback to unregister. If this parameter is not specified, this API unregisters all callbacks for the specified type.| 69 70**Example** 71 72```ts 73import { selectionManager } from '@kit.BasicServicesKit'; 74 75let selectionChangeCallback = (info: selectionManager.SelectionInfo) => { 76 console.info(`SelectionInfo text: ${info.text}`); 77}; 78 79selectionManager.on('selectionCompleted', selectionChangeCallback); 80try { 81 selectionManager.off('selectionCompleted', selectionChangeCallback); 82} catch (err) { 83 console.error(`Failed to unregister selectionCompleted: ${JSON.stringify(err)}`); 84} 85``` 86 87## createPanel 88 89createPanel(ctx: Context, info: PanelInfo): Promise\<Panel> 90 91Creates a word selection panel. This API uses a promise to return the result. 92 93Only one [main panel](./js-apis-selectionInput-selectionPanel-sys.md) and one [menu panel](./js-apis-selectionInput-selectionPanel-sys.md) can be created for a single word selection application. 94 95**System capability**: SystemCapability.SelectionInput.Selection 96 97**Parameters** 98 99| Name | Type | Mandatory| Description | 100| ------- | ----------- | ---- | ------------------------ | 101| ctx | [Context](../apis-ability-kit/js-apis-inner-application-context.md) | Yes | Context that the current word selection panel depends on.| 102| info | [PanelInfo](./js-apis-selectionInput-selectionPanel-sys.md) | Yes | Information about the word selection panel.| 103 104**Return value** 105| Type | Description | 106| ------- | ------------------------------------------------------------------ | 107| Promise\<[Panel](#panel)> | Promise used to return the word selection panel created. | 108 109**Error codes** 110 111For details about the error codes, see [Word Selection Service Error Codes](errorcode-selection.md). 112 113| ID | Error Message | 114| ---------- | ----------------------------- | 115| 33600001 | Selection service exception. | 116| 33600003 | Invalid operation. The selection app is not valid. | 117 118**Example** 119 120```ts 121import { selectionManager, SelectionExtensionAbility, PanelInfo, PanelType, BusinessError } from '@kit.BasicServicesKit'; 122import { rpc } from '@kit.IPCKit'; 123import { Want } from '@kit.AbilityKit'; 124 125class SelectionAbilityStub extends rpc.RemoteObject { 126 constructor(des: string) { 127 super(des); 128 } 129 onRemoteMessageRequest( 130 code: number, 131 data: rpc.MessageSequence, 132 reply: rpc.MessageSequence, 133 options: rpc.MessageOption 134 ): boolean | Promise<boolean> { 135 return true; 136 } 137} 138 139class ServiceExtAbility extends SelectionExtensionAbility { 140 onConnect(want: Want): rpc.RemoteObject { 141 let panelInfo: PanelInfo = { 142 panelType: PanelType.MENU_PANEL, 143 x: 0, 144 y: 0, 145 width: 500, 146 height: 200 147 } 148 selectionManager.createPanel(this.context, panelInfo) 149 .then((panel: selectionManager.Panel) => { 150 console.info('Succeed in creating panel.'); 151 }).catch((err: BusinessError) => { 152 console.error(`Failed to create panel: ${JSON.stringify(err)}`); 153 }); 154 return new SelectionAbilityStub('remote'); 155 } 156} 157export default ServiceExtAbility; 158``` 159 160## destroyPanel 161 162destroyPanel(panel: Panel): Promise\<void> 163 164Destroys the word selection panel. This API uses a promise to return the result. 165 166**System capability**: SystemCapability.SelectionInput.Selection 167 168**Parameters** 169 170| Name | Type | Mandatory| Description | 171| ---------| ----------- | ---- | ------------------------ | 172| panel | [Panel](#panel) | Yes | Word selection panel to destroy. | 173 174**Return value** 175| Type | Description | 176| ------- | -------------------------------------------------------------------- | 177| Promise\<void> | Promise that returns no value.| 178 179**Error codes** 180 181For details about the error codes, see [Word Selection Service Error Codes](errorcode-selection.md). 182 183| ID | Error Message | 184| ---------- | ----------------------------- | 185| 33600001 | Selection service exception. | 186 187**Example** 188 189```ts 190import { selectionManager, SelectionExtensionAbility, PanelInfo, PanelType, BusinessError } from '@kit.BasicServicesKit'; 191import { rpc } from '@kit.IPCKit'; 192import { Want } from '@kit.AbilityKit'; 193 194class SelectionAbilityStub extends rpc.RemoteObject { 195 constructor(des: string) { 196 super(des); 197 } 198 onRemoteMessageRequest( 199 code: number, 200 data: rpc.MessageSequence, 201 reply: rpc.MessageSequence, 202 options: rpc.MessageOption 203 ): boolean | Promise<boolean> { 204 return true; 205 } 206} 207 208class ServiceExtAbility extends SelectionExtensionAbility { 209 onConnect(want: Want): rpc.RemoteObject { 210 let panelInfo: PanelInfo = { 211 panelType: PanelType.MENU_PANEL, 212 x: 0, 213 y: 0, 214 width: 500, 215 height: 200 216 } 217 let selectionPanel: selectionManager.Panel | undefined = undefined; 218 219 selectionManager.createPanel(this.context, panelInfo) 220 .then((panel: selectionManager.Panel) => { 221 console.info('Succeed in creating panel.'); 222 selectionPanel = panel; 223 try { 224 if (selectionPanel) { 225 selectionManager.destroyPanel(selectionPanel).then(() => { 226 console.info('Succeed in destroying panel.'); 227 }).catch((err: BusinessError) => { 228 console.error(`Failed to destroy panel: ${JSON.stringify(err)}`); 229 }); 230 } 231 } catch (err) { 232 console.error(`Failed to destroy panel: ${JSON.stringify(err)}`); 233 } 234 }).catch((err: BusinessError) => { 235 console.error(`Failed to create panel: ${JSON.stringify(err)}`); 236 }); 237 return new SelectionAbilityStub('remote'); 238 } 239} 240export default ServiceExtAbility; 241``` 242 243## SelectionInfo 244 245Defines the information of a word selection event. 246 247**System capability**: SystemCapability.SelectionInput.Selection 248 249| Name | Type| Read-Only| Optional| Description | 250| --------- | -------- | ---- | ---- | ------------ | 251| text | string | No | No | Selected text.| 252| selectionType | [SelectionType](#selectiontype) | No | No | Operation for selecting words.| 253| startDisplayX | number | No | No | X-coordinate of the screen where the word selection starts, in px.| 254| startDisplayY | number | No | No | Y-coordinate of the screen where the word selection starts, in px.| 255| endDisplayX | number | No | No | X-coordinate of the screen where the word selection ends, in px.| 256| endDisplayY | number | No | No | Y-coordinate of the screen where the word selection ends, in px.| 257| startWindowX | number | No | No | X-coordinate of the window where the word selection starts, in px.| 258| startWindowY | number | No | No | Y-coordinate of the window where the word selection starts, in px.| 259| endWindowX | number | No | No | X-coordinate of the window where the word selection ends, in px.| 260| endWindowY | number | No | No | Y-coordinate of the window where the word selection ends, in px.| 261| displayID | number | No | No | ID of the screen where the window with selected words is located.| 262| windowID | number | No | No | ID of the window where words are selected.| 263| bundleName | string | No | No | Bundle name of the application where words are selected.| 264 265## Panel 266 267In the following API examples, you must first use [createPanel](#createpanel) to obtain a **Panel** instance, and then call the APIs using the obtained instance. 268 269### setUiContent 270 271setUiContent(path: string): Promise\<void> 272 273Sets the page content for the word selection panel. This API uses a promise to return the result. 274 275**System capability**: SystemCapability.SelectionInput.Selection 276 277**Parameters** 278 279| Name | Type | Mandatory| Description | 280| -------- | ---------------------- | ---- | -------- | 281| path | string | Yes | Path of the page content to be set. This path is configured in the **resources/base/profile/main_pages.json** file of the project in the stage model. The FA model is not supported.| 282 283**Return value** 284 285| Type | Description | 286| ------- | ------------------------------ | 287| Promise\<void> | Promise that returns no value. | 288 289**Error codes** 290 291For details about the error codes, see [Word Selection Service Error Codes](errorcode-selection.md). 292 293| ID | Error Message | 294| ---------- | ----------------------------- | 295| 33600001 | Selection service exception. | 296| 33600002 | This selection window has been destroyed. | 297 298**Example** 299 300```ts 301import { selectionManager, BusinessError } from '@kit.BasicServicesKit'; 302 303try { 304 selectionPanel.setUiContent('pages/Index').then(() => { 305 console.info('Succeeded in setting the content.'); 306 }).catch((err: BusinessError) => { 307 console.error(`Failed to setUiContent: ${JSON.stringify(err)}`); 308 }); 309} catch (err) { 310 console.error(`Failed to setUiContent: ${JSON.stringify(err)}`); 311} 312``` 313 314### show 315 316show(): Promise\<void> 317 318Shows the word selection panel. This API uses a promise to return the result. 319 320**System capability**: SystemCapability.SelectionInput.Selection 321 322**Return value** 323 324| Type | Description | 325| ------- | ------------------------------ | 326| Promise\<void> | Promise that returns no value. | 327 328**Error codes** 329 330For details about the error codes, see [Word Selection Service Error Codes](errorcode-selection.md). 331 332| ID | Error Message | 333| ---------- | ----------------------------- | 334| 33600001 | Selection service exception. | 335| 33600002 | This selection window has been destroyed. | 336 337**Example** 338 339```ts 340import { selectionManager, BusinessError } from '@kit.BasicServicesKit'; 341 342selectionPanel.show().then(() => { 343 console.info('Succeeded in showing the panel.'); 344}).catch((err: BusinessError) => { 345 console.error(`Failed to show panel: ${JSON.stringify(err)}`); 346}); 347``` 348 349### hide 350 351hide(): Promise\<void> 352 353Hides the word selection panel. This API uses a promise to return the result. 354 355**System capability**: SystemCapability.SelectionInput.Selection 356 357**Return value** 358 359| Type | Description | 360| ------- | ------------------------------ | 361| Promise\<void> | Promise that returns no value. | 362 363**Error codes** 364 365For details about the error codes, see [Word Selection Service Error Codes](errorcode-selection.md). 366 367| ID | Error Message | 368| ---------- | ----------------------------- | 369| 33600001 | Selection service exception. | 370| 33600002 | This selection window has been destroyed. | 371 372**Example** 373 374```ts 375import { selectionManager, BusinessError } from '@kit.BasicServicesKit'; 376 377selectionPanel.hide().then(() => { 378 console.info('Succeeded in hiding the panel.'); 379}).catch((err: BusinessError) => { 380 console.error(`Failed to hide panel: ${JSON.stringify(err)}`); 381}); 382``` 383 384### startMoving 385 386startMoving(): Promise\<void> 387 388Moves the word selection panel by dragging. This API uses a promise to return the result. 389 390**System capability**: SystemCapability.SelectionInput.Selection 391 392**Return value** 393 394| Type | Description | 395| ------- | ------------------------------ | 396| Promise\<void> | Promise that returns no value. | 397 398**Error codes** 399 400For details about the error codes, see [Word Selection Service Error Codes](errorcode-selection.md). 401 402| ID | Error Message | 403| ---------- | ----------------------------- | 404| 33600001 | Selection service exception. | 405| 33600002 | This selection window has been destroyed. | 406 407**Example** 408 409```ts 410import { selectionManager, BusinessError } from '@kit.BasicServicesKit'; 411 412RelativeContainer() { 413} 414.onTouch((event: TouchEvent) => { 415 if (event.type === TouchType.Down) { 416 if (selectionPanel !== undefined) { 417 selectionPanel.startMoving().then(() => { 418 console.info('Succeeded in startMoving the panel.'); 419 }).catch((err: BusinessError) => { 420 console.error(`Failed to startMoving panel: ${JSON.stringify(err)}`); 421 }); 422 } 423 } 424}) 425``` 426 427### moveTo 428 429moveTo(x: number, y: number): Promise\<void> 430 431Moves the word selection panel to the specified coordinates on the screen. This API uses a promise to return the result. 432 433**System capability**: SystemCapability.SelectionInput.Selection 434 435**Parameters** 436 437| Name | Type | Mandatory| Description | 438| -------- | ---------------------- | ---- | -------- | 439| x | number | Yes |Value of the movement along the X axis, in px.| 440| y | number | Yes |Value of the movement along the Y axis, in px.| 441 442**Return value** 443 444| Type | Description | 445| ------- | ------------------------------ | 446| Promise\<void> | Promise that returns no value. | 447 448**Error codes** 449 450For details about the error codes, see [Word Selection Service Error Codes](errorcode-selection.md). 451 452| ID | Error Message | 453| ---------- | ----------------------------- | 454| 33600001 | Selection service exception. | 455| 33600002 | This selection window has been destroyed. | 456 457**Example** 458 459```ts 460import { selectionManager, BusinessError } from '@kit.BasicServicesKit'; 461 462try { 463 selectionPanel.moveTo(200, 200).then(() => { 464 console.info('Succeeded in moving the panel.'); 465 }).catch((err: BusinessError) => { 466 console.error(`Failed to move panel: ${JSON.stringify(err)}`); 467 }); 468} catch (err) { 469 console.error(`Failed to move panel: ${JSON.stringify(err)}`); 470} 471``` 472 473### on('destroyed') 474 475on(type: 'destroyed', callback: Callback\<void>): void 476 477Registers a callback to listen for the destroy event of the word selection panel. This API uses an asynchronous callback to return the result. 478 479**System capability**: SystemCapability.SelectionInput.Selection 480 481**Parameters** 482 483| Name | Type | Mandatory| Description | 484| -------- | ------------------------------------------- | ---- | ---------------------------------------------- | 485| type | string | Yes | Event type, which is **'destroyed'**.| 486| callback | Callback\<void> | Yes | Callback used to return the result. | 487 488**Example** 489 490```ts 491import { selectionManager, BusinessError } from '@kit.BasicServicesKit'; 492 493try { 494 selectionPanel.on('destroyed', () => { 495 console.info('Panel has destroyed.'); 496 }); 497} catch (err) { 498 console.error(`Failed to register destroyed callback: ${JSON.stringify(err)}`); 499} 500``` 501 502### off('destroyed') 503 504off(type: 'destroyed', callback?: Callback\<void>): void 505 506Unregisters the callback used to listen for the destroy event of the word selection panel. This API uses an asynchronous callback to return the result. 507 508**System capability**: SystemCapability.SelectionInput.Selection 509 510**Parameters** 511 512| Name | Type | Mandatory| Description | 513| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 514| type | string | Yes | Event type, which is **'destroyed'**. | 515| callback | Callback\<void> | No | Callback to unregister. If this parameter is not specified, this API unregisters all callbacks for the specified type.| 516 517**Example** 518 519```ts 520import { selectionManager, BusinessError } from '@kit.BasicServicesKit'; 521 522try { 523 selectionPanel.off('destroyed'); 524} catch (err) { 525 console.error(`Failed to unregister destroyed: ${JSON.stringify(err)}`); 526} 527``` 528 529### on('hidden') 530 531on(type: 'hidden', callback: Callback\<void>): void 532 533Registers a callback to listen for the hide event of the word selection panel. This API uses an asynchronous callback to return the result. 534 535**System capability**: SystemCapability.SelectionInput.Selection 536 537**Parameters** 538 539| Name | Type | Mandatory| Description | 540| -------- | ------------------------------------------- | ---- | ---------------------------------------------- | 541| type | string | Yes | Event type, which is **'hidden'**.| 542| callback | Callback\<void> | Yes | Callback used to return the result. | 543 544**Example** 545 546```ts 547import { selectionManager, BusinessError } from '@kit.BasicServicesKit'; 548 549try { 550 selectionPanel.on('hidden', () => { 551 console.info('Panel has hidden.'); 552 }); 553} catch (err) { 554 console.error(`Failed to register hidden callback: ${JSON.stringify(err)}`); 555} 556``` 557 558### off('hidden') 559 560off(type: 'hidden', callback?: Callback\<void>): void 561 562Unregisters the callback used to listen for the hide event of the word selection panel. This API uses an asynchronous callback to return the result. 563 564**System capability**: SystemCapability.SelectionInput.Selection 565 566**Parameters** 567 568| Name | Type | Mandatory| Description | 569| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 570| type | string | Yes | Event type, which is **'hidden'**. | 571| callback | Callback\<void> | No | Callback to unregister. If this parameter is not specified, this API unregisters all callbacks for the specified type.| 572 573**Example** 574 575```ts 576import { selectionManager, BusinessError } from '@kit.BasicServicesKit'; 577 578try { 579 selectionPanel.off('hidden'); 580} catch (err) { 581 console.error(`Failed to unregister hidden: ${JSON.stringify(err)}`); 582} 583``` 584 585## SelectionType 586 587Enumerates the operations for selecting words. 588 589**System capability**: SystemCapability.SelectionInput.Selection 590 591| Name | Value| Description | 592| ------------ | -- | ------------------ | 593| MOUSE_MOVE | 1 | Move the cursor to select words.| 594| DOUBLE_CLICK | 2 | Double-click to select words.| 595| TRIPLE_CLICK | 3 | Triple-click to select words.| 596