-
Notifications
You must be signed in to change notification settings - Fork 38
/
input.go
244 lines (219 loc) · 7.08 KB
/
input.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package terminal
import (
"runtime"
"unicode/utf8"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver/desktop"
)
// TypedRune is called when the user types a visible character
func (t *Terminal) TypedRune(r rune) {
b := make([]byte, utf8.UTFMax)
size := utf8.EncodeRune(b, r)
_, _ = t.in.Write(b[:size])
}
// TypedKey will be called if a non-printable keyboard event occurs
func (t *Terminal) TypedKey(e *fyne.KeyEvent) {
if t.keyboardState.shiftPressed {
t.keyTypedWithShift(e)
return
}
switch e.Name {
case fyne.KeyReturn:
_, _ = t.in.Write([]byte{'\r'})
case fyne.KeyEnter:
if t.newLineMode {
_, _ = t.in.Write([]byte{'\r'})
return
}
_, _ = t.in.Write([]byte{'\n'})
case fyne.KeyTab:
_, _ = t.in.Write([]byte{'\t'})
case fyne.KeyF1:
_, _ = t.in.Write([]byte{asciiEscape, 'O', 'P'})
case fyne.KeyF2:
_, _ = t.in.Write([]byte{asciiEscape, 'O', 'Q'})
case fyne.KeyF3:
_, _ = t.in.Write([]byte{asciiEscape, 'O', 'R'})
case fyne.KeyF4:
_, _ = t.in.Write([]byte{asciiEscape, 'O', 'S'})
case fyne.KeyF5:
_, _ = t.in.Write([]byte{asciiEscape, '[', '1', '5', '~'})
case fyne.KeyF6:
_, _ = t.in.Write([]byte{asciiEscape, '[', '1', '7', '~'})
case fyne.KeyF7:
_, _ = t.in.Write([]byte{asciiEscape, '[', '1', '8', '~'})
case fyne.KeyF8:
_, _ = t.in.Write([]byte{asciiEscape, '[', '1', '9', '~'})
case fyne.KeyF9:
_, _ = t.in.Write([]byte{asciiEscape, '[', '2', '0', '~'})
case fyne.KeyF10:
_, _ = t.in.Write([]byte{asciiEscape, '[', '2', '1', '~'})
case fyne.KeyF11:
_, _ = t.in.Write([]byte{asciiEscape, '[', '2', '3', '~'})
case fyne.KeyF12:
_, _ = t.in.Write([]byte{asciiEscape, '[', '2', '4', '~'})
case fyne.KeyEscape:
_, _ = t.in.Write([]byte{asciiEscape})
case fyne.KeyBackspace:
_, _ = t.in.Write([]byte{asciiBackspace})
case fyne.KeyDelete:
_, _ = t.in.Write([]byte{asciiEscape, '[', '3', '~'})
case fyne.KeyUp, fyne.KeyDown, fyne.KeyLeft, fyne.KeyRight:
t.typeCursorKey(e.Name)
case fyne.KeyPageUp:
_, _ = t.in.Write([]byte{asciiEscape, '[', '5', '~'})
case fyne.KeyPageDown:
_, _ = t.in.Write([]byte{asciiEscape, '[', '6', '~'})
case fyne.KeyHome:
_, _ = t.in.Write([]byte{asciiEscape, 'O', 'H'})
case fyne.KeyInsert:
_, _ = t.in.Write([]byte{asciiEscape, '[', '2', '~'})
case fyne.KeyEnd:
_, _ = t.in.Write([]byte{asciiEscape, 'O', 'F'})
}
}
func (t *Terminal) keyTypedWithShift(e *fyne.KeyEvent) {
switch e.Name {
case fyne.KeyF1:
_, _ = t.in.Write([]byte{asciiEscape, '[', '2', '5', '~'})
case fyne.KeyF2:
_, _ = t.in.Write([]byte{asciiEscape, '[', '2', '6', '~'})
case fyne.KeyF3:
_, _ = t.in.Write([]byte{asciiEscape, 'O', 'R', ';', '2', '~'})
case fyne.KeyF4:
_, _ = t.in.Write([]byte{asciiEscape, '[', '1', ';', '2', 'S'})
case fyne.KeyF5:
_, _ = t.in.Write([]byte{asciiEscape, '[', '1', '5', ';', '2', '~'})
case fyne.KeyF6:
_, _ = t.in.Write([]byte{asciiEscape, '[', '1', '7', ';', '2', '~'})
case fyne.KeyF7:
_, _ = t.in.Write([]byte{asciiEscape, '[', '1', '8', ';', '2', '~'})
case fyne.KeyF8:
_, _ = t.in.Write([]byte{asciiEscape, '[', '1', '9', ';', '2', '~'})
case fyne.KeyF9:
_, _ = t.in.Write([]byte{asciiEscape, '[', '2', '0', ';', '2', '~'})
case fyne.KeyF10:
_, _ = t.in.Write([]byte{asciiEscape, '[', '2', '1', ';', '2', '~'})
case fyne.KeyF11:
_, _ = t.in.Write([]byte{asciiEscape, '[', '2', '3', ';', '2', '~'})
case fyne.KeyF12:
_, _ = t.in.Write([]byte{asciiEscape, '[', '2', '4', ';', '2', '~'})
case fyne.KeyPageUp:
_, _ = t.in.Write([]byte{asciiEscape, '[', '5', ';', '2', '~'})
case fyne.KeyPageDown:
_, _ = t.in.Write([]byte{asciiEscape, '[', '6', ';', '2', '~'})
case fyne.KeyHome:
_, _ = t.in.Write([]byte{asciiEscape, '[', '1', ';', '2', 'H'})
case fyne.KeyInsert:
_, _ = t.in.Write([]byte{asciiEscape, '[', '2', ';', '2', '~'})
case fyne.KeyDelete:
_, _ = t.in.Write([]byte{asciiEscape, '[', '3', ';', '2', '~'})
case fyne.KeyEnd:
_, _ = t.in.Write([]byte{asciiEscape, '[', '1', ';', '2', 'F'})
case fyne.KeyUp:
_, _ = t.in.Write([]byte{asciiEscape, '[', 'A', ';', '2'})
case fyne.KeyDown:
_, _ = t.in.Write([]byte{asciiEscape, '[', 'B', ';', '2'})
case fyne.KeyLeft:
_, _ = t.in.Write([]byte{asciiEscape, '[', 'D', ';', '2'})
case fyne.KeyRight:
_, _ = t.in.Write([]byte{asciiEscape, '[', 'C', ';', '2'})
}
}
func (t *Terminal) trackKeyboardState(down bool, e *fyne.KeyEvent) {
switch e.Name {
case desktop.KeyShiftLeft:
t.keyboardState.shiftPressed = down
case desktop.KeyAltLeft:
t.keyboardState.altPressed = down
case desktop.KeyControlLeft:
t.keyboardState.ctrlPressed = down
case desktop.KeyShiftRight:
t.keyboardState.shiftPressed = down
case desktop.KeyAltRight:
t.keyboardState.altPressed = down
case desktop.KeyControlRight:
t.keyboardState.ctrlPressed = down
}
}
// KeyDown is called when we get a down key event
func (t *Terminal) KeyDown(e *fyne.KeyEvent) {
t.trackKeyboardState(true, e)
}
// KeyUp is called when we get an up key event
func (t *Terminal) KeyUp(e *fyne.KeyEvent) {
t.trackKeyboardState(false, e)
}
// FocusGained notifies the terminal that it has focus
func (t *Terminal) FocusGained() {
t.focused = true
t.Refresh()
}
// TypedShortcut handles key combinations, we pass them on to the tty.
func (t *Terminal) TypedShortcut(s fyne.Shortcut) {
if ds, ok := s.(*desktop.CustomShortcut); ok {
t.ShortcutHandler.TypedShortcut(s) // it's not clear how we can check if this consumed the event
// handle CTRL+A to CTRL+_ and everything inbetween
if ds.Modifier == fyne.KeyModifierControl {
char := ds.KeyName[0]
off := char - 'A' + 1
switch {
case ds.Key() == fyne.KeySpace:
fallthrough
case ds.Key() == "@":
off = 0
fallthrough
case char >= 'A' && char <= '_':
_, _ = t.in.Write([]byte{off})
}
}
return
}
if runtime.GOOS == "darwin" {
// do the default thing for macOS as they separete ctrl/cmd
t.ShortcutHandler.TypedShortcut(s)
} else {
// we need to override the default ctrl-X/C/V/A for non-mac and do it ourselves
if _, ok := s.(*fyne.ShortcutCut); ok {
_, _ = t.in.Write([]byte{0x18})
} else if _, ok := s.(*fyne.ShortcutCopy); ok {
_, _ = t.in.Write([]byte{0x3})
} else if _, ok := s.(*fyne.ShortcutPaste); ok {
_, _ = t.in.Write([]byte{0x16})
} else if _, ok := s.(*fyne.ShortcutSelectAll); ok {
_, _ = t.in.Write([]byte{0x1})
}
}
}
// FocusLost tells the terminal it no longer has focus
func (t *Terminal) FocusLost() {
t.focused = false
t.Refresh()
}
// Focused is used to determine if this terminal currently has focus
func (t *Terminal) Focused() bool {
return t.focused
}
func (t *Terminal) typeCursorKey(key fyne.KeyName) {
cursorPrefix := byte('[')
if t.bufferMode {
cursorPrefix = 'O'
}
switch key {
case fyne.KeyUp:
_, _ = t.in.Write([]byte{asciiEscape, cursorPrefix, 'A'})
case fyne.KeyDown:
_, _ = t.in.Write([]byte{asciiEscape, cursorPrefix, 'B'})
case fyne.KeyLeft:
_, _ = t.in.Write([]byte{asciiEscape, cursorPrefix, 'D'})
case fyne.KeyRight:
_, _ = t.in.Write([]byte{asciiEscape, cursorPrefix, 'C'})
}
}
type discardWriter struct{}
func (d discardWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}
func (d discardWriter) Close() error {
return nil
}