Send a keyevent to the device.
(self, keycode: str)
| 256 | return ActionResult(True, False, message="User interaction required") |
| 257 | |
| 258 | def _send_keyevent(self, keycode: str) -> None: |
| 259 | """Send a keyevent to the device.""" |
| 260 | from phone_agent.device_factory import DeviceType, get_device_factory |
| 261 | from phone_agent.hdc.connection import _run_hdc_command |
| 262 | |
| 263 | device_factory = get_device_factory() |
| 264 | |
| 265 | # Handle HDC devices with HarmonyOS-specific keyEvent command |
| 266 | if device_factory.device_type == DeviceType.HDC: |
| 267 | hdc_prefix = ["hdc", "-t", self.device_id] if self.device_id else ["hdc"] |
| 268 | |
| 269 | # Map common keycodes to HarmonyOS keyEvent codes |
| 270 | # KEYCODE_ENTER (66) -> 2054 (HarmonyOS Enter key code) |
| 271 | if keycode == "KEYCODE_ENTER" or keycode == "66": |
| 272 | _run_hdc_command( |
| 273 | hdc_prefix + ["shell", "uitest", "uiInput", "keyEvent", "2054"], |
| 274 | capture_output=True, |
| 275 | text=True, |
| 276 | ) |
| 277 | else: |
| 278 | # For other keys, try to use the numeric code directly |
| 279 | # If keycode is a string like "KEYCODE_ENTER", convert it |
| 280 | try: |
| 281 | # Try to extract numeric code from string or use as-is |
| 282 | if keycode.startswith("KEYCODE_"): |
| 283 | # For now, only handle ENTER, other keys may need mapping |
| 284 | if "ENTER" in keycode: |
| 285 | _run_hdc_command( |
| 286 | hdc_prefix + ["shell", "uitest", "uiInput", "keyEvent", "2054"], |
| 287 | capture_output=True, |
| 288 | text=True, |
| 289 | ) |
| 290 | else: |
| 291 | # Fallback to ADB-style command for unsupported keys |
| 292 | subprocess.run( |
| 293 | hdc_prefix + ["shell", "input", "keyevent", keycode], |
| 294 | capture_output=True, |
| 295 | text=True, |
| 296 | ) |
| 297 | else: |
| 298 | # Assume it's a numeric code |
| 299 | _run_hdc_command( |
| 300 | hdc_prefix + ["shell", "uitest", "uiInput", "keyEvent", str(keycode)], |
| 301 | capture_output=True, |
| 302 | text=True, |
| 303 | ) |
| 304 | except Exception: |
| 305 | # Fallback to ADB-style command |
| 306 | subprocess.run( |
| 307 | hdc_prefix + ["shell", "input", "keyevent", keycode], |
| 308 | capture_output=True, |
| 309 | text=True, |
| 310 | ) |
| 311 | else: |
| 312 | # ADB devices use standard input keyevent command |
| 313 | cmd_prefix = ["adb", "-s", self.device_id] if self.device_id else ["adb"] |
| 314 | subprocess.run( |
| 315 | cmd_prefix + ["shell", "input", "keyevent", keycode], |
nothing calls this directly
no test coverage detected