Skip to content

Commit a8df59a

Browse files
committed
windows: add console input record event member functions
Events in `INPUT_RECORD` are defined as `union` with multiple members. Since Go doesn't have a notion of `union`s, we need to decode the input event into their respective types. Here, we use member functions that match the union type names to get the respective event type.
1 parent 1ba63fc commit a8df59a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

windows/types_windows.go

+48
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package windows
66

77
import (
8+
"encoding/binary"
89
"net"
910
"syscall"
1011
"unsafe"
@@ -3561,3 +3562,50 @@ type InputRecord struct {
35613562
// Event holds the actual event data.
35623563
Event [16]byte
35633564
}
3565+
3566+
// FocusEvent returns the event as a FOCUS_EVENT_RECORD.
3567+
func (ir InputRecord) FocusEvent() FocusEventRecord {
3568+
return FocusEventRecord{SetFocus: ir.Event[0] > 0}
3569+
}
3570+
3571+
// KeyEvent returns the event as a KEY_EVENT_RECORD.
3572+
func (ir InputRecord) KeyEvent() KeyEventRecord {
3573+
return KeyEventRecord{
3574+
KeyDown: binary.LittleEndian.Uint32(ir.Event[0:4]) > 0,
3575+
RepeatCount: binary.LittleEndian.Uint16(ir.Event[4:6]),
3576+
VirtualKeyCode: binary.LittleEndian.Uint16(ir.Event[6:8]),
3577+
VirtualScanCode: binary.LittleEndian.Uint16(ir.Event[8:10]),
3578+
Char: rune(binary.LittleEndian.Uint16(ir.Event[10:12])),
3579+
ControlKeyState: binary.LittleEndian.Uint32(ir.Event[12:16]),
3580+
}
3581+
}
3582+
3583+
// MouseEvent returns the event as a MOUSE_EVENT_RECORD.
3584+
func (ir InputRecord) MouseEvent() MouseEventRecord {
3585+
return MouseEventRecord{
3586+
MousePositon: Coord{
3587+
X: int16(binary.LittleEndian.Uint16(ir.Event[0:2])),
3588+
Y: int16(binary.LittleEndian.Uint16(ir.Event[2:4])),
3589+
},
3590+
ButtonState: binary.LittleEndian.Uint32(ir.Event[4:8]),
3591+
ControlKeyState: binary.LittleEndian.Uint32(ir.Event[8:12]),
3592+
EventFlags: binary.LittleEndian.Uint32(ir.Event[12:16]),
3593+
}
3594+
}
3595+
3596+
// WindowBufferSizeEvent returns the event as a WINDOW_BUFFER_SIZE_RECORD.
3597+
func (ir InputRecord) WindowBufferSizeEvent() WindowBufferSizeRecord {
3598+
return WindowBufferSizeRecord{
3599+
Size: Coord{
3600+
X: int16(binary.LittleEndian.Uint16(ir.Event[0:2])),
3601+
Y: int16(binary.LittleEndian.Uint16(ir.Event[2:4])),
3602+
},
3603+
}
3604+
}
3605+
3606+
// MenuEvent returns the event as a MENU_EVENT_RECORD.
3607+
func (ir InputRecord) MenuEvent() MenuEventRecord {
3608+
return MenuEventRecord{
3609+
CommandID: binary.LittleEndian.Uint32(ir.Event[0:4]),
3610+
}
3611+
}

0 commit comments

Comments
 (0)