Skip to content

Commit 1ba63fc

Browse files
committed
windows: add console input api
This adds multiple functions to read, count, and discard console input events. Using `ReadConsoleInput` instead of `ReadConsole` or `ReadFile` is necessary to capture extended events such as window resize, focus, and extended key and mouse events. This also define console event types and structs.
1 parent adbb8bb commit 1ba63fc

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

windows/syscall_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,10 @@ func NewCallbackCDecl(fn interface{}) uintptr {
319319
//sys SetConsoleOutputCP(cp uint32) (err error) = kernel32.SetConsoleOutputCP
320320
//sys WriteConsole(console Handle, buf *uint16, towrite uint32, written *uint32, reserved *byte) (err error) = kernel32.WriteConsoleW
321321
//sys ReadConsole(console Handle, buf *uint16, toread uint32, read *uint32, inputControl *byte) (err error) = kernel32.ReadConsoleW
322+
//sys ReadConsoleInput(console Handle, buf *InputRecord, toread uint32, read *uint32) (err error) = kernel32.ReadConsoleInputW
323+
//sys PeekConsoleInput(console Handle, buf *InputRecord, toread uint32, read *uint32) (err error) = kernel32.PeekConsoleInputW
324+
//sys GetNumberOfConsoleInputEvents(console Handle, numevents *uint32) (err error) = kernel32.GetNumberOfConsoleInputEvents
325+
//sys FlushConsoleInputBuffer(console Handle) (err error) = kernel32.FlushConsoleInputBuffer
322326
//sys resizePseudoConsole(pconsole Handle, size uint32) (hr error) = kernel32.ResizePseudoConsole
323327
//sys CreateToolhelp32Snapshot(flags uint32, processId uint32) (handle Handle, err error) [failretval==InvalidHandle] = kernel32.CreateToolhelp32Snapshot
324328
//sys Module32First(snapshot Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32FirstW

windows/types_windows.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3474,3 +3474,90 @@ const (
34743474
KLF_NOTELLSHELL = 0x00000080
34753475
KLF_SETFORPROCESS = 0x00000100
34763476
)
3477+
3478+
// FocusEventRecord corresponds to the FocusEventRecord structure from the
3479+
// Windows console API.
3480+
// https://docs.microsoft.com/en-us/windows/console/focus-event-record-str
3481+
type FocusEventRecord struct {
3482+
// SetFocus is reserved and should not be used.
3483+
SetFocus bool
3484+
}
3485+
3486+
// KeyEventRecord corresponds to the KeyEventRecord structure from the Windows
3487+
// console API.
3488+
// https://docs.microsoft.com/en-us/windows/console/key-event-record-str
3489+
type KeyEventRecord struct {
3490+
// KeyDown specified whether the key is pressed or released.
3491+
KeyDown bool
3492+
3493+
// RepeatCount indicates that a key is being held down. For example, when a
3494+
// key is held down, five events with RepeatCount equal to 1 may be
3495+
// generated, one event with RepeatCount equal to 5, or multiple events
3496+
// with RepeatCount greater than or equal to 1.
3497+
RepeatCount uint16
3498+
3499+
// VirtualKeyCode identifies the given key in a device-independent manner
3500+
// (see
3501+
// https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes).
3502+
VirtualKeyCode uint16
3503+
3504+
// VirtualScanCode represents the device-dependent value generated by the
3505+
// keyboard hardware.
3506+
VirtualScanCode uint16
3507+
3508+
// Char is the character that corresponds to the pressed key. Char can be
3509+
// zero for some keys.
3510+
Char rune
3511+
3512+
//ControlKeyState holds the state of the control keys.
3513+
ControlKeyState uint32
3514+
}
3515+
3516+
// MenuEventRecord corresponds to the MenuEventRecord structure from the
3517+
// Windows console API.
3518+
// https://docs.microsoft.com/en-us/windows/console/menu-event-record-str
3519+
type MenuEventRecord struct {
3520+
CommandID uint32
3521+
}
3522+
3523+
// MouseEventRecord corresponds to the MouseEventRecord structure from the
3524+
// Windows console API.
3525+
// https://docs.microsoft.com/en-us/windows/console/mouse-event-record-str
3526+
type MouseEventRecord struct {
3527+
// MousePosition contains the location of the cursor, in terms of the
3528+
// console screen buffer's character-cell coordinates.
3529+
MousePositon Coord
3530+
3531+
// ButtonState holds the status of the mouse buttons.
3532+
ButtonState uint32
3533+
3534+
// ControlKeyState holds the state of the control keys.
3535+
ControlKeyState uint32
3536+
3537+
// EventFlags specify the type of mouse event.
3538+
EventFlags uint32
3539+
}
3540+
3541+
// WindowBufferSizeRecord corresponds to the WindowBufferSizeRecord structure
3542+
// from the Windows console API.
3543+
// https://docs.microsoft.com/en-us/windows/console/window-buffer-size-record-str
3544+
type WindowBufferSizeRecord struct {
3545+
// Size contains the size of the console screen buffer, in character cell
3546+
// columns and rows.
3547+
Size Coord
3548+
}
3549+
3550+
// InputRecord corresponds to the INPUT_RECORD structure from the Windows
3551+
// console API.
3552+
//
3553+
// https://docs.microsoft.com/en-us/windows/console/input-record-str
3554+
type InputRecord struct {
3555+
// EventType specifies the type of event that helt in Event.
3556+
EventType uint16
3557+
3558+
// Padding of the 16-bit EventType to a whole 32-bit dword.
3559+
_ [2]byte
3560+
3561+
// Event holds the actual event data.
3562+
Event [16]byte
3563+
}

windows/zsyscall_windows.go

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)