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.6 kB

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