8000 x/sys/windows: Neither Read() nor ReadConsole() read special keys · Issue #44373 · golang/go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
x/sys/windows: Neither Read() nor ReadConsole() read special keys #44373
Closed
@scrouthtv

Description

@scrouthtv

What version of Go are you using (go version)?

$ go version
go version go1.16 windows/amd64

Does this issue reproduce with the latest release?

Yes. It also happens to me on Go 1.15.4

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\lenni\AppData\Local\go-build
set GOENV=C:\Users\lenni\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\lenni\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\lenni\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\lenni\scoop\apps\go\current
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Users\lenni\scoop\apps\go\current\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\Programmieren\termios\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\lenni\AppData\Local\Temp\go-build2957794104=/tmp/go-build -gno-record-gcc-switches

What did you do?

I am currently writing a simple command-line editor in Go. For this, I want my program to be able to read every of the user's keypresses. For this, I think I have to set the Console Mode to only ENABLE_WINDOW_INPUT.

Here is my code (sorry for inline code, I think Go Playground is not really applicable):

  package main

  import "golang.org/x/sys/windows"

  var in windows.Handle
  var oldMode uint32

 func main() {
    err := SetRaw()
    if err != nil {
      panic(err)
    }

    buf := make([]uint16, 20)
    Read(buf)
    os.Stdout.Write([]byte("Read"))
    Close()
  }

  func SetRaw() error {
    var err error
    in, err = windows.Open("CONIN$", windows.O_RDWR, 0)
    if err != nil {
      return err
    }

    err = windows.GetConsoleMode(in, &oldMode)

    err = windows.SetConsoleMode(in, windows.ENABLE_WINDOW_INPUT)
    if err != nil {
      return err
    }

    return nil
  }

  // Read reads a single keypress
  func Read(p []uint16) (int, error) {
    //return windows.Read(in, p)
    var tmp_arg uint32
    var inputControl byte = 0
    err := windows.ReadConsole(in, &p[0], 1, &tmp_arg, &inputControl)
    return 0, err
  }

  func Close() {
    windows.SetConsoleMode(in, oldMode)
    windows.Close(in)
  }

What did you expect to see?

I'd expect this program to start, and close after I pressed an arrow key.

What did you see instead?

The program starts, but does only exit if I press a letter key (a-z, 0-9). Special keys as F1 through F12, the arrow keys or home/end/pgup/pgdown don't work.

If I set the console mode to ENABLE_WINDOW_INPUT | ENABLE_VIRTUAL_TERMINAL_INPUT instead, the behaviour is as expected, but then I don't have support on older Windows devices (7, 8, ...)

Thanks for any help in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0