Send stdin keystrokes to channel without newline required

你说的曾经没有我的故事 提交于 2020-01-21 11:02:11

问题


I'd like to send the user's keystrokes to a channel directly after each individual keystroke is made to stdin.

I've attempted the code below, but this doesn't give the desired result because the reader.ReadByte() method blocks until newline is entered.

func chars() <-chan byte {
    ch := make(chan byte)
    reader := bufio.NewReader(os.Stdin)
    go func() {
        for {           
            char, err := reader.ReadByte()
            if err != nil {
                log.Fatal(err)
            }
            ch <- char
        }
    }()
    return ch
}

Thank you for any advice on how might I get each user input character to go immediately to the channel without the need for a newline character.


回答1:


Stdin is line-buffered by default. This means it will not yield any input to you, until a newline is encountered. This is not a Go specific thing.

Having it behave in a non-buffered way is highly platform specific. As Rami suggested, ncurses is a way to do it. Another option is the much lighter go-termbox package.

If you want to do it all manually (on Linux at least), you can look at writing C bindings for termios or do syscalls directly in Go.

How platforms like Windows handle this, I do not know. You can dig into the source code for ncurses or termbox to see how they did it.




回答2:


If you are on Linux, You might want to look at Goncurses package

http://code.google.com/p/goncurses/

It has the function: GetChar() which is what you want.



来源:https://stackoverflow.com/questions/12360597/send-stdin-keystrokes-to-channel-without-newline-required

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!