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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from pynput import keyboard
  2. from pynput.keyboard import Key, Events
  3. import pyarrow as pa
  4. from dora import Node
  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. else:
  29. if 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