You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

keyboard_op.py 2.5 kB

10 months ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import pyarrow as pa
  2. from dora import Node
  3. from pynput import keyboard
  4. from pynput.keyboard import Events, Key
  5. node = Node()
  6. buffer_text = ""
  7. ctrl = False
  8. submitted_text = []
  9. cursor = 0
  10. NODE_TOPIC = ["record", "send", "ask", "change"]
  11. with keyboard.Events() as events:
  12. while True:
  13. dora_event = node.next(0.01)
  14. if (
  15. dora_event is not None
  16. and dora_event["type"] == "INPUT"
  17. and dora_event["id"] == "recording"
  18. ):
  19. buffer_text += dora_event["value"][0].as_py()
  20. node.send_output("buffer", pa.array([buffer_text]))
  21. continue
  22. event = events.get(1.0)
  23. if event is not None and isinstance(event, Events.Press):
  24. if hasattr(event.key, "char"):
  25. cursor = 0
  26. buffer_text += event.key.char
  27. node.send_output("buffer", pa.array([buffer_text]))
  28. elif event.key == Key.backspace:
  29. buffer_text = buffer_text[:-1]
  30. node.send_output("buffer", pa.array([buffer_text]))
  31. elif event.key == Key.esc:
  32. buffer_text = ""
  33. node.send_output("buffer", pa.array([buffer_text]))
  34. elif event.key == Key.enter:
  35. node.send_output("submitted", pa.array([buffer_text]))
  36. first_word = buffer_text.split(" ")[0]
  37. if first_word in NODE_TOPIC:
  38. node.send_output(first_word, pa.array([buffer_text]))
  39. submitted_text.append(buffer_text)
  40. buffer_text = ""
  41. node.send_output("buffer", pa.array([buffer_text]))
  42. elif event.key == Key.ctrl:
  43. ctrl = True
  44. elif event.key == Key.space:
  45. buffer_text += " "
  46. node.send_output("buffer", pa.array([buffer_text]))
  47. elif event.key == Key.up:
  48. if len(submitted_text) > 0:
  49. cursor = max(cursor - 1, -len(submitted_text))
  50. buffer_text = submitted_text[cursor]
  51. node.send_output("buffer", pa.array([buffer_text]))
  52. elif event.key == Key.down:
  53. if len(submitted_text) > 0:
  54. cursor = min(cursor + 1, 0)
  55. buffer_text = submitted_text[cursor]
  56. node.send_output("buffer", pa.array([buffer_text]))
  57. elif event is not None and isinstance(event, Events.Release):
  58. if event.key == Key.ctrl:
  59. ctrl = False