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

Fix: Input handling for Windows to prevent first key loss #1180

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 14 additions & 2 deletions inputreader_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os"
"sync"
"unsafe"

"github.com/charmbracelet/x/term"
"github.com/erikgeiser/coninput"
Expand Down Expand Up @@ -73,10 +74,21 @@ func (r *conInputReader) Close() error {
}

// Read implements cancelreader.CancelReader.
func (*conInputReader) Read(_ []byte) (n int, err error) {
return 0, nil
func (r *conInputReader) Read(p []byte) (n int, err error) {
var inputBuf [128]uint16 // Increase buffer size
var read uint32

err = windows.ReadConsole(r.conin, (*uint16)(unsafe.Pointer(&inputBuf[0])), uint32(len(inputBuf)), &read, nil)
if err != nil {
return 0, fmt.Errorf("failed to read console input: %w", err)
}

n = copy(p, windows.UTF16ToString(inputBuf[:read]))
return n, nil
}



func prepareConsole(input windows.Handle, modes ...uint32) (originalMode uint32, err error) {
err = windows.GetConsoleMode(input, &originalMode)
if err != nil {
Expand Down
Loading