Skip to content

Commit 44194b0

Browse files
committed
windows: add console input handling api
Add Console API input handling syscalls. This is an effort to upstream the amazing coninput library by @erikgeiser https://github.com/erikgeiser/coninput
1 parent 6943ab6 commit 44194b0

File tree

3 files changed

+498
-0
lines changed

3 files changed

+498
-0
lines changed

windows/syscall_windows.go

+45
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ func NewCallbackCDecl(fn interface{}) uintptr {
310310
//sys WriteConsole(console Handle, buf *uint16, towrite uint32, written *uint32, reserved *byte) (err error) = kernel32.WriteConsoleW
311311
//sys ReadConsole(console Handle, buf *uint16, toread uint32, read *uint32, inputControl *byte) (err error) = kernel32.ReadConsoleW
312312
//sys resizePseudoConsole(pconsole Handle, size uint32) (hr error) = kernel32.ResizePseudoConsole
313+
//sys readConsoleInputW(console Handle, buf *InputRecord, toread uint32, read *uint32) (err error) = kernel32.ReadConsoleInputW
314+
//sys peekConsoleInputW(console Handle, buf *InputRecord, toread uint32, read *uint32) (err error) = kernel32.PeekConsoleInputW
315+
//sys getNumberOfConsoleInputEvents(console Handle, numevents *uint32) (err error) = kernel32.GetNumberOfConsoleInputEvents
316+
//sys FlushConsoleInputBuffer(console Handle) (err error) = kernel32.FlushConsoleInputBuffer
313317
//sys CreateToolhelp32Snapshot(flags uint32, processId uint32) (handle Handle, err error) [failretval==InvalidHandle] = kernel32.CreateToolhelp32Snapshot
314318
//sys Module32First(snapshot Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32FirstW
315319
//sys Module32Next(snapshot Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32NextW
@@ -1916,3 +1920,44 @@ const (
19161920
EV_ERR = 0x0080
19171921
EV_RING = 0x0100
19181922
)
1923+
1924+
// ReadConsoleInputW provides an ideomatic interface to the Windows console API
1925+
// function ReadConsoleInputW (see
1926+
// https://docs.microsoft.com/en-us/windows/console/readconsoleinput). The size
1927+
// of inputRecords must be greater than zero.
1928+
func ReadConsoleInputW(console Handle, inputRecords []InputRecord) (uint32, error) {
1929+
if len(inputRecords) == 0 {
1930+
return 0, fmt.Errorf("size of input record buffer cannot be zero")
1931+
}
1932+
1933+
var read uint32
1934+
err := readConsoleInputW(console, &inputRecords[0], uint32(len(inputRecords)), &read)
1935+
1936+
return read, err
1937+
}
1938+
1939+
// PeekConsoleInputW provides an ideomatic interface to the Windows console API
1940+
// function PeekConsoleInputW (see
1941+
// https://docs.microsoft.com/en-us/windows/console/peekconsoleinput). The size
1942+
// of inputRecords must be greater than zero.
1943+
func PeekConsoleInputW(console Handle, inputRecords []InputRecord) (uint32, error) {
1944+
if len(inputRecords) == 0 {
1945+
return 0, fmt.Errorf("size of input record buffer cannot be zero")
1946+
}
1947+
1948+
var read uint32
1949+
1950+
err := peekConsoleInputW(console, &inputRecords[0], uint32(len(inputRecords)), &read)
1951+
1952+
return read, err
1953+
}
1954+
1955+
// GetNumberOfConsoleInputEvents provides an ideomatic interface to the Windows
1956+
// console API function GetNumberOfConsoleInputEvents (see
1957+
// https://docs.microsoft.com/en-us/windows/console/getnumberofconsoleinputevents).
1958+
func GetNumberOfConsoleInputEvents(console Handle) (uint32, error) {
1959+
var nEvents uint32
1960+
err := getNumberOfConsoleInputEvents(console, &nEvents)
1961+
1962+
return nEvents, err
1963+
}

0 commit comments

Comments
 (0)