keys

package
v0.0.0-...-df5e07d Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Shift = 1 << 63
	Ctrl  = 1 << 62
	Alt   = 1 << 61
)

Modifiers keys.

View Source
const (
	Tab    = 'i' | Ctrl
	Escape = '[' | Ctrl
)

Common control sequences better known by their name than letter+Ctrl combination.

View Source
const CursesVersion = `ncurses-6.5.20240511`

CursesVersion is the version of curses this data was generated with, as [implementation]-[version].

Variables

Keys maps caps.Cap to Key constants

Functions

This section is empty.

Types

type Key

type Key uint64

Key represents a keypress. This is formatted as follows:

  • First 32 bits → rune (int32)
  • Next 16 bits → Named key constant.
  • Bits 49-61 → Currently unused.
  • Bit 62 → Alt
  • Bit 63 → Ctrl
  • Bit 64 → Shift

The upshot of this is that you can now use a single value to test for all combinations:

switch Key(0x61) {
case 'a':                           // 'a' w/o modifiers
case 'a' | keys.Ctrl:               // 'a' with control
case 'a' | keys.Ctrl | keys.Shift:  // 'a' with shift and control

case keys.Up:                       // Arrow up
case keys.Up | keys.Ctrl:           // Arrow up with control
}

Which is nicer than using two or three different variables to signal various things.

Letters are always in lower-case; use keys.Shift to test for upper case.

Control sequences (0x00-0x1f, 0x1f), are used sent as key + Ctrl. So this:

switch k {
case 0x09:
}

Won't work. you need to use:

switch k {
case 'i' | key.Ctrl:
}

Or better yet:

ti := termfo.New("")

...

switch k {
case ti.Keys[keys.Tab]:
}

This just makes more sense, because people press "<C-a>" not "Start of heading".

It's better to use the variables from the terminfo, in case it's something different. Especially with things like Shift and Ctrl modifiers this can differ.

Note that support for multiple modifier keys is flaky across terminals.

const (
	// Special key used to signal errors.
	UnknownSequence Key = iota + (1 << 32)

	ShiftLeft
	PageUp
	Insert
	ShiftInsert
	Up
	Right
	F1
	ShiftRight
	F2
	ShiftHome
	F3
	Delete
	PageDown
	F4
	F10
	F11
	ShiftDelete
	F5
	Down
	Mouse
	F12
	ShiftEnd
	F6
	Enter
	Left
	F7
	End
	F8
	F9
	Backspace
	BackTab
	Home
)

List of all key sequences we know about. This excludes most obscure ones not present on modern devices.

func (Key) Alt

func (k Key) Alt() bool

Alt reports if the Alt modifier is set.

func (Key) Ctrl

func (k Key) Ctrl() bool

Ctrl reports if the Ctrl modifier is set.

func (Key) Name

func (k Key) Name() string

Name gets the key name. This doesn't show if any modifiers are set; use String() for that.

func (Key) Named

func (k Key) Named() bool

Named reports if this is a named key.

func (Key) Shift

func (k Key) Shift() bool

Shift reports if the Shift modifier is set.

func (Key) String

func (k Key) String() string

func (Key) Valid

func (k Key) Valid() bool

Valid reports if this key is valid.

func (Key) WithoutMods

func (k Key) WithoutMods() Key

WithoutMods returns a copy of k without any modifier keys set.

Jump to

Keyboard shortcuts

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