-
Notifications
You must be signed in to change notification settings - Fork 563
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 api #196
base: master
Are you sure you want to change the base?
Conversation
This PR (HEAD: 44194b0) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/sys/+/585495. Important tips:
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/585495. |
Message from Ayman Bagabas: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/585495. |
This PR (HEAD: a9a4b62) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/sys/+/585495. Important tips:
|
Here's example code of how to use this. This is based on https://github.com/erikgeiser/coninput/blob/main/example/main.go package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"golang.org/x/sys/windows"
)
func main() {
con, err := windows.GetStdHandle(windows.STD_INPUT_HANDLE)
if err != nil {
log.Fatalf("get stdin handle: %s", err)
}
var originalConsoleMode uint32
err = windows.GetConsoleMode(con, &originalConsoleMode)
if err != nil {
log.Fatalf("get console mode: %s", err)
}
newConsoleMode := uint32(windows.ENABLE_MOUSE_INPUT) |
windows.ENABLE_WINDOW_INPUT |
windows.ENABLE_PROCESSED_INPUT |
windows.ENABLE_EXTENDED_FLAGS
err = windows.SetConsoleMode(con, newConsoleMode)
if err != nil {
log.Fatalf("set console mode: %s", err)
}
defer func() {
resetErr := windows.SetConsoleMode(con, originalConsoleMode)
if err == nil && resetErr != nil {
log.Fatalf("reset console mode: %s", resetErr)
}
}()
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
var read uint32
events := [16]windows.InputRecord{}
for {
if ctx.Err() != nil {
break
}
if err := windows.ReadConsoleInput(con, &events[0], uint32(len(events)), &read); err != nil {
log.Fatalf("read input events: %s", err)
}
fmt.Printf("Read %d events:\n", len(events))
for _, event := range events[:read] {
var e any
switch event.EventType {
case windows.KEY_EVENT:
e = event.KeyEvent()
case windows.MOUSE_EVENT:
e = event.MouseEvent()
case windows.WINDOW_BUFFER_SIZE_EVENT:
e = event.WindowBufferSizeEvent()
case windows.FOCUS_EVENT:
e = event.FocusEvent()
case windows.MENU_EVENT:
e = event.MenuEvent()
}
fmt.Printf("%#v\n", e)
}
}
} |
Message from Ayman Bagabas: Patch Set 2: Code-Review+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/585495. |
Message from Ayman Bagabas: Patch Set 2: -Code-Review Please don’t reply on this GitHub thread. Visit golang.org/cl/585495. |
Message from Ayman Bagabas: Patch Set 2: Code-Review+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/585495. |
Add Console API input handling syscall wrappers. This is an effort to upstream the amazing coninput library by @erikgeiser https://github.com/erikgeiser/coninput Related golang#98
This PR (HEAD: e49ad90) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/sys/+/585495. Important tips:
|
Add Console API input handling syscalls. This is an effort to upstream the amazing coninput library by @erikgeiser
https://github.com/erikgeiser/coninput