File size: 2,663 Bytes
034b730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from pynput import keyboard
from pynput.keyboard import Key, Events
import pyarrow as pa
from dora import Node


node = Node()
buffer_text = ""
ctrl = False
submitted_text = []
cursor = 0

NODE_TOPIC = ["record", "send", "ask", "change"]

with keyboard.Events() as events:
    while True:
        dora_event = node.next(0.01)
        if (
            dora_event is not None
            and dora_event["type"] == "INPUT"
            and dora_event["id"] == "recording"
        ):
            buffer_text += dora_event["value"][0].as_py()
            node.send_output("buffer", pa.array([buffer_text]))
            continue

        event = events.get(1.0)
        if event is not None and isinstance(event, Events.Press):
            if hasattr(event.key, "char"):
                cursor = 0
                buffer_text += event.key.char
                node.send_output("buffer", pa.array([buffer_text]))
            else:
                if event.key == Key.backspace:
                    buffer_text = buffer_text[:-1]
                    node.send_output("buffer", pa.array([buffer_text]))
                elif event.key == Key.esc:
                    buffer_text = ""
                    node.send_output("buffer", pa.array([buffer_text]))
                elif event.key == Key.enter:
                    node.send_output("submitted", pa.array([buffer_text]))
                    first_word = buffer_text.split(" ")[0]
                    if first_word in NODE_TOPIC:
                        node.send_output(first_word, pa.array([buffer_text]))
                    submitted_text.append(buffer_text)
                    buffer_text = ""
                    node.send_output("buffer", pa.array([buffer_text]))
                elif event.key == Key.ctrl:
                    ctrl = True
                elif event.key == Key.space:
                    buffer_text += " "
                    node.send_output("buffer", pa.array([buffer_text]))
                elif event.key == Key.up:
                    if len(submitted_text) > 0:
                        cursor = max(cursor - 1, -len(submitted_text))
                        buffer_text = submitted_text[cursor]
                        node.send_output("buffer", pa.array([buffer_text]))
                elif event.key == Key.down:
                    if len(submitted_text) > 0:
                        cursor = min(cursor + 1, 0)
                        buffer_text = submitted_text[cursor]
                        node.send_output("buffer", pa.array([buffer_text]))
        elif event is not None and isinstance(event, Events.Release):
            if event.key == Key.ctrl:
                ctrl = False