keyboard.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. def press_node(node: dict[str, Any], inputs: dict[str, Any], context: WorkflowContext) -> dict[str, Any]:
  7. key = str(inputs.get("key", node.get("params", {}).get("key")) or "")
  8. result = windows_automation.keyboard_action("press", key=key)
  9. return {"key": result.get("key"), "action": result.get("action")}
  10. def hotkey_node(node: dict[str, Any], inputs: dict[str, Any], context: WorkflowContext) -> dict[str, Any]:
  11. keys = inputs.get("keys", node.get("params", {}).get("keys")) or []
  12. if isinstance(keys, str):
  13. keys = [part.strip() for part in keys.split("+") if part.strip()]
  14. result = windows_automation.keyboard_action("hotkey", keys=keys)
  15. return {"keys": result.get("keys"), "action": result.get("action")}
  16. def key_down_node(node: dict[str, Any], inputs: dict[str, Any], context: WorkflowContext) -> dict[str, Any]:
  17. key = str(inputs.get("key", node.get("params", {}).get("key")) or "")
  18. result = windows_automation.keyboard_action("key_down", key=key)
  19. return {"key": result.get("key"), "action": result.get("action")}
  20. def key_up_node(node: dict[str, Any], inputs: dict[str, Any], context: WorkflowContext) -> dict[str, Any]:
  21. key = str(inputs.get("key", node.get("params", {}).get("key")) or "")
  22. result = windows_automation.keyboard_action("key_up", key=key)
  23. return {"key": result.get("key"), "action": result.get("action")}
  24. KEY_OUTPUTS = {
  25. "key": {"type": "string", "label": "按键"},
  26. "keys": {"type": "array", "label": "组合键"},
  27. "action": {"type": "string", "label": "动作名称"},
  28. }
  29. for node_type, label, executor in [
  30. ("keyboard.press", "按下按键", press_node),
  31. ("keyboard.key_down", "按键按下", key_down_node),
  32. ("keyboard.key_up", "按键释放", key_up_node),
  33. ]:
  34. register_node(
  35. {
  36. "type": node_type,
  37. "category": "keyboard",
  38. "label": label,
  39. "params": {"key": field_def("text", "按键", required=True)},
  40. "inputs": {"key": field_def("string", "按键")},
  41. "outputs": KEY_OUTPUTS,
  42. "control_ports": control_ports(),
  43. },
  44. executor,
  45. )
  46. register_node(
  47. {
  48. "type": "keyboard.hotkey",
  49. "category": "keyboard",
  50. "label": "组合键",
  51. "params": {"keys": field_def("array", "组合键", ["ctrl", "s"], required=True)},
  52. "inputs": {"keys": field_def("array", "组合键")},
  53. "outputs": KEY_OUTPUTS,
  54. "control_ports": control_ports(),
  55. },
  56. hotkey_node,
  57. )