termfozgo.at/termfo/keys Index | Files

package keys

import "zgo.at/termfo/keys"

Index

Constants

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

Modifiers keys.

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

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

const CursesVersion = `ncurses-6.3.20221105`

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

Variables

var Keys = map[*caps.Cap]Key{
	caps.TableStrs[55]:  Backspace,
	caps.TableStrs[59]:  Delete,
	caps.TableStrs[61]:  Down,
	caps.TableStrs[66]:  F1,
	caps.TableStrs[67]:  F10,
	caps.TableStrs[68]:  F2,
	caps.TableStrs[69]:  F3,
	caps.TableStrs[70]:  F4,
	caps.TableStrs[71]:  F5,
	caps.TableStrs[72]:  F6,
	caps.TableStrs[73]:  F7,
	caps.TableStrs[74]:  F8,
	caps.TableStrs[75]:  F9,
	caps.TableStrs[76]:  Home,
	caps.TableStrs[77]:  Insert,
	caps.TableStrs[79]:  Left,
	caps.TableStrs[81]:  PageDown,
	caps.TableStrs[82]:  PageUp,
	caps.TableStrs[83]:  Right,
	caps.TableStrs[87]:  Up,
	caps.TableStrs[148]: BackTab,
	caps.TableStrs[164]: End,
	caps.TableStrs[165]: Enter,
	caps.TableStrs[172]: Next,
	caps.TableStrs[173]: Open,
	caps.TableStrs[174]: Options,
	caps.TableStrs[175]: Previous,
	caps.TableStrs[176]: Print,
	caps.TableStrs[177]: Redo,
	caps.TableStrs[178]: Reference,
	caps.TableStrs[179]: Refresh,
	caps.TableStrs[180]: Replace,
	caps.TableStrs[181]: Restart,
	caps.TableStrs[182]: Resume,
	caps.TableStrs[183]: Save,
	caps.TableStrs[184]: Suspend,
	caps.TableStrs[185]: Undo,
	caps.TableStrs[186]: Sbeg,
	caps.TableStrs[187]: Scancel,
	caps.TableStrs[188]: Scommand,
	caps.TableStrs[189]: Scopy,
	caps.TableStrs[190]: Screate,
	caps.TableStrs[191]: Sdc,
	caps.TableStrs[192]: Sdl,
	caps.TableStrs[193]: Select,
	caps.TableStrs[194]: Send,
	caps.TableStrs[195]: Seol,
	caps.TableStrs[196]: Sexit,
	caps.TableStrs[197]: Sfind,
	caps.TableStrs[198]: Shelp,
	caps.TableStrs[199]: Shome,
	caps.TableStrs[200]: Sic,
	caps.TableStrs[201]: Sleft,
	caps.TableStrs[202]: Smessage,
	caps.TableStrs[203]: Smove,
	caps.TableStrs[204]: Snext,
	caps.TableStrs[205]: Soptions,
	caps.TableStrs[206]: Sprevious,
	caps.TableStrs[207]: Sprint,
	caps.TableStrs[208]: Sredo,
	caps.TableStrs[209]: Sreplace,
	caps.TableStrs[210]: Sright,
	caps.TableStrs[211]: Srsume,
	caps.TableStrs[212]: Ssave,
	caps.TableStrs[213]: Ssuspend,
	caps.TableStrs[214]: Sundo,
	caps.TableStrs[216]: F11,
	caps.TableStrs[217]: F12,
	caps.TableStrs[355]: Mouse,
}

Keys maps caps.Cap to Key constants

Types

type Key

type Key uint64

Key represents a keypress. This is formatted as follows:

And the last three bits are flags for modifier keys:

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; the 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)

	Sredo
	Snext
	Backspace
	Scommand
	Shome
	End
	Options
	Save
	Smessage
	Smove
	Refresh
	Restart
	Scancel
	Seol
	Undo
	Ssave
	Sbeg
	F1
	F2
	F3
	F4
	F5
	F6
	Ssuspend
	F7
	F8
	F9
	Shelp
	Scopy
	Left
	Sexit
	Suspend
	Sic
	Right
	Resume
	Reference
	Sundo
	Sfind
	Enter
	Select
	Redo
	Previous
	Delete
	Sright
	Down
	F10
	F11
	F12
	Send
	Up
	Print
	Next
	Home
	Sreplace
	Sdc
	Sleft
	PageDown
	Insert
	Sprint
	Sdl
	Screate
	PageUp
	BackTab
	Replace
	Soptions
	Sprevious
	Srsume
	Mouse
	Open
)

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.

Source Files

key.go keys.go

Version
v0.0.0-20221111023549-ad7a16ef7b8d (latest)
Published
Nov 11, 2022
Platform
linux/amd64
Imports
4 packages
Last checked
5 days ago

Tools for package owners.