Go-GetKeyPushed
This package implements OnKeyPress()
like functionality to the CLI app in Golang. (Very much powered by the awesome go-tty
)
It simply returns the key pushed from the console/terminal (TTY) without the enter
key.
$ # Run sample
$ go run ./example <enter>
Ready. Press any key ... (q = quit)
Key pressed => "a"
Key pressed => "b"
Key pressed => "A"
Key pressed => "B"
Key pressed => "あ"
Key pressed => "い"
Key pressed => "愛"
Key pressed => "\x1b" // ESC key
Key pressed => "\x1b[A" // Up arrow key
Key pressed => "\x1b[B" // Down arrow key
Key pressed => "\x1b[C" // Right arrow key
Key pressed => "\x1b[D" // Left arrow key
Key pressed => "\x1b[19~" // F8 key (if not assigned in the terminal)
Key pressed => "\x1b[20~" // F9 key (if not assigned in the terminal)
Key pressed => "\x1b[21~" // F10 key (if not assigned in the terminal)
Key pressed => "q"
Quit (q) detected. Exiting ...
$ # Interrupt with Ctrl+c
$ go run ./example <enter>
Ready. Press any key ... (q = quit)
Key pressed => "a"
Key pressed => "b"
Failed to get pressed key. Msg: user cancelled (ctrl+c, SIGINT detectd)
exit status 1
Install
go get github.com/KEINOS/go-getkeypushed
Usage Sample
// import "github.com/KEINOS/go-getkeypushed/key"
key := key.New() // Instantiate
timeWait := 5 // Timeout user input in 5 secs (0=no timeout)
keyDefault := "my default" // Default value when timeout
fmt.Println("Press any key:")
// The below returns the key pressed from the console/terminal
// or returns the default vaule if it timeouts.
inputUser, err := key.Get(timeWait, keyDefault)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get pressed key. Msg: %v\n", err)
os.Exit(1)
}
fmt.Println("Input: "+ inputUser)
package main
import (
"fmt"
"os"
"strings"
"github.com/KEINOS/go-getkeypushed/key"
)
func main() {
var (
char string
err error
keyQuit = "q"
keyDefault = "?" // A char to use when wait time exceeds
timeWait = 5 // Seconds to wait user input
)
keyInput := key.New()
fmt.Println("Ready. Press any key ... (q = quit)")
for {
char, err = keyInput.Get(keyDefault, timeWait)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get pressed key. Msg: %v\n", err)
os.Exit(1)
}
fmt.Printf("Key pressed => %#+v\n", char)
if char == keyQuit {
fmt.Printf("Quit (%s) detected. Exiting ...\n", keyQuit)
break
}
if strings.TrimSpace(char) == "" {
fmt.Println("Empty char(white space) detected.")
break
}
}
}
Notes
- Develop Online
- Compatible to GitHub Codespaces.
- Fork this repo and open it via CodeSpaces in your repo.
- Develop Locally
- No Docker + VSCode user
- With GitHub Codespaces extension, you can remotely access to cloud-hosted dev container in GitHub Cosespaces.
- Docker + VSCode user
- Compatible to VSCode's Remote-Containers extension.
- Fork and clone this repo and "Reopen in Container" via Remote-Containers
- Or clone the repo in container volume for one-time usage.
- No Docker + VSCode user
License & Copyright
-
MIT
- KEINOS and the contributors
-
go-tty
& go-isatty: Yasuhiro Matsumoto (a.k.a mattn) @ GitHub - See the packages in go.mod as well
TODO
-
Find out how to test the
tty
input -
Implement basic testing
- Cover test as much as possible
-
Wating time implementaion
- Wait Nth seconds and return default key if wait time exceed with no user interaction)