1# AccessibilityExtensionContext (辅助功能扩展上下文) 2 3AccessibilityExtensionContext是AccessibilityExtensionAbility上下文环境,继承自ExtensionContext。 4 5辅助功能扩展上下文模块提供辅助功能扩展的上下文环境的能力,包括允许配置辅助应用关注信息类型、查询节点信息、手势注入等。 6 7> **说明:** 8> 9> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10> 11 12## 使用说明 13 14在使用AccessibilityExtensionContext的功能前,需要通过AccessibilityExtensionAbility子类实例获取AccessibilityExtensionContex的实例。 15 16```ts 17import AccessibilityExtensionAbility from '@ohos.application.AccessibilityExtensionAbility'; 18 19class EntryAbility extends AccessibilityExtensionAbility { 20 onConnect(): void { 21 let axContext = this.context; 22 } 23} 24``` 25 26## FocusDirection 27 28表示查询下一焦点元素的方向。 29 30**系统能力**:以下各项对应的系统能力均为 SystemCapability.BarrierFree.Accessibility.Core 31 32| 名称 | 说明 | 33| -------- | ------- | 34| up | 表示向上查询。 | 35| down | 表示向下查询。 | 36| left | 表示向左查询。 | 37| right | 表示向右查询。 | 38| forward | 表示向前查询。 | 39| backward | 表示向后查询。 | 40 41## FocusType 42 43表示查询焦点元素的类型。 44 45**系统能力**:以下各项对应的系统能力均为 SystemCapability.BarrierFree.Accessibility.Core 46 47| 名称 | 说明 | 48| ------------- | ----------- | 49| accessibility | 表示无障碍的焦点类型。 | 50| normal | 表示普通的焦点类型。 | 51 52## Rect 53 54表示矩形区域。 55 56**系统能力**:以下各项对应的系统能力均为 SystemCapability.BarrierFree.Accessibility.Core 57 58| 名称 | 类型 | 可读 | 可写 | 说明 | 59| ------ | ------ | ---- | ---- | --------- | 60| left | number | 是 | 否 | 矩形区域的左边界。 | 61| top | number | 是 | 否 | 矩形区域的上边界。 | 62| width | number | 是 | 否 | 矩形区域的宽度。 | 63| height | number | 是 | 否 | 矩形区域的高度。 | 64 65## WindowType 66 67表示窗口的类型。 68 69**系统能力**:以下各项对应的系统能力均为 SystemCapability.BarrierFree.Accessibility.Core 70 71| 名称 | 说明 | 72| ----------- | --------- | 73| application | 表示应用窗口类型。 | 74| system | 表示系统窗口类型。 | 75 76## AccessibilityExtensionContext.setTargetBundleName 77 78setTargetBundleName(targetNames: Array\<string>): Promise\<void>; 79 80设置关注的目标包名,使用Promise异步回调。 81 82**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 83 84**参数:** 85 86| 参数名 | 类型 | 必填 | 说明 | 87| ----------- | ------------------- | ---- | -------- | 88| targetNames | Array<string> | 是 | 关注的目标包名。 | 89 90**返回值:** 91 92| 类型 | 说明 | 93| ------------------- | ---------------- | 94| Promise<void> | 无返回结果的Promise对象。 | 95 96**示例:** 97 98```ts 99let targetNames = ['com.ohos.xyz']; 100 101try { 102 axContext.setTargetBundleName(targetNames).then(() => { 103 console.info('set target bundle names success'); 104 }).catch((err: object) => { 105 console.error(`failed to set target bundle names, because ${JSON.stringify(err)}`); 106 }); 107} catch (exception) { 108 console.error(`failed to set target bundle names, because ${JSON.stringify(exception)}`); 109} 110``` 111 112## AccessibilityExtensionContext.setTargetBundleName 113 114setTargetBundleName(targetNames: Array\<string>, callback: AsyncCallback\<void>): void; 115 116设置关注的目标包名,使用callback异步回调。 117 118**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 119 120**参数:** 121 122| 参数名 | 类型 | 必填 | 说明 | 123| ----------- | ------------------------- | ---- | ---------------------------------------- | 124| targetNames | Array<string> | 是 | 关注的目标包名。 | 125| callback | AsyncCallback<void> | 是 | 回调函数,如果设置关注的目标包名失败,则AsyncCallback中err有数据返回。 | 126 127**示例:** 128 129```ts 130import { BusinessError } from '@ohos.base'; 131 132let targetNames = ['com.ohos.xyz']; 133try { 134 axContext.setTargetBundleName(targetNames, (err: BusinessError<void>) => { 135 if (err) { 136 console.error(`failed to set target bundle names, because ${JSON.stringify(err)}`); 137 return; 138 } 139 console.info('set target bundle names success'); 140 }); 141} catch (exception) { 142 console.error(`failed to set target bundle names, because ${JSON.stringify(exception)}`); 143} 144``` 145 146## AccessibilityExtensionContext.getFocusElement 147 148getFocusElement(isAccessibilityFocus?: boolean): Promise\<AccessibilityElement>; 149 150获取焦点元素, 使用Promise异步回调。 151 152**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 153 154**参数:** 155 156| 参数名 | 类型 | 必填 | 说明 | 157| -------------------- | ------- | ---- | ------------------- | 158| isAccessibilityFocus | boolean | 否 | 获取的是否是无障碍焦点元素,默认为否。 | 159 160**返回值:** 161 162| 类型 | 说明 | 163| ----------------------------------- | ---------------------- | 164| Promise<AccessibilityElement> | Promise对象,返回当前对应的焦点元素。 | 165 166**错误码:** 167 168以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 169 170| 错误码ID | 错误信息 | 171| ------- | ---------------------------------------- | 172| 9300003 | Do not have accessibility right for this operation. | 173 174**示例:** 175 176```ts 177import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 178 179let focusElement: AccessibilityElement; 180try { 181 axContext.getFocusElement().then((data: AccessibilityElement) => { 182 focusElement = data; 183 console.log('get focus element success'); 184 }).catch((err: object) => { 185 console.error(`failed to get focus element, because ${JSON.stringify(err)}`); 186 }); 187} catch (exception) { 188 console.error(`failed to get focus element, because ${JSON.stringify(exception)}`); 189} 190``` 191 192## AccessibilityExtensionContext.getFocusElement 193 194getFocusElement(callback: AsyncCallback\<AccessibilityElement>): void; 195 196获取焦点元素, 使用callback异步回调。 197 198**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 199 200**参数:** 201 202| 参数名 | 类型 | 必填 | 说明 | 203| -------- | ---------------------------------------- | ---- | ----------------- | 204| callback | AsyncCallback<AccessibilityElement> | 是 | 回调函数,返回当前对应的焦点元素。 | 205 206**错误码:** 207 208以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 209 210| 错误码ID | 错误信息 | 211| ------- | ---------------------------------------- | 212| 9300003 | Do not have accessibility right for this operation. | 213 214**示例:** 215 216```ts 217import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 218import { BusinessError } from '@ohos.base'; 219 220let focusElement: AccessibilityElement; 221try { 222 axContext.getFocusElement((err: BusinessError<void>, data: AccessibilityElement) => { 223 if (err) { 224 console.error(`failed to get focus element, because ${JSON.stringify(err)}`); 225 return; 226 } 227 focusElement = data; 228 console.info('get focus element success'); 229 }); 230} catch (exception) { 231 console.error(`failed to get focus element, because ${JSON.stringify(exception)}`); 232} 233``` 234 235## AccessibilityExtensionContext.getFocusElement 236 237getFocusElement(isAccessibilityFocus: boolean, callback: AsyncCallback\<AccessibilityElement>): void; 238 239获取焦点元素, 使用callback异步回调。 240 241**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 242 243**参数:** 244 245| 参数名 | 类型 | 必填 | 说明 | 246| -------------------- | ---------------------------------------- | ---- | ----------------- | 247| isAccessibilityFocus | boolean | 是 | 获取的是否是无障碍焦点元素。 | 248| callback | AsyncCallback<AccessibilityElement> | 是 | 回调函数,返回当前对应的焦点元素。 | 249 250**错误码:** 251 252以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 253 254| 错误码ID | 错误信息 | 255| ------- | ---------------------------------------- | 256| 9300003 | Do not have accessibility right for this operation. | 257 258**示例:** 259 260```ts 261import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 262import { BusinessError } from '@ohos.base'; 263 264let focusElement: AccessibilityElement; 265let isAccessibilityFocus = true; 266 267try { 268 axContext.getFocusElement(isAccessibilityFocus, (err: BusinessError<void>, data: AccessibilityElement) => { 269 if (err) { 270 console.error(`failed to get focus element, because ${JSON.stringify(err)}`); 271 return; 272 } 273 focusElement = data; 274 console.info('get focus element success'); 275 }); 276} catch (exception) { 277 console.error(`failed to get focus element, because ${JSON.stringify(exception)}`); 278} 279``` 280## AccessibilityExtensionContext.getWindowRootElement 281 282getWindowRootElement(windowId?: number): Promise\<AccessibilityElement>; 283 284获取指定窗口的根节点元素, 使用Promise异步回调。 285 286**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 287 288**参数:** 289 290| 参数名 | 类型 | 必填 | 说明 | 291| -------- | ------ | ---- | ---------------------- | 292| windowId | number | 否 | 指定窗口的编号,未指定则从当前活跃窗口获取。 | 293 294**返回值:** 295 296| 类型 | 说明 | 297| ----------------------------------- | ---------------------- | 298| Promise<AccessibilityElement> | Promise对象,返回指定窗口的根节点元素。 | 299 300**错误码:** 301 302以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 303 304| 错误码ID | 错误信息 | 305| ------- | ---------------------------------------- | 306| 9300003 | Do not have accessibility right for this operation. | 307 308**示例:** 309 310```ts 311import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 312 313let rootElement: AccessibilityElement; 314try { 315 axContext.getWindowRootElement().then((data: AccessibilityElement) => { 316 rootElement = data; 317 console.log('get root element of the window success'); 318 }).catch((err: object) => { 319 console.error(`failed to get root element of the window, because ${JSON.stringify(err)}`); 320 }); 321} catch (exception) { 322 console.error(`failed to get root element of the window, ${JSON.stringify(exception)}`); 323} 324``` 325 326## AccessibilityExtensionContext.getWindowRootElement 327 328getWindowRootElement(callback: AsyncCallback\<AccessibilityElement>): void; 329 330获取指定窗口的根节点元素, 使用callback异步回调。 331 332**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 333 334**参数:** 335 336| 参数名 | 类型 | 必填 | 说明 | 337| -------- | ---------------------------------------- | ---- | ------------------ | 338| callback | AsyncCallback<AccessibilityElement> | 是 | 回调函数,返回指定窗口的根节点元素。 | 339 340**错误码:** 341 342以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 343 344| 错误码ID | 错误信息 | 345| ------- | ---------------------------------------- | 346| 9300003 | Do not have accessibility right for this operation. | 347 348**示例:** 349 350```ts 351import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 352import { BusinessError } from '@ohos.base'; 353 354let rootElement: AccessibilityElement; 355try { 356 axContext.getWindowRootElement((err: BusinessError<void> 357 , data: AccessibilityElement) => { 358 if (err) { 359 console.error(`failed to get root element of the window, because ${JSON.stringify(err)}`); 360 return; 361 } 362 rootElement = data; 363 console.info('get root element of the window success'); 364 }); 365} catch (exception) { 366 console.error(`failed to get root element of the window, because ${JSON.stringify(exception)}`); 367} 368``` 369 370## AccessibilityExtensionContext.getWindowRootElement 371 372getWindowRootElement(windowId: number, callback: AsyncCallback\<AccessibilityElement>): void; 373 374获取指定窗口的根节点元素, 使用callback异步回调。 375 376**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 377 378**参数:** 379 380| 参数名 | 类型 | 必填 | 说明 | 381| -------- | ---------------------------------------- | ---- | ---------------------- | 382| windowId | number | 是 | 指定窗口的编号,未指定则从当前活跃窗口获取。 | 383| callback | AsyncCallback<AccessibilityElement> | 是 | 回调函数,返回指定窗口的根节点元素。 | 384 385**错误码:** 386 387以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 388 389| 错误码ID | 错误信息 | 390| ------- | ---------------------------------------- | 391| 9300003 | Do not have accessibility right for this operation. | 392 393**示例:** 394 395```ts 396import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 397import { BusinessError } from '@ohos.base'; 398 399let rootElement: AccessibilityElement; 400let windowId = 10; 401 402try { 403 axContext.getWindowRootElement(windowId, (err: BusinessError<void>, data: AccessibilityElement) => { 404 if (err) { 405 console.error(`failed to get root element of the window, because ${JSON.stringify(err)}`); 406 return; 407 } 408 rootElement = data; 409 console.info('get root element of the window success'); 410 }); 411} catch (exception) { 412 console.error(`failed to get root element of the window, because ${JSON.stringify(exception)}`); 413} 414``` 415 416## AccessibilityExtensionContext.getWindows 417 418getWindows(displayId?: number): Promise\<Array\<AccessibilityElement>>; 419 420获取指定屏幕中的所有窗口, 使用Promise异步回调。 421 422**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 423 424**参数:** 425 426| 参数名 | 类型 | 必填 | 说明 | 427| --------- | ------ | ---- | --------------------- | 428| displayId | number | 否 | 指定的屏幕编号,未指定则从默认主屏幕获取。 | 429 430**返回值:** 431 432| 类型 | 说明 | 433| ---------------------------------------- | ---------------------- | 434| Promise<Array<AccessibilityElement>> | Promise对象,返回指定屏幕的所有窗口。 | 435 436**错误码:** 437 438以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 439 440| 错误码ID | 错误信息 | 441| ------- | ---------------------------------------- | 442| 9300003 | Do not have accessibility right for this operation. | 443 444**示例:** 445 446```ts 447import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 448 449let windows: AccessibilityElement[]; 450try { 451 axContext.getWindows().then((data: AccessibilityElement[]) => { 452 windows = data; 453 console.log('get windows success'); 454 }).catch((err: object) => { 455 console.error(`failed to get windows, because ${JSON.stringify(err)}`); 456 }); 457} catch (exception) { 458 console.error(`failed to get windows, because ${JSON.stringify(exception)}`); 459} 460``` 461 462## AccessibilityExtensionContext.getWindows 463 464getWindows(callback: AsyncCallback\<Array\<AccessibilityElement>>): void; 465 466获取指定屏幕中的所有窗口, 使用callback异步回调。 467 468**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 469 470**参数:** 471 472| 参数名 | 类型 | 必填 | 说明 | 473| -------- | ---------------------------------------- | ---- | ----------------- | 474| callback | AsyncCallback<Array<AccessibilityElement>> | 是 | 回调函数,返回指定屏幕的所有窗口。 | 475 476**错误码:** 477 478以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 479 480| 错误码ID | 错误信息 | 481| ------- | ---------------------------------------- | 482| 9300003 | Do not have accessibility right for this operation. | 483 484**示例:** 485 486```ts 487import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 488import { BusinessError } from '@ohos.base'; 489 490let windows: AccessibilityElement[]; 491try { 492 axContext.getWindows((err: BusinessError<void>, data: AccessibilityElement[]) => { 493 if (err) { 494 console.error(`failed to get windows, because ${JSON.stringify(err)}`); 495 return; 496 } 497 windows = data; 498 console.info('get windows success'); 499 }); 500} catch (exception) { 501 console.error(`failed to get windows, because ${JSON.stringify(exception)}`); 502} 503``` 504 505## AccessibilityExtensionContext.getWindows 506 507getWindows(displayId: number, callback: AsyncCallback\<Array\<AccessibilityElement>>): void; 508 509获取指定屏幕中的所有窗口, 使用callback异步回调。 510 511**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 512 513**参数:** 514 515| 参数名 | 类型 | 必填 | 说明 | 516| --------- | ---------------------------------------- | ---- | --------------------- | 517| displayId | number | 是 | 指定的屏幕编号,未指定则从默认主屏幕获取。 | 518| callback | AsyncCallback<Array<AccessibilityElement>> | 是 | 回调函数,返回指定屏幕的所有窗口。 | 519 520**错误码:** 521 522以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 523 524| 错误码ID | 错误信息 | 525| ------- | ---------------------------------------- | 526| 9300003 | Do not have accessibility right for this operation. | 527 528**示例:** 529 530```ts 531import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 532import { BusinessError } from '@ohos.base'; 533 534let windows: AccessibilityElement[]; 535let displayId = 10; 536try { 537 axContext.getWindows(displayId, (err: BusinessError<void>, data: AccessibilityElement[]) => { 538 if (err) { 539 console.error(`failed to get windows, because ${JSON.stringify(err)}`); 540 return; 541 } 542 windows = data; 543 console.info('get windows success'); 544 }); 545} catch (exception) { 546 console.error(`failed to get windows, because ${JSON.stringify(exception)}`); 547} 548``` 549 550## AccessibilityExtensionContext.injectGesture<sup>(deprecated)</sup> 551 552injectGesture(gesturePath: GesturePath): Promise\<void>; 553 554注入手势,使用Promise异步回调。 555 556**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 557 558**参数:** 559 560| 参数名 | 类型 | 必填 | 说明 | 561| ----------- | ---------------------------------------- | ---- | ---------- | 562| gesturePath | [GesturePath](js-apis-accessibility-GesturePath.md#gesturepath) | 是 | 表示手势的路径信息。 | 563 564**返回值:** 565 566| 类型 | 说明 | 567| ------------------- | ---------------- | 568| Promise<void> | 无返回结果的Promise对象。 | 569 570**错误码:** 571 572以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 573 574| 错误码ID | 错误信息 | 575| ------- | ---------------------------------------- | 576| 9300003 | Do not have accessibility right for this operation. | 577 578**示例:** 579 580```ts 581import GesturePath from '@ohos.accessibility.GesturePath'; 582import GesturePoint from '@ohos.accessibility.GesturePoint'; 583 584let gesturePath: GesturePath.GesturePath = new GesturePath.GesturePath(100); 585try { 586 for (let i = 0; i < 10; i++) { 587 let gesturePoint = new GesturePoint.GesturePoint(100, i * 200); 588 gesturePath.points.push(gesturePoint); 589 } 590 axContext.injectGesture(gesturePath).then(() => { 591 console.info('inject gesture success'); 592 }).catch((err: object) => { 593 console.error(`failed to inject gesture, because ${JSON.stringify(err)}`); 594 }); 595} catch (exception) { 596 console.error(`failed to inject gesture, because ${JSON.stringify(exception)}`); 597} 598``` 599## AccessibilityExtensionContext.injectGesture<sup>(deprecated)</sup> 600 601injectGesture(gesturePath: GesturePath, callback: AsyncCallback\<void>): void 602 603注入手势,使用callback异步回调。 604 605**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 606 607**参数:** 608 609| 参数名 | 类型 | 必填 | 说明 | 610| ----------- | ---------------------------------------- | ---- | ------------------- | 611| gesturePath | [GesturePath](js-apis-accessibility-GesturePath.md#gesturepath) | 是 | 表示手势的路径信息。 | 612| callback | AsyncCallback<void> | 是 | 回调函数,表示注入手势执行结果的回调。 | 613 614**错误码:** 615 616以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 617 618| 错误码ID | 错误信息 | 619| ------- | ---------------------------------------- | 620| 9300003 | Do not have accessibility right for this operation. | 621 622**示例:** 623 624```ts 625import GesturePath from '@ohos.accessibility.GesturePath'; 626import GesturePoint from '@ohos.accessibility.GesturePoint'; 627 628let gesturePath: GesturePath.GesturePath = new GesturePath.GesturePath(100); 629try { 630 for (let i = 0; i < 10; i++) { 631 let gesturePoint = new GesturePoint.GesturePoint(100, i * 200); 632 gesturePath.points.push(gesturePoint); 633 } 634 axContext.injectGesture(gesturePath, (err) => { 635 if (err) { 636 console.error(`failed to inject gesture, because ${JSON.stringify(err)}`); 637 return; 638 } 639 console.info('inject gesture success'); 640 }); 641} catch (exception) { 642 console.error(`failed to inject gesture, because ${JSON.stringify(exception)}`); 643} 644``` 645## AccessibilityExtensionContext.injectGestureSync<sup>10+</sup> 646 647injectGestureSync(gesturePath: GesturePath): void 648 649注入手势。 650 651**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 652 653**参数:** 654 655| 参数名 | 类型 | 必填 | 说明 | 656| ----------- | ------------------------------------------------------------ | ---- | -------------------- | 657| gesturePath | [GesturePath](js-apis-accessibility-GesturePath.md#gesturepath) | 是 | 表示手势的路径信息。 | 658 659**错误码:** 660 661以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 662 663| 错误码ID | 错误信息 | 664| -------- | --------------------------------------------------- | 665| 9300003 | Do not have accessibility right for this operation. | 666 667**示例:** 668 669```ts 670import GesturePath from '@ohos.accessibility.GesturePath'; 671import GesturePoint from '@ohos.accessibility.GesturePoint'; 672 673let gesturePath: GesturePath.GesturePath = new GesturePath.GesturePath(100); 674try { 675 for (let i = 0; i < 10; i++) { 676 let gesturePoint = new GesturePoint.GesturePoint(100, i * 200); 677 gesturePath.points.push(gesturePoint); 678 } 679 axContext.injectGestureSync(gesturePath); 680} catch (exception) { 681 console.error(`failed to inject gesture, because ${JSON.stringify(exception)}`); 682} 683``` 684 685## AccessibilityElement<sup>9+</sup> 686 687无障碍节点元素, 在调用AccessibilityElement的方法前,需要先通过[AccessibilityExtensionContext.getFocusElement()](#accessibilityextensioncontextgetfocuselement)或者[AccessibilityExtensionContext.getWindowRootElement()](#accessibilityextensioncontextgetwindowrootelement)获取AccessibilityElement实例。 688 689**系统能力**:以下各项对应的系统能力均为SystemCapability.BarrierFree.Accessibility.Core 690 691### attributeNames 692 693attributeNames\<T extends keyof ElementAttributeValues>(): Promise\<Array\<T>>; 694 695获取节点元素的所有属性名称,使用Promise异步回调。 696 697**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 698 699**返回值:** 700 701| 类型 | 说明 | 702| ----------------------------- | ------------------------ | 703| Promise<Array<T>> | Promise对象,返回节点元素的所有属性名称。 | 704 705**示例:** 706 707```ts 708import { ElementAttributeKeys } from '@ohos.application.AccessibilityExtensionAbility'; 709 710let attributeNames: ElementAttributeKeys; 711rootElement.attributeNames().then((data: ElementAttributeKeys) => { 712 console.log('get attribute names success'); 713 attributeNames = data; 714}).catch((err: object) => { 715 console.log(`failed to get attribute names, because ${JSON.stringify(err)}`); 716}); 717``` 718### attributeNames 719 720attributeNames\<T extends keyof ElementAttributeValues>(callback: AsyncCallback\<Array\<T>>): void; 721 722获取节点元素的所有属性名称,使用callback异步回调。 723 724**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 725 726**参数:** 727 728| 参数名 | 类型 | 必填 | 说明 | 729| -------- | ----------------------------------- | ---- | ------------------- | 730| callback | AsyncCallback<Array<T>> | 是 | 回调函数,返回节点元素的所有属性名称。 | 731 732**示例:** 733 734```ts 735import { ElementAttributeKeys } from '@ohos.application.AccessibilityExtensionAbility'; 736import { BusinessError } from '@ohos.base'; 737 738let attributeNames: ElementAttributeKeys[]; 739rootElement.attributeNames((err: BusinessError<void>, data: ElementAttributeKeys[]) => { 740 if (err) { 741 console.error(`failed to get attribute names, because ${JSON.stringify(err)}`); 742 return; 743 } 744 attributeNames = data; 745 console.info('get attribute names success'); 746}); 747``` 748### attributeValue 749 750attributeValue\<T extends keyof ElementAttributeValues>(attributeName: T): Promise\<ElementAttributeValues[T]>; 751 752根据属性名称获取属性值,使用Promise异步回调。 753 754**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 755 756**参数:** 757 758| 参数名 | 类型 | 必填 | 说明 | 759| ------------- | ---- | ---- | -------- | 760| attributeName | ElementAttributeKeys | 是 | 表示属性的名称。 | 761 762**返回值:** 763 764| 类型 | 说明 | 765| ---------------------------------------- | --------------------------- | 766| Promise<ElementAttributeValues[T]> | Promise对象,返回根据节点属性名称获取的属性值。 | 767 768**错误码:** 769 770以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 771 772| 错误码ID | 错误信息 | 773| ------- | ----------------------------- | 774| 9300004 | This property does not exist. | 775 776**示例:** 777 778```ts 779import { ElementAttributeKeys } from '@ohos.application.AccessibilityExtensionAbility'; 780 781let attributeName: ElementAttributeKeys = 'bundleName'; 782let attributeValue: string; 783try { 784 rootElement.attributeValue(attributeName).then((data: string) => { 785 console.log('get attribute value by name success'); 786 attributeValue = data; 787 }).catch((err: object) => { 788 console.error(`failed to get attribute value, because ${JSON.stringify(err)}`); 789 }); 790} catch (exception) { 791 console.error(`failed to get attribute value, because ${JSON.stringify(exception)}`); 792} 793``` 794### attributeValue 795 796attributeValue\<T extends keyof ElementAttributeValues>(attributeName: T, 797 callback: AsyncCallback\<ElementAttributeValues[T]>): void; 798 799根据属性名称获取属性值,使用callback异步回调。 800 801**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 802 803**参数:** 804 805| 参数名 | 类型 | 必填 | 说明 | 806| ------------- | ---------------------------------------- | ---- | ---------------------- | 807| attributeName | ElementAttributeKeys | 是 | 表示属性的名称。 | 808| callback | AsyncCallback<ElementAttributeValues[T]> | 是 | 回调函数,返回根据节点属性名称获取的属性值。 | 809 810**错误码:** 811 812以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 813 814| 错误码ID | 错误信息 | 815| ------- | ----------------------------- | 816| 9300004 | This property does not exist. | 817 818**示例:** 819 820```ts 821import { ElementAttributeKeys } from '@ohos.application.AccessibilityExtensionAbility'; 822import { BusinessError } from '@ohos.base'; 823 824let attributeName: ElementAttributeKeys = 'bundleName'; 825let attributeValue: string; 826try { 827 rootElement.attributeValue(attributeName, (err: BusinessError<void>, data: string) => { 828 if (err) { 829 console.error(`failed to get attribute value, because ${JSON.stringify(err)}`); 830 return; 831 } 832 attributeValue = data; 833 console.info('get attribute value success'); 834 }); 835} catch (exception) { 836 console.error(`failed to get attribute value, because ${JSON.stringify(exception)}`); 837} 838``` 839### actionNames 840 841actionNames(): Promise\<Array\<string>>; 842 843获取节点元素支持的所有操作名称,使用Promise异步回调。 844 845**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 846 847**返回值:** 848 849| 类型 | 说明 | 850| ---------------------------------- | -------------------------- | 851| Promise<Array<string>> | Promise对象,返回节点元素支持的所有操作名称。 | 852 853**示例:** 854 855```ts 856let actionNames: string[]; 857rootElement.actionNames().then((data: string[]) => { 858 console.log('get action names success'); 859 actionNames = data; 860}).catch((err: object) => { 861 console.error(`failed to get action names because ${JSON.stringify(err)}`); 862}) 863``` 864### actionNames 865 866actionNames(callback: AsyncCallback\<Array\<string>>): void; 867 868获取节点元素支持的所有操作名称,使用callback异步回调。 869 870**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 871 872**参数:** 873 874| 参数名 | 类型 | 必填 | 说明 | 875| -------- | ---------------------------------------- | ---- | --------------------- | 876| callback | AsyncCallback<Array<string>> | 是 | 回调函数,返回节点元素支持的所有操作名称。 | 877 878**示例:** 879 880```ts 881let actionNames: string[]; 882rootElement.actionNames((err: BusinessError<void>, data: string[]) => { 883 if (err) { 884 console.error(`failed to get action names, because ${JSON.stringify(err)}`); 885 return; 886 } 887 actionNames = data; 888 console.info('get action names success'); 889}) 890``` 891### performAction 892 893performAction(actionName: string, parameters?: object): Promise\<void>; 894 895根据操作名称执行某个操作,使用Promise异步回调。 896 897**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 898 899**参数:** 900 901| 参数名 | 类型 | 必填 | 说明 | 902| ----------- | ---------------------------------------- | ---- | -------------- | 903| actionName | string | 是 | 表示属性的名称,取值参考[Action](./js-apis-accessibility.md#action)。 904| parameters | object | 否 | 表示执行操作时所需要的参数;默认为空;当前版本暂不支持。 | 905 906**返回值:** 907 908| 类型 | 说明 | 909| ------------------- | ---------------- | 910| Promise<void> | 无返回结果的Promise对象。 | 911 912**错误码:** 913 914以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 915 916| 错误码ID | 错误信息 | 917| ------- | ----------------------------- | 918| 9300005 | This action is not supported. | 919 920**示例:** 921 922```ts 923let actionName = 'action'; 924try { 925 rootElement.performAction(actionName).then(() => { 926 console.info('perform action success'); 927 }).catch((err: object) => { 928 console.error(`failed to perform action, because ${JSON.stringify(err)}`); 929 }); 930} catch (exception) { 931 console.error(`failed to perform action, because ${JSON.stringify(exception)}`); 932} 933``` 934### performAction 935 936performAction(actionName: string, callback: AsyncCallback\<void>): void; 937 938根据操作名称执行某个操作,使用callback异步回调。 939 940**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 941 942**参数:** 943 944| 参数名 | 类型 | 必填 | 说明 | 945| ----------- | ---------------------------------------- | ---- | -------------- | 946| actionName | string | 是 | 表示属性的名称,取值参考[Action](./js-apis-accessibility.md#action)。 947| callback | AsyncCallback<void> | 是 | 回调函数,表示执行指定操作的回调。| 948 949**错误码:** 950 951以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 952 953| 错误码ID | 错误信息 | 954| ------- | ----------------------------- | 955| 9300005 | This action is not supported. | 956 957**示例:** 958 959```ts 960import { BusinessError } from '@ohos.base'; 961 962let actionName = 'action'; 963try { 964 rootElement.performAction(actionName, (err:BusinessError) => { 965 if (err) { 966 console.error(`failed to perform action, because ${JSON.stringify(err)}`); 967 return; 968 } 969 console.info('perform action success'); 970 }); 971} catch (exception) { 972 console.error(`failed to perform action, because ${JSON.stringify(exception)}`); 973} 974``` 975### performAction 976 977performAction(actionName: string, parameters: object, callback: AsyncCallback\<void>): void; 978 979根据操作名称执行某个操作,使用callback异步回调。 980 981**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 982 983**参数:** 984 985| 参数名 | 类型 | 必填 | 说明 | 986| ---------- | ------------------------- | ---- | ---------------------------------------- | 987| actionName | string | 是 | 表示属性的名称,取值参考[Action](./js-apis-accessibility.md#action)。 | 988| parameters | object | 是 | 表示执行操作时所需要的参数;默认为空;当前版本暂不支持。 | 989| callback | AsyncCallback<void> | 是 | 回调函数,表示执行指定操作的回调。 | 990 991**错误码:** 992 993以下错误码的详细介绍请参见[无障碍子系统错误码](../errorcodes/errorcode-accessibility.md)。 994 995| 错误码ID | 错误信息 | 996| ------- | ----------------------------- | 997| 9300005 | This action is not supported. | 998 999**示例:** 1000 1001```ts 1002import { BusinessError } from '@ohos.base'; 1003 1004let actionName = 'action'; 1005let parameters: object = []; 1006try { 1007 rootElement.performAction(actionName, parameters, (err: BusinessError<void>) => { 1008 if (err) { 1009 console.error(`failed to perform action, because ${JSON.stringify(err)}`); 1010 return; 1011 } 1012 console.info('perform action success'); 1013 }); 1014} catch (exception) { 1015 console.error(`failed to perform action, because ${JSON.stringify(exception)}`); 1016} 1017``` 1018### findElement('content') 1019 1020findElement(type: 'content', condition: string): Promise\<Array\<AccessibilityElement>>; 1021 1022根据节点内容查询所有节点元素,使用Promise异步回调。 1023 1024**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 1025 1026**参数:** 1027 1028| 参数名 | 类型 | 必填 | 说明 | 1029| --------- | ------ | ---- | ----------------------------- | 1030| type | string | 是 | 固定为'content', 表示查找的类型为节点元素内容。 | 1031| condition | string | 是 | 表示查找的条件。 | 1032 1033**返回值:** 1034 1035| 类型 | 说明 | 1036| ---------------------------------------- | ----------------------------- | 1037| Promise<Array<AccessibilityElement>> | Promise对象,返回满足指定查询关键字的所有节点元素。 | 1038 1039**示例:** 1040 1041```ts 1042let condition = 'keyword'; 1043let elements: AccessibilityElement[]; 1044try { 1045 rootElement.findElement('content', condition).then((data: AccessibilityElement[]) => { 1046 elements = data; 1047 console.log('find element success'); 1048 }).catch((err: object) => { 1049 console.error(`failed to find element, because ${JSON.stringify(err)}`); 1050 }); 1051} catch (exception) { 1052 console.error(`failed to find element, because ${JSON.stringify(exception)}`); 1053} 1054``` 1055### findElement('content') 1056 1057findElement(type: 'content', condition: string, callback: AsyncCallback\<Array\<AccessibilityElement>>): void; 1058 1059根据节点内容查询所有节点元素。 1060 1061**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 1062 1063**参数:** 1064 1065| 参数名 | 类型 | 必填 | 说明 | 1066| --------- | ---------------------------------------- | ---- | ---------------------------- | 1067| type | string | 是 | 固定为'content',表示查找的类型为节点元素内容。 | 1068| condition | string | 是 | 表示查找的条件。 | 1069| callback | AsyncCallback<Array<AccessibilityElement>> | 是 | 回调函数,返回满足指定查询关键字的所有节点元素。 | 1070 1071**示例:** 1072 1073```ts 1074import { BusinessError } from '@ohos.base'; 1075 1076let condition = 'keyword'; 1077let elements: AccessibilityElement[]; 1078try { 1079 rootElement.findElement('content', condition, (err: BusinessError<void>, data: AccessibilityElement[]) => { 1080 if (err) { 1081 console.error(`failed to find element, because ${JSON.stringify(err)}`); 1082 return; 1083 } 1084 elements = data; 1085 console.info('find element success'); 1086 }); 1087} catch (exception) { 1088 console.error(`failed to find element, because ${JSON.stringify(exception)}`); 1089} 1090``` 1091### findElement('focusType') 1092 1093findElement(type: 'focusType', condition: FocusType): Promise\<AccessibilityElement>; 1094 1095根据焦点元素类型查询节点元素,使用Promise异步回调。 1096 1097**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 1098 1099**参数:** 1100 1101| 参数名 | 类型 | 必填 | 说明 | 1102| --------- | ----------------------- | ---- | ---------------------------------- | 1103| type | string | 是 | 固定为'focusType', 表示查询的类型为节点的焦点元素类型。 | 1104| condition | [FocusType](#focustype) | 是 | 表示查询焦点元素的类型。 | 1105 1106**返回值:** 1107 1108| 类型 | 说明 | 1109| ----------------------------------- | ------------------------------ | 1110| Promise<AccessibilityElement> | Promise对象,返回满足指定查询焦点元素类型的节点元素。 | 1111 1112**示例:** 1113 1114```ts 1115import { FocusType } from '@ohos.application.AccessibilityExtensionAbility'; 1116 1117let condition: FocusType = 'normal'; 1118let element: AccessibilityElement; 1119try { 1120 rootElement.findElement('focusType', condition).then((data: AccessibilityElement) => { 1121 element = data; 1122 console.log('find element success'); 1123 }).catch((err: object) => { 1124 console.error(`failed to find element, because ${JSON.stringify(err)}`); 1125 }); 1126} catch (exception) { 1127 console.error(`failed to find element, because ${JSON.stringify(exception)}`); 1128} 1129``` 1130### findElement('focusType') 1131 1132findElement(type: 'focusType', condition: FocusType, callback: AsyncCallback\<AccessibilityElement>): void; 1133 1134根据焦点元素类型查询节点元素,使用callback异步回调。 1135 1136**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 1137 1138**参数:** 1139 1140| 参数名 | 类型 | 必填 | 说明 | 1141| --------- | ---------------------------------------- | ---- | ---------------------------------- | 1142| type | string | 是 | 固定为'focusType', 表示查询的类型为节点的焦点元素类型。 | 1143| condition | [FocusType](#focustype) | 是 | 表示查询焦点元素的类型。 | 1144| callback | AsyncCallback<AccessibilityElement> | 是 | 回调函数,返回满足指定查询焦点元素类型的节点元素。 | 1145 1146**示例:** 1147 1148```ts 1149import { FocusType } from '@ohos.application.AccessibilityExtensionAbility'; 1150import { BusinessError } from '@ohos.base'; 1151 1152let condition: FocusType = 'normal'; 1153let element: AccessibilityElement; 1154try { 1155 rootElement.findElement('focusType', condition, (err: BusinessError<void>, data: AccessibilityElement) => { 1156 if (err) { 1157 console.error(`failed to find element, because ${JSON.stringify(err)}`); 1158 return; 1159 } 1160 element = data; 1161 console.info('find element success'); 1162 }); 1163} catch (exception) { 1164 console.error(`failed to find element, because ${JSON.stringify(exception)}`); 1165} 1166``` 1167### findElement('focusDirection') 1168 1169findElement(type: 'focusDirection', condition: FocusDirection): Promise\<AccessibilityElement>; 1170 1171根据下一焦点元素方向查询节点元素,使用Promise异步回调。 1172 1173**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 1174 1175**参数:** 1176 1177| 参数名 | 类型 | 必填 | 说明 | 1178| --------- | --------------------------------- | ---- | ---------------------------------------- | 1179| type | string | 是 | 固定为'focusDirection', 表示查询的类型为节点的下一焦点元素方向。 | 1180| condition | [FocusDirection](#focusdirection) | 是 | 表示查询下一焦点元素的方向。 | 1181 1182**返回值:** 1183 1184| 类型 | 说明 | 1185| ----------------------------------- | -------------------------------- | 1186| Promise<AccessibilityElement> | Promise对象,返回满足指定查询下一焦点元素方向的节点元素。 | 1187 1188**示例:** 1189 1190```ts 1191import { FocusDirection } from '@ohos.application.AccessibilityExtensionAbility'; 1192 1193let condition: FocusDirection = 'up'; 1194let element: AccessibilityElement; 1195try { 1196 rootElement.findElement('focusDirection', condition).then((data: AccessibilityElement) => { 1197 element = data; 1198 console.log('find element success'); 1199 }).catch((err: object) => { 1200 console.error(`failed to find element, because ${JSON.stringify(err)}`); 1201 }); 1202} catch (exception) { 1203 console.error(`failed to find element, because ${JSON.stringify(exception)}`); 1204} 1205``` 1206### findElement('focusDirection') 1207 1208findElement(type: 'focusDirection', condition: FocusDirection, callback: AsyncCallback\<AccessibilityElement>): void; 1209 1210根据下一焦点元素方向查询所有节点元素,使用callback异步回调。 1211 1212**系统能力:** SystemCapability.BarrierFree.Accessibility.Core 1213 1214**参数:** 1215 1216| 参数名 | 类型 | 必填 | 说明 | 1217| --------- | ---------------------------------------- | ---- | ---------------------------------------- | 1218| type | string | 是 | 固定为'focusDirection', 表示查询的类型为节点的下一焦点元素方向。 | 1219| condition | [FocusDirection](#focusdirection) | 是 | 表示下一查询焦点元素的方向。 | 1220| callback | AsyncCallback<AccessibilityElement> | 是 | 回调函数,返回满足指定查询下一焦点元素方向的节点元素。 | 1221 1222**示例:** 1223 1224```ts 1225import { FocusDirection } from '@ohos.application.AccessibilityExtensionAbility'; 1226import { BusinessError } from '@ohos.base'; 1227 1228let condition: FocusDirection = 'up'; 1229let elements: AccessibilityElement; 1230try { 1231 rootElement.findElement('focusDirection', condition, (err: BusinessError<void>, data: AccessibilityElement) => { 1232 if (err) { 1233 console.error(`failed to find element, because ${JSON.stringify(err)}`); 1234 return; 1235 } 1236 elements = data; 1237 console.info('find element success'); 1238 }); 1239} catch (exception) { 1240 console.error(`failed to find element, because ${JSON.stringify(exception)}`); 1241} 1242``` 1243