cbind

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2020 License: MIT Imports: 6 Imported by: 0

README

cbind

GoDoc CI status Donate

Key event handling library for tcell

Features

  • Set KeyEvent handlers
  • Encode and decode KeyEvents as human-readable strings

Usage

// Create a new input configuration to store the key bindings.
c := NewConfiguration()

handleSave := func(ev *tcell.EventKey) *tcell.EventKey {
    // Save
    return nil
}

handleOpen := func(ev *tcell.EventKey) *tcell.EventKey {
    // Open
    return nil
}

handleExit := func(ev *tcell.EventKey) *tcell.EventKey {
    // Exit
    return nil
}

// Bind Alt+s.
if err := c.Set("Alt+s", handleSave); err != nil {
    log.Fatalf("failed to set keybind: %s", err)
}

// Bind Alt+o.
c.SetRune(tcell.ModAlt, 'o', handleOpen)

// Bind Escape.
c.SetKey(tcell.ModNone, tcell.KeyEscape, handleExit)

// Capture input. This will differ based on the framework in use (if any).
// When using tview or cview, call Application.SetInputCapture before calling
// Application.Run.
app.SetInputCapture(c.Capture)

Documentation

Documentation is available via gdooc.

The utility program whichkeybind may be used to determine and validate key combinations.

go get gitlab.com/tslocum/cbind/whichkeybind

Support

Please share issues and suggestions here.

Documentation

Overview

Package cbind provides tcell key event encoding, decoding and handling.

The NewConfiguration example demonstrates how to use cbind.

There are some limitations on reading keyboard input, which is explained in the tcell.EventKey documentation:

https://godoc.org/github.com/gdamore/tcell#EventKey

Index

Examples

Constants

View Source
const (
	LabelCtrl  = "ctrl"
	LabelAlt   = "alt"
	LabelMeta  = "meta"
	LabelShift = "shift"
)

Modifier labels

Variables

View Source
var ErrInvalidKeyEvent = errors.New("invalid key event")

ErrInvalidKeyEvent is the error returned when encoding or decoding a key event fails.

View Source
var UnifyEnterKeys = true

UnifyEnterKeys is a flag that determines whether or not KPEnter (keypad enter) key events are interpreted as Enter key events. When enabled, Ctrl+J key events are also interpreted as Enter key events.

Functions

func Decode

func Decode(s string) (mod tcell.ModMask, key tcell.Key, ch rune, err error)

Decode decodes a string as a key or combination of keys.

func Encode

func Encode(mod tcell.ModMask, key tcell.Key, ch rune) (string, error)

Encode encodes a key or combination of keys a string.

Types

type Configuration

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

Configuration processes key events by mapping keys to event handlers.

func NewConfiguration

func NewConfiguration() *Configuration

NewConfiguration returns a new input configuration.

Example

Example of creating and using an input configuration.

// Create a new input configuration to store the key bindings.
c := NewConfiguration()

handleSave := func(ev *tcell.EventKey) *tcell.EventKey {
	// Save
	return nil
}

handleOpen := func(ev *tcell.EventKey) *tcell.EventKey {
	// Open
	return nil
}

handleExit := func(ev *tcell.EventKey) *tcell.EventKey {
	// Exit
	return nil
}

// Bind Alt+s.
if err := c.Set("Alt+s", handleSave); err != nil {
	log.Fatalf("failed to set keybind: %s", err)
}

// Bind Alt+o.
c.SetRune(tcell.ModAlt, 'o', handleOpen)

// Bind Escape.
c.SetKey(tcell.ModNone, tcell.KeyEscape, handleExit)

// Capture input. This will differ based on the framework in use (if any).
// When using tview or cview, call Application.SetInputCapture before calling
// Application.Run.
// app.SetInputCapture(c.Capture)
Output:

func (*Configuration) Capture

func (c *Configuration) Capture(ev *tcell.EventKey) *tcell.EventKey

Capture handles key events.

func (*Configuration) Clear added in v0.1.2

func (c *Configuration) Clear()

Clear removes all handlers.

func (*Configuration) Set added in v0.1.3

func (c *Configuration) Set(s string, handler func(ev *tcell.EventKey) *tcell.EventKey) error

Set sets the handler for a key event string.

func (*Configuration) SetKey

func (c *Configuration) SetKey(mod tcell.ModMask, key tcell.Key, handler func(ev *tcell.EventKey) *tcell.EventKey)

SetKey sets the handler for a key.

func (*Configuration) SetRune

func (c *Configuration) SetRune(mod tcell.ModMask, ch rune, handler func(ev *tcell.EventKey) *tcell.EventKey)

SetRune sets the handler for a rune.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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