vendor/github.com/charmbracelet/bubbletea/key_windows.go

351 lines · 8362 bytes

  1//go:build windows
  2// +build windows
  3
  4package tea
  5
  6import (
  7	"context"
  8	"fmt"
  9	"io"
 10
 11	"github.com/erikgeiser/coninput"
 12	localereader "github.com/mattn/go-localereader"
 13	"golang.org/x/sys/windows"
 14)
 15
 16func readInputs(ctx context.Context, msgs chan<- Msg, input io.Reader) error {
 17	if coninReader, ok := input.(*conInputReader); ok {
 18		return readConInputs(ctx, msgs, coninReader.conin)
 19	}
 20
 21	return readAnsiInputs(ctx, msgs, localereader.NewReader(input))
 22}
 23
 24func readConInputs(ctx context.Context, msgsch chan<- Msg, con windows.Handle) error {
 25	var ps coninput.ButtonState                 // keep track of previous mouse state
 26	var ws coninput.WindowBufferSizeEventRecord // keep track of the last window size event
 27	for {
 28		events, err := coninput.ReadNConsoleInputs(con, 16)
 29		if err != nil {
 30			return fmt.Errorf("read coninput events: %w", err)
 31		}
 32
 33		for _, event := range events {
 34			var msgs []Msg
 35			switch e := event.Unwrap().(type) {
 36			case coninput.KeyEventRecord:
 37				if !e.KeyDown || e.VirtualKeyCode == coninput.VK_SHIFT {
 38					continue
 39				}
 40
 41				for i := 0; i < int(e.RepeatCount); i++ {
 42					eventKeyType := keyType(e)
 43					var runes []rune
 44
 45					// Add the character only if the key type is an actual character and not a control sequence.
 46					// This mimics the behavior in readAnsiInputs where the character is also removed.
 47					// We don't need to handle KeySpace here. See the comment in keyType().
 48					if eventKeyType == KeyRunes {
 49						runes = []rune{e.Char}
 50					}
 51
 52					msgs = append(msgs, KeyMsg{
 53						Type:  eventKeyType,
 54						Runes: runes,
 55						Alt:   e.ControlKeyState.Contains(coninput.LEFT_ALT_PRESSED | coninput.RIGHT_ALT_PRESSED),
 56					})
 57				}
 58			case coninput.WindowBufferSizeEventRecord:
 59				if e != ws {
 60					ws = e
 61					msgs = append(msgs, WindowSizeMsg{
 62						Width:  int(e.Size.X),
 63						Height: int(e.Size.Y),
 64					})
 65				}
 66			case coninput.MouseEventRecord:
 67				event := mouseEvent(ps, e)
 68				if event.Type != MouseUnknown {
 69					msgs = append(msgs, event)
 70				}
 71				ps = e.ButtonState
 72			case coninput.FocusEventRecord, coninput.MenuEventRecord:
 73				// ignore
 74			default: // unknown event
 75				continue
 76			}
 77
 78			// Send all messages to the channel
 79			for _, msg := range msgs {
 80				select {
 81				case msgsch <- msg:
 82				case <-ctx.Done():
 83					err := ctx.Err()
 84					if err != nil {
 85						return fmt.Errorf("coninput context error: %w", err)
 86					}
 87					return err
 88				}
 89			}
 90		}
 91	}
 92}
 93
 94func mouseEventButton(p, s coninput.ButtonState) (button MouseButton, action MouseAction) {
 95	btn := p ^ s
 96	action = MouseActionPress
 97	if btn&s == 0 {
 98		action = MouseActionRelease
 99	}
100
101	if btn == 0 {
102		switch {
103		case s&coninput.FROM_LEFT_1ST_BUTTON_PRESSED > 0:
104			button = MouseButtonLeft
105		case s&coninput.FROM_LEFT_2ND_BUTTON_PRESSED > 0:
106			button = MouseButtonMiddle
107		case s&coninput.RIGHTMOST_BUTTON_PRESSED > 0:
108			button = MouseButtonRight
109		case s&coninput.FROM_LEFT_3RD_BUTTON_PRESSED > 0:
110			button = MouseButtonBackward
111		case s&coninput.FROM_LEFT_4TH_BUTTON_PRESSED > 0:
112			button = MouseButtonForward
113		}
114		return
115	}
116
117	switch {
118	case btn == coninput.FROM_LEFT_1ST_BUTTON_PRESSED: // left button
119		button = MouseButtonLeft
120	case btn == coninput.RIGHTMOST_BUTTON_PRESSED: // right button
121		button = MouseButtonRight
122	case btn == coninput.FROM_LEFT_2ND_BUTTON_PRESSED: // middle button
123		button = MouseButtonMiddle
124	case btn == coninput.FROM_LEFT_3RD_BUTTON_PRESSED: // unknown (possibly mouse backward)
125		button = MouseButtonBackward
126	case btn == coninput.FROM_LEFT_4TH_BUTTON_PRESSED: // unknown (possibly mouse forward)
127		button = MouseButtonForward
128	}
129
130	return button, action
131}
132
133func mouseEvent(p coninput.ButtonState, e coninput.MouseEventRecord) MouseMsg {
134	ev := MouseMsg{
135		X:     int(e.MousePositon.X),
136		Y:     int(e.MousePositon.Y),
137		Alt:   e.ControlKeyState.Contains(coninput.LEFT_ALT_PRESSED | coninput.RIGHT_ALT_PRESSED),
138		Ctrl:  e.ControlKeyState.Contains(coninput.LEFT_CTRL_PRESSED | coninput.RIGHT_CTRL_PRESSED),
139		Shift: e.ControlKeyState.Contains(coninput.SHIFT_PRESSED),
140	}
141	switch e.EventFlags {
142	case coninput.CLICK, coninput.DOUBLE_CLICK:
143		ev.Button, ev.Action = mouseEventButton(p, e.ButtonState)
144		if ev.Action == MouseActionRelease {
145			ev.Type = MouseRelease
146		}
147		switch ev.Button {
148		case MouseButtonLeft:
149			ev.Type = MouseLeft
150		case MouseButtonMiddle:
151			ev.Type = MouseMiddle
152		case MouseButtonRight:
153			ev.Type = MouseRight
154		case MouseButtonBackward:
155			ev.Type = MouseBackward
156		case MouseButtonForward:
157			ev.Type = MouseForward
158		}
159	case coninput.MOUSE_WHEELED:
160		if e.WheelDirection > 0 {
161			ev.Button = MouseButtonWheelUp
162			ev.Type = MouseWheelUp
163		} else {
164			ev.Button = MouseButtonWheelDown
165			ev.Type = MouseWheelDown
166		}
167	case coninput.MOUSE_HWHEELED:
168		if e.WheelDirection > 0 {
169			ev.Button = MouseButtonWheelRight
170			ev.Type = MouseWheelRight
171		} else {
172			ev.Button = MouseButtonWheelLeft
173			ev.Type = MouseWheelLeft
174		}
175	case coninput.MOUSE_MOVED:
176		ev.Button, _ = mouseEventButton(p, e.ButtonState)
177		ev.Action = MouseActionMotion
178		ev.Type = MouseMotion
179	}
180
181	return ev
182}
183
184func keyType(e coninput.KeyEventRecord) KeyType {
185	code := e.VirtualKeyCode
186
187	shiftPressed := e.ControlKeyState.Contains(coninput.SHIFT_PRESSED)
188	ctrlPressed := e.ControlKeyState.Contains(coninput.LEFT_CTRL_PRESSED | coninput.RIGHT_CTRL_PRESSED)
189
190	switch code {
191	case coninput.VK_RETURN:
192		return KeyEnter
193	case coninput.VK_BACK:
194		return KeyBackspace
195	case coninput.VK_TAB:
196		if shiftPressed {
197			return KeyShiftTab
198		}
199		return KeyTab
200	case coninput.VK_SPACE:
201		return KeyRunes // this could be KeySpace but on unix space also produces KeyRunes
202	case coninput.VK_ESCAPE:
203		return KeyEscape
204	case coninput.VK_UP:
205		switch {
206		case shiftPressed && ctrlPressed:
207			return KeyCtrlShiftUp
208		case shiftPressed:
209			return KeyShiftUp
210		case ctrlPressed:
211			return KeyCtrlUp
212		default:
213			return KeyUp
214		}
215	case coninput.VK_DOWN:
216		switch {
217		case shiftPressed && ctrlPressed:
218			return KeyCtrlShiftDown
219		case shiftPressed:
220			return KeyShiftDown
221		case ctrlPressed:
222			return KeyCtrlDown
223		default:
224			return KeyDown
225		}
226	case coninput.VK_RIGHT:
227		switch {
228		case shiftPressed && ctrlPressed:
229			return KeyCtrlShiftRight
230		case shiftPressed:
231			return KeyShiftRight
232		case ctrlPressed:
233			return KeyCtrlRight
234		default:
235			return KeyRight
236		}
237	case coninput.VK_LEFT:
238		switch {
239		case shiftPressed && ctrlPressed:
240			return KeyCtrlShiftLeft
241		case shiftPressed:
242			return KeyShiftLeft
243		case ctrlPressed:
244			return KeyCtrlLeft
245		default:
246			return KeyLeft
247		}
248	case coninput.VK_HOME:
249		switch {
250		case shiftPressed && ctrlPressed:
251			return KeyCtrlShiftHome
252		case shiftPressed:
253			return KeyShiftHome
254		case ctrlPressed:
255			return KeyCtrlHome
256		default:
257			return KeyHome
258		}
259	case coninput.VK_END:
260		switch {
261		case shiftPressed && ctrlPressed:
262			return KeyCtrlShiftEnd
263		case shiftPressed:
264			return KeyShiftEnd
265		case ctrlPressed:
266			return KeyCtrlEnd
267		default:
268			return KeyEnd
269		}
270	case coninput.VK_PRIOR:
271		return KeyPgUp
272	case coninput.VK_NEXT:
273		return KeyPgDown
274	case coninput.VK_DELETE:
275		return KeyDelete
276	default:
277		if e.ControlKeyState&(coninput.LEFT_CTRL_PRESSED|coninput.RIGHT_CTRL_PRESSED) == 0 {
278			return KeyRunes
279		}
280
281		switch e.Char {
282		case '@':
283			return KeyCtrlAt
284		case '\x01':
285			return KeyCtrlA
286		case '\x02':
287			return KeyCtrlB
288		case '\x03':
289			return KeyCtrlC
290		case '\x04':
291			return KeyCtrlD
292		case '\x05':
293			return KeyCtrlE
294		case '\x06':
295			return KeyCtrlF
296		case '\a':
297			return KeyCtrlG
298		case '\b':
299			return KeyCtrlH
300		case '\t':
301			return KeyCtrlI
302		case '\n':
303			return KeyCtrlJ
304		case '\v':
305			return KeyCtrlK
306		case '\f':
307			return KeyCtrlL
308		case '\r':
309			return KeyCtrlM
310		case '\x0e':
311			return KeyCtrlN
312		case '\x0f':
313			return KeyCtrlO
314		case '\x10':
315			return KeyCtrlP
316		case '\x11':
317			return KeyCtrlQ
318		case '\x12':
319			return KeyCtrlR
320		case '\x13':
321			return KeyCtrlS
322		case '\x14':
323			return KeyCtrlT
324		case '\x15':
325			return KeyCtrlU
326		case '\x16':
327			return KeyCtrlV
328		case '\x17':
329			return KeyCtrlW
330		case '\x18':
331			return KeyCtrlX
332		case '\x19':
333			return KeyCtrlY
334		case '\x1a':
335			return KeyCtrlZ
336		case '\x1b':
337			return KeyCtrlCloseBracket
338		case '\x1c':
339			return KeyCtrlBackslash
340		case '\x1f':
341			return KeyCtrlUnderscore
342		}
343
344		switch code {
345		case coninput.VK_OEM_4:
346			return KeyCtrlOpenBracket
347		}
348
349		return KeyRunes
350	}
351}