media.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from __future__ import annotations
  2. from typing import Any
  3. from ... import windows_automation
  4. from ..context import WorkflowContext
  5. from ..registry import control_ports, field_def, register_node
  6. MEDIA_KEY_MAP = {
  7. "play_pause": "space",
  8. "site_fullscreen": "f",
  9. "mute": "m",
  10. "volume_up": "volumeup",
  11. "volume_down": "volumedown",
  12. "system_mute": "volumemute",
  13. "next": "down",
  14. "previous": "up",
  15. "escape": "escape",
  16. }
  17. def media_control_node(node: dict[str, Any], inputs: dict[str, Any], context: WorkflowContext) -> dict[str, Any]:
  18. params = node.get("params", {})
  19. action = str(inputs.get("action", params.get("action")) or "play_pause")
  20. key = MEDIA_KEY_MAP.get(action, "space")
  21. result = windows_automation.keyboard_action("press", key=key)
  22. return {"action": action, "key": key, "keyboard": result}
  23. register_node(
  24. {
  25. "type": "media.control",
  26. "category": "media",
  27. "label": "媒体遥控",
  28. "description": "对当前播放页面执行播放暂停、网页全屏、静音、音量和上下条等客厅遥控动作。",
  29. "params": {
  30. "action": field_def(
  31. "select",
  32. "动作",
  33. "play_pause",
  34. options=[
  35. "play_pause",
  36. "site_fullscreen",
  37. "mute",
  38. "volume_up",
  39. "volume_down",
  40. "system_mute",
  41. "next",
  42. "previous",
  43. "escape",
  44. ],
  45. )
  46. },
  47. "inputs": {"action": field_def("string", "动作")},
  48. "outputs": {
  49. "action": {"type": "string", "label": "动作"},
  50. "key": {"type": "string", "label": "按键"},
  51. "keyboard": {"type": "object", "label": "键盘结果"},
  52. },
  53. "control_ports": control_ports(),
  54. },
  55. media_control_node,
  56. )