-
Notifications
You must be signed in to change notification settings - Fork 816
/
key_windows.go
351 lines (328 loc) Β· 8.17 KB
/
key_windows.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//go:build windows
// +build windows
package tea
import (
"context"
"fmt"
"io"
"github.com/erikgeiser/coninput"
localereader "github.com/mattn/go-localereader"
"golang.org/x/sys/windows"
)
func readInputs(ctx context.Context, msgs chan<- Msg, input io.Reader) error {
if coninReader, ok := input.(*conInputReader); ok {
return readConInputs(ctx, msgs, coninReader.conin)
}
return readAnsiInputs(ctx, msgs, localereader.NewReader(input))
}
func readConInputs(ctx context.Context, msgsch chan<- Msg, con windows.Handle) error {
var ps coninput.ButtonState // keep track of previous mouse state
var ws coninput.WindowBufferSizeEventRecord // keep track of the last window size event
for {
events, err := coninput.ReadNConsoleInputs(con, 16)
if err != nil {
return fmt.Errorf("read coninput events: %w", err)
}
for _, event := range events {
var msgs []Msg
switch e := event.Unwrap().(type) {
case coninput.KeyEventRecord:
if !e.KeyDown || e.VirtualKeyCode == coninput.VK_SHIFT {
continue
}
for i := 0; i < int(e.RepeatCount); i++ {
eventKeyType := keyType(e)
var runes []rune
// Add the character only if the key type is an actual character and not a control sequence.
// This mimics the behavior in readAnsiInputs where the character is also removed.
// We don't need to handle KeySpace here. See the comment in keyType().
if eventKeyType == KeyRunes {
runes = []rune{e.Char}
}
msgs = append(msgs, KeyMsg{
Type: eventKeyType,
Runes: runes,
Alt: e.ControlKeyState.Contains(coninput.LEFT_ALT_PRESSED | coninput.RIGHT_ALT_PRESSED),
})
}
case coninput.WindowBufferSizeEventRecord:
if e != ws {
ws = e
msgs = append(msgs, WindowSizeMsg{
Width: int(e.Size.X),
Height: int(e.Size.Y),
})
}
case coninput.MouseEventRecord:
event := mouseEvent(ps, e)
if event.Type != MouseUnknown {
msgs = append(msgs, event)
}
ps = e.ButtonState
case coninput.FocusEventRecord, coninput.MenuEventRecord:
// ignore
default: // unknown event
continue
}
// Send all messages to the channel
for _, msg := range msgs {
select {
case msgsch <- msg:
case <-ctx.Done():
err := ctx.Err()
if err != nil {
return fmt.Errorf("coninput context error: %w", err)
}
return err
}
}
}
}
}
func mouseEventButton(p, s coninput.ButtonState) (button MouseButton, action MouseAction) {
btn := p ^ s
action = MouseActionPress
if btn&s == 0 {
action = MouseActionRelease
}
if btn == 0 {
switch {
case s&coninput.FROM_LEFT_1ST_BUTTON_PRESSED > 0:
button = MouseButtonLeft
case s&coninput.FROM_LEFT_2ND_BUTTON_PRESSED > 0:
button = MouseButtonMiddle
case s&coninput.RIGHTMOST_BUTTON_PRESSED > 0:
button = MouseButtonRight
case s&coninput.FROM_LEFT_3RD_BUTTON_PRESSED > 0:
button = MouseButtonBackward
case s&coninput.FROM_LEFT_4TH_BUTTON_PRESSED > 0:
button = MouseButtonForward
}
return
}
switch {
case btn == coninput.FROM_LEFT_1ST_BUTTON_PRESSED: // left button
button = MouseButtonLeft
case btn == coninput.RIGHTMOST_BUTTON_PRESSED: // right button
button = MouseButtonRight
case btn == coninput.FROM_LEFT_2ND_BUTTON_PRESSED: // middle button
button = MouseButtonMiddle
case btn == coninput.FROM_LEFT_3RD_BUTTON_PRESSED: // unknown (possibly mouse backward)
button = MouseButtonBackward
case btn == coninput.FROM_LEFT_4TH_BUTTON_PRESSED: // unknown (possibly mouse forward)
button = MouseButtonForward
}
return button, action
}
func mouseEvent(p coninput.ButtonState, e coninput.MouseEventRecord) MouseMsg {
ev := MouseMsg{
X: int(e.MousePositon.X),
Y: int(e.MousePositon.Y),
Alt: e.ControlKeyState.Contains(coninput.LEFT_ALT_PRESSED | coninput.RIGHT_ALT_PRESSED),
Ctrl: e.ControlKeyState.Contains(coninput.LEFT_CTRL_PRESSED | coninput.RIGHT_CTRL_PRESSED),
Shift: e.ControlKeyState.Contains(coninput.SHIFT_PRESSED),
}
switch e.EventFlags {
case coninput.CLICK, coninput.DOUBLE_CLICK:
ev.Button, ev.Action = mouseEventButton(p, e.ButtonState)
if ev.Action == MouseActionRelease {
ev.Type = MouseRelease
}
switch ev.Button {
case MouseButtonLeft:
ev.Type = MouseLeft
case MouseButtonMiddle:
ev.Type = MouseMiddle
case MouseButtonRight:
ev.Type = MouseRight
case MouseButtonBackward:
ev.Type = MouseBackward
case MouseButtonForward:
ev.Type = MouseForward
}
case coninput.MOUSE_WHEELED:
if e.WheelDirection > 0 {
ev.Button = MouseButtonWheelUp
ev.Type = MouseWheelUp
} else {
ev.Button = MouseButtonWheelDown
ev.Type = MouseWheelDown
}
case coninput.MOUSE_HWHEELED:
if e.WheelDirection > 0 {
ev.Button = MouseButtonWheelRight
ev.Type = MouseWheelRight
} else {
ev.Button = MouseButtonWheelLeft
ev.Type = MouseWheelLeft
}
case coninput.MOUSE_MOVED:
ev.Button, _ = mouseEventButton(p, e.ButtonState)
ev.Action = MouseActionMotion
ev.Type = MouseMotion
}
return ev
}
func keyType(e coninput.KeyEventRecord) KeyType {
code := e.VirtualKeyCode
shiftPressed := e.ControlKeyState.Contains(coninput.SHIFT_PRESSED)
ctrlPressed := e.ControlKeyState.Contains(coninput.LEFT_CTRL_PRESSED | coninput.RIGHT_CTRL_PRESSED)
switch code {
case coninput.VK_RETURN:
return KeyEnter
case coninput.VK_BACK:
return KeyBackspace
case coninput.VK_TAB:
if shiftPressed {
return KeyShiftTab
}
return KeyTab
case coninput.VK_SPACE:
return KeyRunes // this could be KeySpace but on unix space also produces KeyRunes
case coninput.VK_ESCAPE:
return KeyEscape
case coninput.VK_UP:
switch {
case shiftPressed && ctrlPressed:
return KeyCtrlShiftUp
case shiftPressed:
return KeyShiftUp
case ctrlPressed:
return KeyCtrlUp
default:
return KeyUp
}
case coninput.VK_DOWN:
switch {
case shiftPressed && ctrlPressed:
return KeyCtrlShiftDown
case shiftPressed:
return KeyShiftDown
case ctrlPressed:
return KeyCtrlDown
default:
return KeyDown
}
case coninput.VK_RIGHT:
switch {
case shiftPressed && ctrlPressed:
return KeyCtrlShiftRight
case shiftPressed:
return KeyShiftRight
case ctrlPressed:
return KeyCtrlRight
default:
return KeyRight
}
case coninput.VK_LEFT:
switch {
case shiftPressed && ctrlPressed:
return KeyCtrlShiftLeft
case shiftPressed:
return KeyShiftLeft
case ctrlPressed:
return KeyCtrlLeft
default:
return KeyLeft
}
case coninput.VK_HOME:
switch {
case shiftPressed && ctrlPressed:
return KeyCtrlShiftHome
case shiftPressed:
return KeyShiftHome
case ctrlPressed:
return KeyCtrlHome
default:
return KeyHome
}
case coninput.VK_END:
switch {
case shiftPressed && ctrlPressed:
return KeyCtrlShiftEnd
case shiftPressed:
return KeyShiftEnd
case ctrlPressed:
return KeyCtrlEnd
default:
return KeyEnd
}
case coninput.VK_PRIOR:
return KeyPgUp
case coninput.VK_NEXT:
return KeyPgDown
case coninput.VK_DELETE:
return KeyDelete
default:
if e.ControlKeyState&(coninput.LEFT_CTRL_PRESSED|coninput.RIGHT_CTRL_PRESSED) == 0 {
return KeyRunes
}
switch e.Char {
case '@':
return KeyCtrlAt
case '\x01':
return KeyCtrlA
case '\x02':
return KeyCtrlB
case '\x03':
return KeyCtrlC
case '\x04':
return KeyCtrlD
case '\x05':
return KeyCtrlE
case '\x06':
return KeyCtrlF
case '\a':
return KeyCtrlG
case '\b':
return KeyCtrlH
case '\t':
return KeyCtrlI
case '\n':
return KeyCtrlJ
case '\v':
return KeyCtrlK
case '\f':
return KeyCtrlL
case '\r':
return KeyCtrlM
case '\x0e':
return KeyCtrlN
case '\x0f':
return KeyCtrlO
case '\x10':
return KeyCtrlP
case '\x11':
return KeyCtrlQ
case '\x12':
return KeyCtrlR
case '\x13':
return KeyCtrlS
case '\x14':
return KeyCtrlT
case '\x15':
return KeyCtrlU
case '\x16':
return KeyCtrlV
case '\x17':
return KeyCtrlW
case '\x18':
return KeyCtrlX
case '\x19':
return KeyCtrlY
case '\x1a':
return KeyCtrlZ
case '\x1b':
return KeyCtrlCloseBracket
case '\x1c':
return KeyCtrlBackslash
case '\x1f':
return KeyCtrlUnderscore
}
switch code {
case coninput.VK_OEM_4:
return KeyCtrlOpenBracket
}
return KeyRunes
}
}