Version: v2.0.1 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2021 License: MIT


golangci-lint Go Reference


It simply gets a character of the key pushed from the console/terminal (TTY).

No need to enter the enter key. Some what like OnKeyPress() functionality. It is very much powered by go-tty.

$ go run ./examples/main.go <enter>
Ready. Press any key ...
Key pressed => "a"
Key pressed => "b"
Key pressed => "A"
Key pressed => "B"
Key pressed => "あ"
Key pressed => "い"
Key pressed => "\x1b" // Up arrow
Key pressed => "\x1b" // Down arrow
Key pressed => "q"
Quit (q) key detected. Exiting ...


go get github.com/KEINOS/go-getkeypushed
package main

import (

    gkp "github.com/KEINOS/go-getkeypushed"

func main() {
    var (
        timeWait int = 10       // Wait 10 seconds ( 0 = wait for ever)
        keyDefault string = "q" // Key to use when wait time exceeds

    fmt.Println("Ready. Press any key ...")

    for {
        char, err = gkp.GetKeyPushed(keyDefault, timeWait)

        if err != nil {
            fmt.Fprintf(os.Stderr, "Failed to get key. ErrMsg: %v\n", err)

        if char == "q" {
            fmt.Printf("Key pressed => %#+v\n", char)
            fmt.Println("Quit (q) key detected. Exiting ...")

        fmt.Printf("Key pressed => %#+v\n", char)


  • Suitable for just getting keypress such as y, n. But not suitable for capturing keys like F1 nor SIGINT (Ctrl+C).
  • It is a wrapper of amazing package "github.com/mattn/go-tty" to simplify its usage for my other projects.


  • Find out how to test the tty input
  • Implement basic testing
  • Wating time implementaion
    • Wait Nth seconds and return default key if wait time exceed with no user interaction) (Since v2.0)


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL