keyboard

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package keyboard provides raw keyboard input handling with escape sequence parsing. It handles VT100/ANSI escape sequences, UTF-8 characters, bracketed paste, and line assembly for terminal input.

Index

Constants

View Source
const DefaultPasteChunkSize = 1024

DefaultPasteChunkSize is the default size for paste chunks (1KB)

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler added in v0.2.1

type Handler struct {

	// Output channels (plain Go channels)
	Keys  chan string // Parsed key events ("a", "M-a", "F1", etc.)
	Lines chan []byte // Assembled lines

	// Callbacks (optional, called in addition to channel sends)
	OnKey        func(key string)       // Called on each key event
	OnLine       func(line []byte)      // Called on each completed line
	OnPaste      func(content []byte)   // Called on bracketed paste content (complete)
	OnPasteChunk func(chunk PasteChunk) // Called on incremental paste chunks
	// contains filtered or unexported fields
}

Handler handles raw keyboard input, parsing escape sequences and providing both key events and line assembly.

func New added in v0.2.1

func New(opts Options) *Handler

New creates a new keyboard Handler.

func (*Handler) DecodeMacOSOption added in v0.3.2

func (h *Handler) DecodeMacOSOption() bool

DecodeMacOSOption returns true if macOS Option character decoding is enabled.

func (*Handler) IsLineMode added in v0.2.1

func (h *Handler) IsLineMode() bool

IsLineMode returns true if line assembly mode is active.

func (*Handler) IsRunning added in v0.2.1

func (h *Handler) IsRunning() bool

IsRunning returns true if the handler is currently running.

func (*Handler) ManagesTerminal added in v0.2.1

func (h *Handler) ManagesTerminal() bool

ManagesTerminal returns true if this handler is managing terminal raw mode.

func (*Handler) SetDecodeMacOSOption added in v0.3.2

func (h *Handler) SetDecodeMacOSOption(enabled bool)

SetDecodeMacOSOption enables or disables decoding of macOS Option+key Unicode characters to M-key notation (e.g., ∂ → M-d).

func (*Handler) SetEchoWriter added in v0.2.1

func (h *Handler) SetEchoWriter(w io.Writer)

SetEchoWriter sets the writer for echoing typed characters.

func (*Handler) SetLineMode added in v0.2.1

func (h *Handler) SetLineMode(enabled bool)

SetLineMode enables or disables line assembly mode. When enabled, keys go to line assembly and completed lines are sent to Lines channel. When disabled, all keys go directly to Keys channel.

func (*Handler) Start added in v0.2.1

func (h *Handler) Start() error

Start begins reading from input and processing keys.

func (*Handler) Stop added in v0.2.1

func (h *Handler) Stop() error

Stop stops reading and restores terminal state.

type Options added in v0.2.1

type Options struct {
	// InputReader is the source of raw bytes (required)
	InputReader io.Reader

	// EchoWriter is where to echo typed characters during line mode (optional)
	EchoWriter io.Writer

	// KeyBufferSize is the size of the Keys channel buffer (default: 64)
	KeyBufferSize int

	// LineBufferSize is the size of the Lines channel buffer (default: 16)
	LineBufferSize int

	// PasteChunkSize is the size of chunks emitted during bracketed paste (default: 1024)
	// Only used when OnPasteChunk callback is set
	PasteChunkSize int

	// DecodeMacOSOption enables decoding of macOS Option+key Unicode characters
	// to M-key notation (e.g., ∂ → M-d, Ø → M-O). Default: true on Darwin, false otherwise
	DecodeMacOSOption *bool

	// DebugFn is called with debug messages (optional)
	DebugFn func(string)

	// ManageTerminal controls whether to put stdin in raw mode.
	// Only applies if InputReader is os.Stdin and is a terminal.
	// Default: true
	ManageTerminal *bool
}

Options configures the Handler

type PasteChunk added in v0.3.1

type PasteChunk struct {
	Content []byte // The chunk content
	IsFinal bool   // True if this is the final chunk
}

PasteChunk represents an incremental chunk of bracketed paste content

Jump to

Keyboard shortcuts

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