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

os: Support reading Ctrl + Space from os.Stdin on Windows. #60632

Closed
lonnywong opened this issue Jun 6, 2023 · 4 comments
Closed

os: Support reading Ctrl + Space from os.Stdin on Windows. #60632

lonnywong opened this issue Jun 6, 2023 · 4 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@lonnywong
Copy link

I'm writing an SSH client in Go, and want to pass Ctrl + Space to the server.

We can read Ctrl + Space from os.Stdin on MacOS and Linux, but not on Windows.

package main

import (
	"fmt"
	"os"

	"golang.org/x/term"
)

func main() {
	fd := int(os.Stdin.Fd())
	state, err := term.MakeRaw(fd)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer term.Restore(fd, state)

	fmt.Printf("Please press some keys ...\r\n")
	buf := make([]byte, 100)
	for {
		n, err := os.Stdin.Read(buf)
		fmt.Printf("n = %d, err = %v, buf = %#v\r\n", n, err, buf[:n])
		if n > 0 && buf[0] == '\x03' { // `ctrl + c` to exit
			break
		}
	}
}

Related to:

  1. Bug Report: Control+Space not sent to terminal emulator. microsoft/terminal#2865
  2. modify key event logic to fix ctrl+space not being sent PowerShell/openssh-portable#569
@gopherbot gopherbot added this to the Proposal milestone Jun 6, 2023
@seankhliao
Copy link
Member

This doesn't look like a go issue. os.Stdin has no concept of key combinations, it is just a stream of bytes. It is the terminal emulator's responsibility to turn key combinations into a byte sequence that's passed to programs

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jun 6, 2023
@lonnywong
Copy link
Author

lonnywong commented Jun 6, 2023

@seankhliao When the Ctrl+Space is pressed, Go could generate a byte \x00 to os.Stdin. Now there's no byte generates on Windows.
It works on MacOS and Linux, just not work on Windows.

@lonnywong
Copy link
Author

I think it's Go's job to generate bytes to os.Stdin.
On MacOS and Linux, Go generates \x00 to os.Stdin when Ctrl+Space is pressing.
On Windows, Go generates nothing to os.Stdin when Ctrl+Space is pressing.
I do think it's a Go issue.

@seankhliao
Copy link
Member

As mentioned in your linked issues, eg microsoft/terminal#2865 (comment) , it is the terminal's responsibility to convert any key presses into byte sequences for Go to consume (in your case, a null byte). Go does not observe the key presses, nor will Go generate any events into os.Stdin. os.Stdin is a file descriptor for a stream of bytes whose input is controlled by the parent process.

Closing as not a Go bug.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 6, 2023
@seankhliao seankhliao changed the title proposal: os: Support reading Ctrl + Space from os.Stdin on Windows. os: Support reading Ctrl + Space from os.Stdin on Windows. Jun 6, 2023
@seankhliao seankhliao modified the milestones: Proposal, Unplanned Jun 6, 2023
@golang golang locked and limited conversation to collaborators Jun 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants