Debian Codegraph @

windows/types_windows.go

raw
1 //nolint:gosec
2 package windows
3
4 import (
5 "encoding/binary"
6
7 "golang.org/x/sys/windows"
8 )
9
10 // FocusEventRecord corresponds to the FocusEventRecord structure from the
11 // Windows console API.
12 // https://docs.microsoft.com/en-us/windows/console/focus-event-record-str
13 type FocusEventRecord struct {
14 // SetFocus is reserved and should not be used.
15 SetFocus bool
16 }
17
18 // MenuEventRecord corresponds to the MenuEventRecord structure from the
19 // Windows console API.
20 // https://docs.microsoft.com/en-us/windows/console/menu-event-record-str
21 type MenuEventRecord struct {
22 CommandID uint32
23 }
24
25 // MouseEventRecord corresponds to the MouseEventRecord structure from the
26 // Windows console API.
27 // https://docs.microsoft.com/en-us/windows/console/mouse-event-record-str
28 type MouseEventRecord struct {
29 // MousePosition contains the location of the cursor, in terms of the
30 // console screen buffer's character-cell coordinates.
31 MousePositon windows.Coord
32
33 // ButtonState holds the status of the mouse buttons.
34 ButtonState uint32
35
36 // ControlKeyState holds the state of the control keys.
37 ControlKeyState uint32
38
39 // EventFlags specify the type of mouse event.
40 EventFlags uint32
41 }
42
43 // WindowBufferSizeRecord corresponds to the WindowBufferSizeRecord structure
44 // from the Windows console API.
45 // https://docs.microsoft.com/en-us/windows/console/window-buffer-size-record-str
46 type WindowBufferSizeRecord struct {
47 // Size contains the size of the console screen buffer, in character cell
48 // columns and rows.
49 Size windows.Coord
50 }
51
52 // FocusEvent returns the event as a FOCUS_EVENT_RECORD.
53 func (ir InputRecord) FocusEvent() FocusEventRecord {
54 return FocusEventRecord{SetFocus: ir.Event[0] > 0}
55 }
56
57 // KeyEvent returns the event as a KEY_EVENT_RECORD.
58 func (ir InputRecord) KeyEvent() KeyEventRecord {
59 return KeyEventRecord{
60 KeyDown: binary.LittleEndian.Uint32(ir.Event[0:4]) > 0,
61 RepeatCount: binary.LittleEndian.Uint16(ir.Event[4:6]),
62 VirtualKeyCode: binary.LittleEndian.Uint16(ir.Event[6:8]),
63 VirtualScanCode: binary.LittleEndian.Uint16(ir.Event[8:10]),
64 Char: rune(binary.LittleEndian.Uint16(ir.Event[10:12])),
65 ControlKeyState: binary.LittleEndian.Uint32(ir.Event[12:16]),
66 }
67 }
68
69 // MouseEvent returns the event as a MOUSE_EVENT_RECORD.
70 func (ir InputRecord) MouseEvent() MouseEventRecord {
71 return MouseEventRecord{
72 MousePositon: windows.Coord{
73 X: int16(binary.LittleEndian.Uint16(ir.Event[0:2])),
74 Y: int16(binary.LittleEndian.Uint16(ir.Event[2:4])),
75 },
76 ButtonState: binary.LittleEndian.Uint32(ir.Event[4:8]),
77 ControlKeyState: binary.LittleEndian.Uint32(ir.Event[8:12]),
78 EventFlags: binary.LittleEndian.Uint32(ir.Event[12:16]),
79 }
80 }
81
82 // WindowBufferSizeEvent returns the event as a WINDOW_BUFFER_SIZE_RECORD.
83 func (ir InputRecord) WindowBufferSizeEvent() WindowBufferSizeRecord {
84 return WindowBufferSizeRecord{
85 Size: windows.Coord{
86 X: int16(binary.LittleEndian.Uint16(ir.Event[0:2])),
87 Y: int16(binary.LittleEndian.Uint16(ir.Event[2:4])),
88 },
89 }
90 }
91
92 // MenuEvent returns the event as a MENU_EVENT_RECORD.
93 func (ir InputRecord) MenuEvent() MenuEventRecord {
94 return MenuEventRecord{
95 CommandID: binary.LittleEndian.Uint32(ir.Event[0:4]),
96 }
97 }