1# 组件标识 2 3id为组件的唯一标识,在整个应用内唯一。本模块提供组件标识相关接口,可以获取指定id组件的属性,也提供向指定id组件发送事件的功能。 4 5> **说明:** 6> 7> 从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## 属性 11 12| 名称 | 类型 | 说明 | 13| -----| -------- | ----------------------------- | 14| id | string | 组件的唯一标识,唯一性由使用者保证。<br>默认值:''<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。 | 15 16 17## 接口 18 19 20### getInspectorByKey<sup>9+</sup> 21 22getInspectorByKey(id: string): string 23 24获取指定id的组件的所有属性,不包括子组件信息。 25 26此接口仅用于对应用的测试。由于耗时长,不建议使用。 27 28**参数:** 29 30| 参数名 | 类型 | 必填 | 说明 | 31| ---- | -------- | ---- | -------------| 32| id | string | 是 | 要获取属性的组件id。 | 33 34**返回值:** 35 36| 类型 | 说明 | 37| -------| -------------- | 38| string | 组件属性列表的JSON字符串。<br />**说明**:<br /> 字符串信息包含组件的tag、id、位置信息(相对于窗口左上角的坐标)以及用于测试检查的组件所包含的相关属性信息。 | 39 40### getInspectorTree<sup>9+</sup> 41 42getInspectorTree(): Object 43 44获取组件树及组件属性。 45 46此接口仅用于对应用的测试。由于耗时长,不建议使用。 47 48**返回值:** 49 50| 类型 | 说明 | 51| ------ | --------------------------- | 52| Object | 组件树及组件属性列表的JSON对象。 | 53 54### sendEventByKey<sup>9+</sup> 55 56sendEventByKey(id: string, action: number, params: string): boolean 57 58给指定id的组件发送事件。 59 60此接口仅用于对应用的测试。由于耗时长,不建议使用。 61 62**参数:** 63 64| 参数名 | 类型 | 必填 | 说明 | 65| ------ | -------| ---- | -------------------------- | 66| id | string | 是 | 要触发事件的组件的id。 | 67| action | number | 是 | 要触发的事件类型,目前支持取值:<br/>- 点击事件Click: 10<br/>- 长按事件LongClick: 11。 | 68| params | string | 是 | 事件参数,无参数传空字符串 ""。 | 69 70**返回值:** 71 72| 类型 | 说明 | 73| -------- | --------------------------| 74| boolean | 找不到指定id的组件时返回false,其余情况返回true。 | 75 76### sendTouchEvent<sup>9+</sup> 77 78sendTouchEvent(event: TouchObject): boolean 79 80发送触摸事件。 81 82此接口仅用于对应用的测试。由于耗时长,不建议使用。 83 84**参数:** 85 86| 参数名 | 类型 | 必填 | 说明 | 87| ----- | ----------- | ---- | ------------------------------------------------------------ | 88| event | [TouchObject](ts-universal-events-touch.md#touchobject对象说明) | 是 | 触发触摸事件的位置,event参数见[TouchEvent](ts-universal-events-touch.md#touchevent对象说明)中TouchObject的介绍。 | 89 90**返回值:** 91 92| 类型 | 说明 | 93| ------- | ---------------------------| 94| boolean | 事件发送失败时返回false,其余情况返回true。 | 95 96### sendKeyEvent<sup>9+</sup> 97 98sendKeyEvent(event: KeyEvent): boolean 99 100发送按键事件。 101 102此接口仅用于对应用的测试。由于耗时长,不建议使用。 103 104**参数:** 105 106| 参数名 | 类型 | 必填 | 说明 | 107| ----- | -------- | ---- | ------------------------------------------------------------ | 108| event | [KeyEvent](ts-universal-events-key.md#keyevent对象说明) | 是 | 按键事件,event参数见[KeyEvent](ts-universal-events-key.md#keyevent对象说明)介绍。 | 109 110**返回值:** 111 112| 类型 | 说明 | 113| ------- | ------------------------------| 114| boolean | 事件发送失败时时返回false,其余情况返回true。 | 115 116### sendMouseEvent<sup>9+</sup> 117 118sendMouseEvent(event: MouseEvent): boolean 119 120发送鼠标事件。 121 122此接口仅用于对应用的测试。由于耗时长,不建议使用。 123 124**参数:** 125 126| 参数名 | 类型 | 必填 | 说明 | 127| ----- | ---------- | ---- | --------------------------------------- | 128| event | [MouseEvent](ts-universal-mouse-key.md#mouseevent对象说明) | 是 | 鼠标事件,event参数见[MouseEvent](ts-universal-mouse-key.md#mouseevent对象说明)介绍。 | 129 130**返回值:** 131 132| 类型 | 说明 | 133| ------- | ---------------------------------- | 134| boolean | 事件发送失败时返回false,其余情况返回true。 | 135 136## 示例 137 138```ts 139// xxx.ets 140import { IntentionCode } from '@ohos.multimodalInput.intentionCode' 141 142class Utils { 143 static rect_left: number 144 static rect_top: number 145 static rect_right: number 146 static rect_bottom: number 147 static rect_value: Record<string, number> 148 149 //获取组件所占矩形区域坐标 150 static getComponentRect(key:string):Record<string, number> { 151 let strJson = getInspectorByKey(key) 152 let obj:Record<string, string> = JSON.parse(strJson) 153 console.info("[getInspectorByKey] current component obj is: " + JSON.stringify(obj)) 154 let rectInfo:string[] = JSON.parse('[' + obj.$rect + ']') 155 console.info("[getInspectorByKey] rectInfo is: " + rectInfo) 156 Utils.rect_left = JSON.parse('[' + rectInfo[0] + ']')[0] 157 Utils.rect_top = JSON.parse('[' + rectInfo[0] + ']')[1] 158 Utils.rect_right = JSON.parse('[' + rectInfo[1] + ']')[0] 159 Utils.rect_bottom = JSON.parse('[' + rectInfo[1] + ']')[1] 160 return Utils.rect_value = { 161 "left": Utils.rect_left, "top": Utils.rect_top, "right": Utils.rect_right, "bottom": Utils.rect_bottom 162 } 163 } 164} 165 166@Entry 167@Component 168struct IdExample { 169 @State text: string = '' 170 171 build() { 172 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 173 174 Button() { 175 Text('onKeyTab').fontSize(25).fontWeight(FontWeight.Bold) 176 }.margin({ top: 20 }).backgroundColor('#0D9FFB') 177 .onKeyEvent(() => { 178 this.text = "onKeyTab" 179 }) 180 181 Button() { 182 Text('click to start').fontSize(25).fontWeight(FontWeight.Bold) 183 }.margin({ top: 20 }) 184 .onClick(() => { 185 console.info(getInspectorByKey("click")) 186 console.info(JSON.stringify(getInspectorTree())) 187 this.text = "Button 'click to start' is clicked" 188 setTimeout(() => { 189 sendEventByKey("longClick", 11, "") // 向id为"longClick"的组件发送长按事件 190 }, 2000) 191 }).id('click') 192 193 Button() { 194 Text('longClick').fontSize(25).fontWeight(FontWeight.Bold) 195 }.margin({ top: 20 }).backgroundColor('#0D9FFB') 196 .gesture( 197 LongPressGesture().onActionEnd(() => { 198 console.info('long clicked') 199 this.text = "Button 'longClick' is longclicked" 200 setTimeout(() => { 201 let rect = Utils.getComponentRect('onTouch') // 获取id为"onTouch"组件的矩形区域坐标 202 let touchPoint: TouchObject = { 203 id: 1, 204 type: TouchType.Down, 205 x: rect.left + (rect.right - rect.left) / 2, // 相对于组件左上角的水平方向坐标 206 y: rect.top + (rect.bottom - rect.top) / 2, // 相对于组件左上角的垂直方向坐标 207 screenX: rect.left + (rect.right - rect.left) / 2, // 相对于应用窗口左上角的水平方向坐标,API10已废弃,采用windowX替代 208 screenY: rect.left + (rect.right - rect.left) / 2, // 相对于应用窗口左上角的垂直方向坐标,API10已废弃,采用windowY替代 209 windowX: rect.left + (rect.right - rect.left) / 2, // 相对于应用窗口左上角的水平方向坐标 210 windowY: rect.left + (rect.right - rect.left) / 2, // 相对于应用窗口左上角的垂直方向坐标 211 displayX: rect.left + (rect.right - rect.left) / 2, // 相对于设备屏幕左上角的水平方向坐标 212 displayY: rect.left + (rect.right - rect.left) / 2, // 相对于设备屏幕左上角的垂直方向坐标 213 } 214 sendTouchEvent(touchPoint) // 发送触摸事件 215 touchPoint.type = TouchType.Up 216 sendTouchEvent(touchPoint) // 发送触摸事件 217 }, 2000) 218 })).id('longClick') 219 220 Button() { 221 Text('onTouch').fontSize(25).fontWeight(FontWeight.Bold) 222 }.type(ButtonType.Capsule).margin({ top: 20 }) 223 .onClick(() => { 224 console.info('onTouch is clicked') 225 this.text = "Button 'onTouch' is clicked" 226 setTimeout(() => { 227 let rect = Utils.getComponentRect('onMouse') // 获取id为"onMouse"组件的矩形区域坐标 228 let mouseEvent: MouseEvent = { 229 button: MouseButton.Left, 230 action: MouseAction.Press, 231 x: rect.left + (rect.right - rect.left) / 2, // 相对于组件左上角的水平方向坐标 232 y: rect.top + (rect.bottom - rect.top) / 2, // 相对于组件左上角的垂直方向坐标 233 screenX: rect.left + (rect.right - rect.left) / 2, // 相对于应用窗口左上角的水平方向坐标,API10已废弃,采用windowX替代 234 screenY: rect.left + (rect.right - rect.left) / 2, // 相对于应用窗口左上角的垂直方向坐标,API10已废弃,采用windowY替代 235 windowX: rect.left + (rect.right - rect.left) / 2, // 相对于应用窗口左上角的水平方向坐标 236 windowY: rect.left + (rect.right - rect.left) / 2, // 相对于应用窗口左上角的垂直方向坐标 237 displayX: rect.left + (rect.right - rect.left) / 2, // 相对于设备屏幕左上角的水平方向坐标 238 displayY: rect.left + (rect.right - rect.left) / 2, // 相对于设备屏幕左上角的垂直方向坐标 239 stopPropagation: () => { 240 }, 241 timestamp: 1, 242 target: { 243 area: { 244 width: 1, 245 height: 1, 246 position: { 247 x: 1, 248 y: 1 249 }, 250 globalPosition: { 251 x: 1, 252 y: 1 253 } 254 } 255 }, 256 source: SourceType.Mouse, 257 pressure: 1, 258 tiltX: 1, 259 tiltY: 1, 260 sourceTool: SourceTool.Unknown 261 } 262 sendMouseEvent(mouseEvent) // 发送鼠标事件 263 }, 2000) 264 }).id('onTouch') 265 266 Button() { 267 Text('onMouse').fontSize(25).fontWeight(FontWeight.Bold) 268 }.margin({ top: 20 }).backgroundColor('#0D9FFB') 269 .onMouse(() => { 270 console.info('onMouse') 271 this.text = "Button 'onMouse' in onMouse" 272 setTimeout(() => { 273 let keyEvent: KeyEvent = { 274 type: KeyType.Down, 275 keyCode: 2049, 276 keyText: 'tab', 277 keySource: 4, 278 deviceId: 0, 279 metaKey: 0, 280 timestamp: 0, 281 stopPropagation: () => { 282 }, 283 intentionCode: IntentionCode.INTENTION_DOWN 284 } 285 sendKeyEvent(keyEvent) // 发送按键事件 286 }, 2000) 287 }).id('onMouse') 288 289 Text(this.text).fontSize(25).padding(15) 290 } 291 .width('100%').height('100%') 292 } 293} 294``` 295