package windows import ( "fmt" "strings" "testing" ) func TestInput(t *testing.T) { in, err := Open("CONIN$", O_RDWR, 0) if err != nil { panic(err) } var oldMode uint32 err = GetConsoleMode(in, &oldMode) if err != nil { panic(err) } // Even with ENABLE_MOUSE_INPUT, mouse events are not read by ReadConsoleInput var mode uint32 = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT err = SetConsoleMode(in, mode) if err != nil { panic(err) } //buf := make([]uint16, 1024) rec := InputRecord{} /*recs := make([]InputRecord, 4) recs[0] := InputRecord{} recs[1] := InputRecord{} recs[2] := InputRecord{} recs[3] := InputRecord{}*/ var read uint32 for i := 0; i < 100; i++ { ReadConsoleInput(in, &rec, uint32(2), &read) //ReadConsole(in, &buf[0], 1024, &read, nil) /*fmt.Printf("Read %d characters\r\n", read) for _, b := range buf[:read] { fmt.Printf("0x%x ", int(b)) } fmt.Print("\r\n") if buf[0] == 0x1 { fmt.Println("It's 1") }*/ //for i, rec := range recs { fmt.Println(rec.String()) /*recs[i] = InputRecord{} }*/ //fmt.Println(iC.String()) } SetConsoleMode(in, oldMode) } func (r *InputRecord) String() string { var sb strings.Builder sb.WriteString(fmt.Sprintf("Type: %d\n\r", r.Type)) sb.WriteString("Data: ") for _, b := range r.Data { sb.WriteString(fmt.Sprintf("0x%x ", b)) } sb.WriteString("\n\r") return sb.String() } /*func (r *InputControl) String() string { var sb strings.Builder sb.WriteString(fmt.Sprintf("Length: %d\n\r", r.NLength)) sb.WriteString(fmt.Sprintf("InitialChars: %d\n\r", r.NInitialChars)) sb.WriteString(fmt.Sprintf("DwCtrlWakeupMask: %d\n\r", r.DwCtrlWakeupMask)) sb.WriteString(fmt.Sprintf("DwControlKeyState: %d\n\r", r.DwControlKeyState)) return sb.String() }*/ /* // None of the members of InputControl are set by the ReadConsole functionality // And setting any of them doesn't have an effect either. // InputControl is the Go version of the CONSOLE_READCONSOLE_CONTROL struct. // For more information, see https://docs.microsoft.com/en-us/windows/console/console-readconsole-control type InputControl struct { // NLength is the size of the structure. // As it consists of 4 * 32 bits = 16 bytes, set it to 16. NLength uint32 // NInitialChars is the number of characters to be preserved in the input buffer NInitialChars uint32 // DwCtrlWakeupMask specifies which control characters between 0x00 and 0x1F should be used to signal that the read is complete. DwCtrlWakeupMask uint32 // DwControlKeyState specifies the state of the control keys. DwControlKeyState uint32 } */