Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

windows: add console input record event member functions #228

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions windows/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ func NewCallbackCDecl(fn interface{}) uintptr {
//sys SetConsoleOutputCP(cp uint32) (err error) = kernel32.SetConsoleOutputCP
//sys WriteConsole(console Handle, buf *uint16, towrite uint32, written *uint32, reserved *byte) (err error) = kernel32.WriteConsoleW
//sys ReadConsole(console Handle, buf *uint16, toread uint32, read *uint32, inputControl *byte) (err error) = kernel32.ReadConsoleW
//sys ReadConsoleInput(console Handle, buf *InputRecord, toread uint32, read *uint32) (err error) = kernel32.ReadConsoleInputW
//sys PeekConsoleInput(console Handle, buf *InputRecord, toread uint32, read *uint32) (err error) = kernel32.PeekConsoleInputW
//sys GetNumberOfConsoleInputEvents(console Handle, numevents *uint32) (err error) = kernel32.GetNumberOfConsoleInputEvents
//sys FlushConsoleInputBuffer(console Handle) (err error) = kernel32.FlushConsoleInputBuffer
//sys resizePseudoConsole(pconsole Handle, size uint32) (hr error) = kernel32.ResizePseudoConsole
//sys CreateToolhelp32Snapshot(flags uint32, processId uint32) (handle Handle, err error) [failretval==InvalidHandle] = kernel32.CreateToolhelp32Snapshot
//sys Module32First(snapshot Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32FirstW
Expand Down
135 changes: 135 additions & 0 deletions windows/types_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package windows

import (
"encoding/binary"
"net"
"syscall"
"unsafe"
Expand Down Expand Up @@ -3474,3 +3475,137 @@ const (
KLF_NOTELLSHELL = 0x00000080
KLF_SETFORPROCESS = 0x00000100
)

// FocusEventRecord corresponds to the FocusEventRecord structure from the
// Windows console API.
// https://docs.microsoft.com/en-us/windows/console/focus-event-record-str
type FocusEventRecord struct {
// SetFocus is reserved and should not be used.
SetFocus bool
}

// KeyEventRecord corresponds to the KeyEventRecord structure from the Windows
// console API.
// https://docs.microsoft.com/en-us/windows/console/key-event-record-str
type KeyEventRecord struct {
// KeyDown specified whether the key is pressed or released.
KeyDown bool

// RepeatCount indicates that a key is being held down. For example, when a
// key is held down, five events with RepeatCount equal to 1 may be
// generated, one event with RepeatCount equal to 5, or multiple events
// with RepeatCount greater than or equal to 1.
RepeatCount uint16

// VirtualKeyCode identifies the given key in a device-independent manner
// (see
// https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes).
VirtualKeyCode uint16

// VirtualScanCode represents the device-dependent value generated by the
// keyboard hardware.
VirtualScanCode uint16

// Char is the character that corresponds to the pressed key. Char can be
// zero for some keys.
Char rune

//ControlKeyState holds the state of the control keys.
ControlKeyState uint32
}

// MenuEventRecord corresponds to the MenuEventRecord structure from the
// Windows console API.
// https://docs.microsoft.com/en-us/windows/console/menu-event-record-str
type MenuEventRecord struct {
CommandID uint32
}

// MouseEventRecord corresponds to the MouseEventRecord structure from the
// Windows console API.
// https://docs.microsoft.com/en-us/windows/console/mouse-event-record-str
type MouseEventRecord struct {
// MousePosition contains the location of the cursor, in terms of the
// console screen buffer's character-cell coordinates.
MousePositon Coord

// ButtonState holds the status of the mouse buttons.
ButtonState uint32

// ControlKeyState holds the state of the control keys.
ControlKeyState uint32

// EventFlags specify the type of mouse event.
EventFlags uint32
}

// WindowBufferSizeRecord corresponds to the WindowBufferSizeRecord structure
// from the Windows console API.
// https://docs.microsoft.com/en-us/windows/console/window-buffer-size-record-str
type WindowBufferSizeRecord struct {
// Size contains the size of the console screen buffer, in character cell
// columns and rows.
Size Coord
}

// InputRecord corresponds to the INPUT_RECORD structure from the Windows
// console API.
//
// https://docs.microsoft.com/en-us/windows/console/input-record-str
type InputRecord struct {
// EventType specifies the type of event that helt in Event.
EventType uint16

// Padding of the 16-bit EventType to a whole 32-bit dword.
_ [2]byte

// Event holds the actual event data.
Event [16]byte
}

// FocusEvent returns the event as a FOCUS_EVENT_RECORD.
func (ir InputRecord) FocusEvent() FocusEventRecord {
return FocusEventRecord{SetFocus: ir.Event[0] > 0}
}

// KeyEvent returns the event as a KEY_EVENT_RECORD.
func (ir InputRecord) KeyEvent() KeyEventRecord {
return KeyEventRecord{
KeyDown: binary.LittleEndian.Uint32(ir.Event[0:4]) > 0,
RepeatCount: binary.LittleEndian.Uint16(ir.Event[4:6]),
VirtualKeyCode: binary.LittleEndian.Uint16(ir.Event[6:8]),
VirtualScanCode: binary.LittleEndian.Uint16(ir.Event[8:10]),
Char: rune(binary.LittleEndian.Uint16(ir.Event[10:12])),
ControlKeyState: binary.LittleEndian.Uint32(ir.Event[12:16]),
}
}

// MouseEvent returns the event as a MOUSE_EVENT_RECORD.
func (ir InputRecord) MouseEvent() MouseEventRecord {
return MouseEventRecord{
MousePositon: Coord{
X: int16(binary.LittleEndian.Uint16(ir.Event[0:2])),
Y: int16(binary.LittleEndian.Uint16(ir.Event[2:4])),
},
ButtonState: binary.LittleEndian.Uint32(ir.Event[4:8]),
ControlKeyState: binary.LittleEndian.Uint32(ir.Event[8:12]),
EventFlags: binary.LittleEndian.Uint32(ir.Event[12:16]),
}
}

// WindowBufferSizeEvent returns the event as a WINDOW_BUFFER_SIZE_RECORD.
func (ir InputRecord) WindowBufferSizeEvent() WindowBufferSizeRecord {
return WindowBufferSizeRecord{
Size: Coord{
X: int16(binary.LittleEndian.Uint16(ir.Event[0:2])),
Y: int16(binary.LittleEndian.Uint16(ir.Event[2:4])),
},
}
}

// MenuEvent returns the event as a MENU_EVENT_RECORD.
func (ir InputRecord) MenuEvent() MenuEventRecord {
return MenuEventRecord{
CommandID: binary.LittleEndian.Uint32(ir.Event[0:4]),
}
}
36 changes: 36 additions & 0 deletions windows/zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.