terminput

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2019 License: MIT Imports: 3 Imported by: 25

README

Go Terminput

Package terminput provides terminal keyboard input for interactive command-line tools.

Example

package main

import (
	"fmt"
	"log"

	"github.com/pkg/term"
	"github.com/tj/go-terminput"
)

func main() {
	t, err := term.Open("/dev/tty")
	if err != nil {
		log.Fatalf("error: %s\n", err)
	}

	t.SetRaw()
	defer t.Restore()

	fmt.Printf("Type something, use 'q' to exit.\r\n")

	for {
		e, err := terminput.Read(t)
		if err != nil {
			log.Fatalf("error: %s\n", err)
		}

		if e.Key() == terminput.KeyEscape || e.Rune() == 'q' {
			break
		}

		fmt.Printf("%s — shift=%v ctrl=%v alt=%v meta=%v\r\n", e.String(), e.Shift(), e.Ctrl(), e.Alt(), e.Meta())
	}
}

GoDoc

Sponsors

This project is sponsored by CTO.ai, making it easy for development teams to create and share workflow automations without leaving the command line.

Documentation

Overview

Package terminput provides terminal keyboard input for interactive command-line tools.

Note that this package does not enable raw mode, use github.com/pkg/term or similar.

Index

Constants

View Source
const (
	KeyBackspace = KeyBS
	KeyTab       = KeyTAB
	KeyEscape    = KeyESC
	KeyEnter     = KeyCR
)

Aliases

Variables

This section is empty.

Functions

This section is empty.

Types

type Key

type Key rune

Key represents the key pressed.

const (
	KeyNUL Key = iota
	KeySOH
	KeySTX
	KeyETX
	KeyEOT
	KeyENQ
	KeyACK
	KeyBEL
	KeyBS
	KeyTAB
	KeyLF
	KeyVT
	KeyFF
	KeyCR
	KeySO
	KeySI
	KeyDLE
	KeyDC1
	KeyDC2
	KeyDC3
	KeyDC4
	KeyNAK
	KeySYN
	KeyETB
	KeyCAN
	KeyEM
	KeySUB
	KeyESC
	KeyFS
	KeyGS
	KeyRS
	KeyUS

	// others
	KeyRune
	KeyUp
	KeyDown
	KeyRight
	KeyLeft
	KeyInsert
	KeyBacktab
	KeyDelete
	KeyHome
	KeyEnd
	KeyPgUp
	KeyPgDn
	KeyF1
	KeyF2
	KeyF3
	KeyF4
	KeyF5
	KeyF6
	KeyF7
	KeyF8
	KeyF9
	KeyF10
	KeyF11
	KeyF12
	KeyF13
	KeyF14
	KeyF15
	KeyF16
	KeyF17
	KeyF18
	KeyF19
	KeyF20
)

Keys supported.

type KeyboardInput

type KeyboardInput struct {
	// contains filtered or unexported fields
}

KeyboardInput represents a key press.

func Read

func Read(r io.Reader) (*KeyboardInput, error)

Read terminal input from the given reader.

func (*KeyboardInput) Alt

func (k *KeyboardInput) Alt() bool

Alt returns true if alt was pressed.

func (*KeyboardInput) Ctrl

func (k *KeyboardInput) Ctrl() bool

Ctrl returns true if ctrl was pressed.

func (*KeyboardInput) Key

func (k *KeyboardInput) Key() Key

Key returns the key pressed. In the case that the key returned is KeyRune, the literal value can be accessed via Rune().

func (*KeyboardInput) Meta

func (k *KeyboardInput) Meta() bool

Meta returns true if meta was pressed.

func (*KeyboardInput) Mod

func (k *KeyboardInput) Mod() Mod

Mod returns the modifier flags.

func (*KeyboardInput) Rune

func (k *KeyboardInput) Rune() rune

Rune returns the rune corresponding to the key press. This value is only defined when Key() is KeyRune.

func (*KeyboardInput) Shift

func (k *KeyboardInput) Shift() bool

Shift returns true if shift was pressed.

func (*KeyboardInput) String

func (k *KeyboardInput) String() string

String returns a human-friendly string representing the key.

type Mod

type Mod int

Mod represents a key modifier such as pressing alt or ctrl.

const (
	ModNone Mod = 1 << iota
	ModMeta
	ModCtrl
	ModAlt
	ModShift
)

Mod flags available.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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