Key-binding config for Go TUI apps. Parse and normalize key strings, load/save JSON keybind files with merge-with-defaults semantics, validate for intra-area conflicts.
go-keybind provides two focused utilities for Go TUI applications:
-
A
Keytype — parse and normalize keyboard shortcut strings. Catches typos ("crtl+c"), canonicalizes aliases ("escape"→"esc","return"→"enter","meta"→"alt"), fixes modifier order, and produces a comparable value you can switch on. -
A generic JSON config loader —
Load[T],Save[T], andValidatework with any app-defined struct. Fields the user didn't touch keep their compiled-in defaults, the file is written on first run, andValidatecatches intra-area conflicts without false-positives for intentional cross-area duplicates.
Extracted from matcha's config package. Zero external dependencies.
go get github.com/floatpane/go-keybindRequires Go 1.26+.
k, err := keybind.Parse("ctrl+shift+a")
// k.Ctrl = true, k.Shift = true, k.Base = "a"
// k.String() = "ctrl+shift+a"
// Aliases normalize to the same Key:
a, _ := keybind.Parse("escape")
b, _ := keybind.Parse("esc")
fmt.Println(a == b) // true
// Modifier order doesn't matter in input:
c, _ := keybind.Parse("shift+ctrl+a")
d, _ := keybind.Parse("ctrl+shift+a")
fmt.Println(c == d) // truetype MyKeys struct {
Quit string `json:"quit"`
Reload string `json:"reload"`
NavUp string `json:"nav_up"`
}
cfg, err := keybind.Load(cfgDir, "keybinds.json", MyKeys{
Quit: "ctrl+c",
Reload: "r",
NavUp: "k",
})
// If keybinds.json didn't exist it was created with the defaults.
// Fields absent from an existing file keep their default values.conflicts := keybind.Validate(map[string]map[string]string{
"global": {"quit": cfg.Quit, "reload": cfg.Reload},
"nav": {"up": cfg.NavUp, "down": cfg.NavDown},
})
// Cross-area duplicates ("d" for delete in two different views) are fine.
// Only intra-area conflicts (two actions bound to the same key) are reported.quitKey := keybind.MustParse(cfg.Quit) // panics on bad config value
// In your event handler:
if event.String() == quitKey.String() {
return tea.Quit
}Modifiers: ctrl, alt (= meta = opt), shift
Special keys:
| Canonical | Aliases |
|---|---|
enter |
return |
esc |
escape |
tab |
— |
backspace |
bs |
space |
— |
up, down, left, right |
— |
delete |
del |
insert |
— |
home, end |
— |
pgup |
pageup |
pgdown |
pagedown, pgdn |
f1 … f12 |
— |
Any single printable character is also accepted (a, A, 1, /, […).
Full API reference: pkg.go.dev/github.com/floatpane/go-keybind
Guides: see docs/.
| Project | Role |
|---|---|
| floatpane/matcha | Reference consumer — inbox, email, composer, folder keybinds. |
PRs welcome. See CONTRIBUTING.md.
Report vulnerabilities privately via SECURITY.md.
MIT. See LICENSE.