Skip to content
Myr

key.go

This file

Path

key.go

371 lines · 10.0 KiB · Go

Last changed by

fix(key): map media record to ultraviolet code (#1757)Ricardo-M-Lbefore adoptiondc4b017

History of this file

Take it

1package tea23import (4	"fmt"56	uv "github.com/charmbracelet/ultraviolet"7)89const (10	// KeyExtended is a special key code used to signify that a key event11	// contains multiple runes.12	KeyExtended = uv.KeyExtended13)1415// Special key symbols.16const (1718	// Special keys.1920	KeyUp     = uv.KeyUp21	KeyDown   = uv.KeyDown22	KeyRight  = uv.KeyRight23	KeyLeft   = uv.KeyLeft24	KeyBegin  = uv.KeyBegin25	KeyFind   = uv.KeyFind26	KeyInsert = uv.KeyInsert27	KeyDelete = uv.KeyDelete28	KeySelect = uv.KeySelect29	KeyPgUp   = uv.KeyPgUp30	KeyPgDown = uv.KeyPgDown31	KeyHome   = uv.KeyHome32	KeyEnd    = uv.KeyEnd3334	// Keypad keys.3536	KeyKpEnter    = uv.KeyKpEnter37	KeyKpEqual    = uv.KeyKpEqual38	KeyKpMultiply = uv.KeyKpMultiply39	KeyKpPlus     = uv.KeyKpPlus40	KeyKpComma    = uv.KeyKpComma41	KeyKpMinus    = uv.KeyKpMinus42	KeyKpDecimal  = uv.KeyKpDecimal43	KeyKpDivide   = uv.KeyKpDivide44	KeyKp0        = uv.KeyKp045	KeyKp1        = uv.KeyKp146	KeyKp2        = uv.KeyKp247	KeyKp3        = uv.KeyKp348	KeyKp4        = uv.KeyKp449	KeyKp5        = uv.KeyKp550	KeyKp6        = uv.KeyKp651	KeyKp7        = uv.KeyKp752	KeyKp8        = uv.KeyKp853	KeyKp9        = uv.KeyKp95455	// The following are keys defined in the Kitty keyboard protocol.56	// XXX: Investigate the names of these keys.57	KeyKpSep    = uv.KeyKpSep58	KeyKpUp     = uv.KeyKpUp59	KeyKpDown   = uv.KeyKpDown60	KeyKpLeft   = uv.KeyKpLeft61	KeyKpRight  = uv.KeyKpRight62	KeyKpPgUp   = uv.KeyKpPgUp63	KeyKpPgDown = uv.KeyKpPgDown64	KeyKpHome   = uv.KeyKpHome65	KeyKpEnd    = uv.KeyKpEnd66	KeyKpInsert = uv.KeyKpInsert67	KeyKpDelete = uv.KeyKpDelete68	KeyKpBegin  = uv.KeyKpBegin6970	// Function keys.7172	KeyF1  = uv.KeyF173	KeyF2  = uv.KeyF274	KeyF3  = uv.KeyF375	KeyF4  = uv.KeyF476	KeyF5  = uv.KeyF577	KeyF6  = uv.KeyF678	KeyF7  = uv.KeyF779	KeyF8  = uv.KeyF880	KeyF9  = uv.KeyF981	KeyF10 = uv.KeyF1082	KeyF11 = uv.KeyF1183	KeyF12 = uv.KeyF1284	KeyF13 = uv.KeyF1385	KeyF14 = uv.KeyF1486	KeyF15 = uv.KeyF1587	KeyF16 = uv.KeyF1688	KeyF17 = uv.KeyF1789	KeyF18 = uv.KeyF1890	KeyF19 = uv.KeyF1991	KeyF20 = uv.KeyF2092	KeyF21 = uv.KeyF2193	KeyF22 = uv.KeyF2294	KeyF23 = uv.KeyF2395	KeyF24 = uv.KeyF2496	KeyF25 = uv.KeyF2597	KeyF26 = uv.KeyF2698	KeyF27 = uv.KeyF2799	KeyF28 = uv.KeyF28100	KeyF29 = uv.KeyF29101	KeyF30 = uv.KeyF30102	KeyF31 = uv.KeyF31103	KeyF32 = uv.KeyF32104	KeyF33 = uv.KeyF33105	KeyF34 = uv.KeyF34106	KeyF35 = uv.KeyF35107	KeyF36 = uv.KeyF36108	KeyF37 = uv.KeyF37109	KeyF38 = uv.KeyF38110	KeyF39 = uv.KeyF39111	KeyF40 = uv.KeyF40112	KeyF41 = uv.KeyF41113	KeyF42 = uv.KeyF42114	KeyF43 = uv.KeyF43115	KeyF44 = uv.KeyF44116	KeyF45 = uv.KeyF45117	KeyF46 = uv.KeyF46118	KeyF47 = uv.KeyF47119	KeyF48 = uv.KeyF48120	KeyF49 = uv.KeyF49121	KeyF50 = uv.KeyF50122	KeyF51 = uv.KeyF51123	KeyF52 = uv.KeyF52124	KeyF53 = uv.KeyF53125	KeyF54 = uv.KeyF54126	KeyF55 = uv.KeyF55127	KeyF56 = uv.KeyF56128	KeyF57 = uv.KeyF57129	KeyF58 = uv.KeyF58130	KeyF59 = uv.KeyF59131	KeyF60 = uv.KeyF60132	KeyF61 = uv.KeyF61133	KeyF62 = uv.KeyF62134	KeyF63 = uv.KeyF63135136	// The following are keys defined in the Kitty keyboard protocol.137	// XXX: Investigate the names of these keys.138139	KeyCapsLock    = uv.KeyCapsLock140	KeyScrollLock  = uv.KeyScrollLock141	KeyNumLock     = uv.KeyNumLock142	KeyPrintScreen = uv.KeyPrintScreen143	KeyPause       = uv.KeyPause144	KeyMenu        = uv.KeyMenu145146	KeyMediaPlay        = uv.KeyMediaPlay147	KeyMediaPause       = uv.KeyMediaPause148	KeyMediaPlayPause   = uv.KeyMediaPlayPause149	KeyMediaReverse     = uv.KeyMediaReverse150	KeyMediaStop        = uv.KeyMediaStop151	KeyMediaFastForward = uv.KeyMediaFastForward152	KeyMediaRewind      = uv.KeyMediaRewind153	KeyMediaNext        = uv.KeyMediaNext154	KeyMediaPrev        = uv.KeyMediaPrev155	KeyMediaRecord      = uv.KeyMediaRecord156157	KeyLowerVol = uv.KeyLowerVol158	KeyRaiseVol = uv.KeyRaiseVol159	KeyMute     = uv.KeyMute160161	KeyLeftShift      = uv.KeyLeftShift162	KeyLeftAlt        = uv.KeyLeftAlt163	KeyLeftCtrl       = uv.KeyLeftCtrl164	KeyLeftSuper      = uv.KeyLeftSuper165	KeyLeftHyper      = uv.KeyLeftHyper166	KeyLeftMeta       = uv.KeyLeftMeta167	KeyRightShift     = uv.KeyRightShift168	KeyRightAlt       = uv.KeyRightAlt169	KeyRightCtrl      = uv.KeyRightCtrl170	KeyRightSuper     = uv.KeyRightSuper171	KeyRightHyper     = uv.KeyRightHyper172	KeyRightMeta      = uv.KeyRightMeta173	KeyIsoLevel3Shift = uv.KeyIsoLevel3Shift174	KeyIsoLevel5Shift = uv.KeyIsoLevel5Shift175176	// Special names in C0.177178	KeyBackspace = uv.KeyBackspace179	KeyTab       = uv.KeyTab180	KeyEnter     = uv.KeyEnter181	KeyReturn    = uv.KeyReturn182	KeyEscape    = uv.KeyEscape183	KeyEsc       = uv.KeyEsc184185	// Special names in G0.186187	KeySpace = uv.KeySpace188)189190// KeyPressMsg represents a key press message.191type KeyPressMsg Key192193// String implements [fmt.Stringer] and is quite useful for matching key194// events. For details, on what this returns see [Key.String].195func (k KeyPressMsg) String() string {196	return Key(k).String()197}198199// Keystroke returns the keystroke representation of the [Key]. While less type200// safe than looking at the individual fields, it will usually be more201// convenient and readable to use this method when matching against keys.202//203// Note that modifier keys are always printed in the following order:204//   - ctrl205//   - alt206//   - shift207//   - meta208//   - hyper209//   - super210//211// For example, you'll always see "ctrl+shift+alt+a" and never212// "shift+ctrl+alt+a".213func (k KeyPressMsg) Keystroke() string {214	return uv.Key(k).Keystroke()215}216217// Key returns the underlying key event. This is a syntactic sugar for casting218// the key event to a [Key].219func (k KeyPressMsg) Key() Key {220	return Key(k)221}222223// KeyReleaseMsg represents a key release message.224type KeyReleaseMsg Key225226// String implements [fmt.Stringer] and is quite useful for matching key227// events. For details, on what this returns see [Key.String].228func (k KeyReleaseMsg) String() string {229	return Key(k).String()230}231232// Keystroke returns the keystroke representation of the [Key]. While less type233// safe than looking at the individual fields, it will usually be more234// convenient and readable to use this method when matching against keys.235//236// Note that modifier keys are always printed in the following order:237//   - ctrl238//   - alt239//   - shift240//   - meta241//   - hyper242//   - super243//244// For example, you'll always see "ctrl+shift+alt+a" and never245// "shift+ctrl+alt+a".246func (k KeyReleaseMsg) Keystroke() string {247	return uv.Key(k).Keystroke()248}249250// Key returns the underlying key event. This is a convenience method and251// syntactic sugar to satisfy the [KeyMsg] interface, and cast the key event to252// [Key].253func (k KeyReleaseMsg) Key() Key {254	return Key(k)255}256257// KeyMsg represents a key event. This can be either a key press or a key258// release event.259type KeyMsg interface {260	fmt.Stringer261262	// Key returns the underlying key event.263	Key() Key264}265266// Key represents a Key press or release event. It contains information about267// the Key pressed, like the runes, the type of Key, and the modifiers pressed.268// There are a couple general patterns you could use to check for key presses269// or releases:270//271//	// Switch on the string representation of the key (shorter)272//	switch msg := msg.(type) {273//	case KeyPressMsg:274//	    switch msg.String() {275//	    case "enter":276//	        fmt.Println("you pressed enter!")277//	    case "a":278//	        fmt.Println("you pressed a!")279//	    }280//	}281//282//	// Switch on the key type (more foolproof)283//	switch msg := msg.(type) {284//	case KeyMsg:285//	    // catch both KeyPressMsg and KeyReleaseMsg286//	    switch key := msg.Key(); key.Code {287//	    case KeyEnter:288//	        fmt.Println("you pressed enter!")289//	    default:290//	        switch key.Text {291//	        case "a":292//	            fmt.Println("you pressed a!")293//	        }294//	    }295//	}296//297// Note that [Key.Text] will be empty for special keys like [KeyEnter],298// [KeyTab], and for keys that don't represent printable characters like key299// combos with modifier keys. In other words, [Key.Text] is populated only for300// keys that represent printable characters shifted or unshifted (like 'a',301// 'A', '1', '!', etc.).302type Key struct {303	// Text contains the actual characters received. This usually the same as304	// [Key.Code]. When [Key.Text] is non-empty, it indicates that the key305	// pressed represents printable character(s).306	Text string307308	// Mod represents modifier keys, like [ModCtrl], [ModAlt], and so on.309	Mod KeyMod310311	// Code represents the key pressed. This is usually a special key like312	// [KeyTab], [KeyEnter], [KeyF1], or a printable character like 'a'.313	Code rune314315	// ShiftedCode is the actual, shifted key pressed by the user. For example,316	// if the user presses shift+a, or caps lock is on, [Key.ShiftedCode] will317	// be 'A' and [Key.Code] will be 'a'.318	//319	// In the case of non-latin keyboards, like Arabic, [Key.ShiftedCode] is the320	// unshifted key on the keyboard.321	//322	// This is only available with the Kitty Keyboard Protocol or the Windows323	// Console API.324	ShiftedCode rune325326	// BaseCode is the key pressed according to the standard PC-101 key layout.327	// On international keyboards, this is the key that would be pressed if the328	// keyboard was set to US PC-101 layout.329	//330	// For example, if the user presses 'q' on a French AZERTY keyboard,331	// [Key.BaseCode] will be 'q'.332	//333	// This is only available with the Kitty Keyboard Protocol or the Windows334	// Console API.335	BaseCode rune336337	// IsRepeat indicates whether the key is being held down and sending events338	// repeatedly.339	//340	// This is only available with the Kitty Keyboard Protocol or the Windows341	// Console API.342	IsRepeat bool343}344345// String implements [fmt.Stringer] and is quite useful for matching key346// events. It will return the textual representation of the [Key] if there is347// one, otherwise, it will fallback to [Key.Keystroke].348//349// For example, you'll always get "?" and instead of "shift+/" on a US ANSI350// keyboard.351func (k Key) String() string {352	return uv.Key(k).String()353}354355// Keystroke returns the keystroke representation of the [Key]. While less type356// safe than looking at the individual fields, it will usually be more357// convenient and readable to use this method when matching against keys.358//359// Note that modifier keys are always printed in the following order:360//   - ctrl361//   - alt362//   - shift363//   - meta364//   - hyper365//   - super366//367// For example, you'll always see "ctrl+shift+alt+a" and never368// "shift+ctrl+alt+a".369func (k Key) Keystroke() string {370	return uv.Key(k).Keystroke()371}