keyboard

package module
v0.0.0-...-54c7737 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2021 License: MIT Imports: 13 Imported by: 0

README

Keyboard

Simple library to listen for keystrokes from the keyboard

The code is inspired by termbox-go library.

Installation

Install and update this go package with go get -u github.com/eiannone/keyboard

Usage

Example of getting a single keystroke:

char, _, err := keyboard.GetSingleKey()
if (err != nil) {
    panic(err)
}
fmt.Printf("You pressed: %q\r\n", char)

Example of getting a series of keystrokes with a blocking GetKey() function:

package main

import (
	"fmt"
	"github.com/eiannone/keyboard"
)

func main() {		
	if err := keyboard.Open(); err != nil {
		panic(err)
	}
	defer func() {
		_ = keyboard.Close()
	}()

	fmt.Println("Press ESC to quit")
	for {
		char, key, err := keyboard.GetKey()
		if err != nil {
			panic(err)
		}
		fmt.Printf("You pressed: rune %q, key %X\r\n", char, key)
        if key == keyboard.KeyEsc {
			break
		}
	}	
}

Example of getting a series of keystrokes using a channel:

package main

import (
	"fmt"
	"github.com/eiannone/keyboard"
)

func main() {
	keysEvents, err := keyboard.GetKeys(10)
	if err != nil {
		panic(err)
	}
	defer func() {
		_ = keyboard.Close()
	}()

	fmt.Println("Press ESC to quit")
	for {
		event := <-keysEvents
		if event.Err != nil {
			panic(event.Err)
		}
		fmt.Printf("You pressed: rune %q, key %X\r\n", event.Rune, event.Key)
		if event.Key == keyboard.KeyEsc {
			break
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() (err error)

Should be called after successful initialization when functionality isn't required anymore.

func GetKeys

func GetKeys(bufferSize int) (<-chan KeyEvent, error)

func IsStarted

func IsStarted(timeout time.Duration) bool

func Open

func Open() (err error)

Types

type Key

type Key uint16
const (
	KeyF1 Key = 0xFFFF - iota
	KeyF2
	KeyF3
	KeyF4
	KeyF5
	KeyF6
	KeyF7
	KeyF8
	KeyF9
	KeyF10
	KeyF11
	KeyF12
	KeyInsert
	KeyDelete
	KeyHome
	KeyEnd
	KeyPgup
	KeyPgdn
	KeyArrowUp
	KeyArrowDown
	KeyArrowLeft
	KeyArrowRight
)

Key constants, see GetKey() function.

const (
	KeyCtrlTilde Key = iota
	KeyCtrl2
	KeyCtrlSpace
	KeyCtrlA
	KeyCtrlB
	KeyCtrlC
	KeyCtrlD
	KeyCtrlE
	KeyCtrlF
	KeyCtrlG
	KeyBackspace
	KeyCtrlH
	KeyTab
	KeyCtrlI
	KeyCtrlJ
	KeyCtrlK
	KeyCtrlL
	KeyEnter
	KeyCtrlM
	KeyCtrlN
	KeyCtrlO
	KeyCtrlP
	KeyCtrlQ
	KeyCtrlR
	KeyCtrlS
	KeyCtrlT
	KeyCtrlU
	KeyCtrlV
	KeyCtrlW
	KeyCtrlX
	KeyCtrlY
	KeyCtrlZ
	KeyEsc
	KeyCtrlLsqBracket
	KeyCtrl3
	KeyCtrl4
	KeyCtrlBackslash
	KeyCtrl5
	KeyCtrlRsqBracket
	KeyCtrl6
	KeyCtrl7
	KeyCtrlSlash
	KeyCtrlUnderscore
	KeySpace
	KeyBackspace2
	KeyCtrl8
)

func GetKey

func GetKey() (rune, Key, error)

func GetSingleKey

func GetSingleKey() (ch rune, key Key, err error)

type KeyEvent

type KeyEvent struct {
	Key  Key   // One of Key* constants, invalid if 'Ch' is not 0
	Rune rune  // A unicode character
	Err  error // Error in case if input failed
}

Jump to

Keyboard shortcuts

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