getch

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2022 License: CC0-1.0 Imports: 13 Imported by: 1

Documentation

Overview

Package getch implements a platform agnostic character-wise input capture.

Things which work on all platforms:

- Detection of special keys F1-F12, Esc, Enter, Cursor keys, etc. - Key combinations: CTRL+<Letter>, Shift+<Letter> - Normal character input via KeyEvent.Rune

Example code:

func main() {
	var err error
	var e *getch.KeyEvent

	if err = getch.Start(); err != nil {
		fmt.Println(err)
		return
	}
	defer getch.Stop()

	for e == nil || e.Code != getch.KeyTab {
		e, err = getch.Getch()

		fmt.Println("==>", e, err)
	}
}

Index

Constants

View Source
const (
	KeyF1  = "Key_F1"
	KeyF2  = "Key_F2"
	KeyF3  = "Key_F3"
	KeyF4  = "Key_F4"
	KeyF5  = "Key_F5"
	KeyF6  = "Key_F6"
	KeyF7  = "Key_F7"
	KeyF8  = "Key_F8"
	KeyF9  = "Key_F9"
	KeyF10 = "Key_F10"
	KeyF11 = "Key_F11"
	KeyF12 = "Key_F12"

	KeyEnter      = "Key_ENTER"
	KeyBackspace  = "Key_BACKSPACE"
	KeyEsc        = "Key_ESC"
	KeyTab        = "Key_TAB"
	KeyInsert     = "Key_INSERT"
	KeyDelete     = "Key_DELETE"
	KeyHome       = "Key_HOME"
	KeyEnd        = "Key_END"
	KeyPgup       = "Key_PAGE_UP"
	KeyPgdn       = "Key_PAGE_DOWN"
	KeyArrowUp    = "Key_ARROW_UP"
	KeyArrowDown  = "Key_ARROW_DOWN"
	KeyArrowLeft  = "Key_ARROW_LEFT"
	KeyArrowRight = "Key_ARROW_RIGHT"
	KeyCommand    = "Key_CMD" // "Windows" key

	KeyA = "Key_A"
	KeyB = "Key_B"
	KeyC = "Key_C"
	KeyD = "Key_D"
	KeyE = "Key_E"
	KeyF = "Key_F"
	KeyG = "Key_G"
	KeyH = "Key_H"
	KeyI = "Key_I"
	KeyJ = "Key_J"
	KeyK = "Key_K"
	KeyL = "Key_L"
	KeyM = "Key_M"
	KeyN = "Key_N"
	KeyO = "Key_O"
	KeyP = "Key_P"
	KeyQ = "Key_Q"
	KeyR = "Key_R"
	KeyS = "Key_S"
	KeyT = "Key_T"
	KeyU = "Key_U"
	KeyV = "Key_V"
	KeyW = "Key_W"
	KeyX = "Key_X"
	KeyY = "Key_Y"
	KeyZ = "Key_Z"

	Key1 = "Key_1"
	Key2 = "Key_2"
	Key3 = "Key_3"
	Key4 = "Key_4"
	Key5 = "Key_5"
	Key6 = "Key_6"
	Key7 = "Key_7"
	Key8 = "Key_8"
	Key9 = "Key_9"
	Key0 = "Key_0"

	KeyBacktick     = "Key_BACKTICK"
	KeyMinus        = "Key_MINUS"
	KeyEqual        = "Key_EQUAL"
	KeyBracketOpen  = "Key_BRACKET_OPEN"
	KeyBracketClose = "Key_BRACKET_CLOSE"
	KeySemiColon    = "Key_SEMICOLON"
	KeyQuote        = "Key_QUOTE"
	KeyHash         = "Key_HASH"
	KeyBackslash    = "Key_BACKSLASH"
	KeyComma        = "Key_COMMA"
	KeyDot          = "Key_DOT"
	KeySlash        = "Key_SLASH"

	KeyUnknown = "Key_UNKNOWN"
)

Key codes for the KeyEvent object

Variables

View Source
var (
	DefaultTermMappings = []struct {
		name string
		keys []string
	}{
		{"Eterm", etermKeys},
		{"screen", screenKeys},
		{"xterm", xtermKeys},
		{"xterm-256color", xtermKeys},
		{"rxvt-unicode", rxvtKeys},
		{"rxvt-256color", rxvtKeys},
		{"linux", linuxKeys},
	}

	CompatibilityTermMappings = []struct {
		namePart string
		keys     []string
	}{
		{"xterm", xtermKeys},
		{"rxvt", rxvtKeys},
		{"linux", linuxKeys},
		{"Eterm", etermKeys},
		{"screen", screenKeys},
		{"cygwin", xtermKeys},
		{"st", xtermKeys},
	}
)

Mappings from terminal type to escape sequence for a particular termKey (see above).

View Source
var ErrNotImplemented = errors.New("Not implemented")

ErrNotImplemented is returned if the platform is not supported by getch

Functions

func CursorPosition

func CursorPosition() (int, int, error)

CursorPosition returns the current cursor position.

func ReadLine

func ReadLine(echo io.Writer, mask rune) (string, error)

ReadLine reads a single line from the terminal. Can optionally include an echo writer. If the mask is not 0 then the echo will be the mask character.

func SetCursorPosition

func SetCursorPosition(x, y int) error

SetCursorPosition sets the current cursor position.

func Start

func Start() error

Start starts the character reader.

func Stop

func Stop()

Stop stops the character reader.

Types

type ErrUnknownEscapeSequence

type ErrUnknownEscapeSequence struct {
	Detail []byte
}

ErrUnknownEscapeSequence is returned if an unknown escape sequence was retrieved. The sequence in question is located in the Detail byte slice.

func (*ErrUnknownEscapeSequence) Error

func (e *ErrUnknownEscapeSequence) Error() string

Error returns a string representation of this error.

type KeyCode

type KeyCode string

KeyCode is a platform-independent key code

type KeyEvent

type KeyEvent struct {
	Code   KeyCode // Code of the pressed key
	Ctrl   bool    // Flag if the ctrl key is also pressed
	Alt    bool    // Flag if the alt key is also pressed
	Shift  bool    // Flag if the shift key is also pressed
	Rune   rune    // Produced rune if the key is printable
	RawBuf []byte  // Raw input buffer since the last key event
}

KeyEvent objects are produced by an input reader.

func Getch

func Getch() (*KeyEvent, error)

Getch reads a single character.

func (*KeyEvent) String

func (k *KeyEvent) String() string

Jump to

Keyboard shortcuts

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