Kbd
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/kbd.go
Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.
package main
import (
"fmt"
"github.com/dyike/keel/ui/core"
"github.com/dyike/keel/ui/el"
"github.com/dyike/keel/ui/kit"
"github.com/dyike/keel/ui/theme"
)
func init() { registerSection("kbd", "general", kbdGallery) }
func kbdGallery() core.Widget {
core.Bind("demo.palette", "mod+shift+p")
opened := 0
return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
cx.Action("demo.palette", func() { opened++ })
rebind := func(chord string) func() { return func() { core.Bind("demo.palette", chord) } }
row := func(size float32) el.Element {
return el.Div().Wrap().Items(el.Center).Gap(12).TextSize(size).Child(
el.Text(demoText("Command palette 123", "命令面板 123")), kit.Kbd("mod+shift+p").Render(cx),
el.Text(demoText("Confirm", "确认")), kit.Kbd("enter").Render(cx), kit.Kbd("mod+s").Plain().Render(cx))
}
return el.Div().W(el.Full).P(24).Gap(16).Child(
el.Text(demoText("Kbd · Keyboard shortcut", "Kbd · 快捷键")).TextSize(24).Bold(),
el.Text(demoText("Follows the parent font size; display only, without registering a shortcut.", "随父元素字号变化;仅展示,不注册快捷键。")),
row(12), row(16), row(24),
el.Div().Wrap().Gap(12).Items(el.Center).Child(kit.Kbd("mod+s").Size(12).Render(cx), kit.Kbd("mod+k").Size(24).Style(func(e *el.TextEl) { e.Bg(theme.Subtle).TextColor(theme.PrimaryText) }).Render(cx)),
el.Div().Wrap().Gap(12).Child(kit.Kbd("esc").Render(cx), kit.Kbd("alt+backspace").Render(cx), kit.Kbd("ctrl+up").Render(cx)),
el.Div().W(el.Dp(140)).Child(kit.Kbd(demoText("Unparsed text: English 123, with a long label", "未解析的中文 English 123 长文案")).Render(cx)),
el.Text(demoText("Action binding: press the displayed shortcut to run the action. Changing the shortcut updates both the keycap and handler.", "动作绑定:按下键帽上的键触发动作,改键后键帽和处理器一起变")).TextSize(16).Bold(),
el.Div().Wrap().Items(el.Center).Gap(12).Child(
el.Text(fmt.Sprintf(demoText("Open command palette (triggered %d times)", "打开命令面板(已触发 %d 次)"), opened)), kit.KbdFor("demo.palette").Render(cx),
kit.Button(demoText("Change to mod+k", "改成 mod+k"), rebind("mod+k")).Variant(kit.ButtonSecondary).Render(cx),
kit.Button(demoText("Restore mod+shift+p", "恢复 mod+shift+p"), rebind("mod+shift+p")).Variant(kit.ButtonGhost).Render(cx)),
)
}))
}
Kbd displays shortcut key caps, does not register shortcut keys, and does not participate in Tab navigation. The font size is inherited from the parent element, the text color uses theme.Muted, and the border uses theme.Outline (call Plain() to hide it). Switching the theme at runtime takes effect immediately.
el.Div().Row().Items(el.Center).TextSize(16).Child(
el.Text("Command palette"),
kit.Kbd("mod+shift+p").Render(cx),
kit.Kbd("enter").Plain().Render(cx),
)
Kbd(shortcut string) returns *KbdView. Plain() hides borders and preserves spacing. Size(sp) sets the font size and adjusts padding proportionally; 0 restores font size inheritance and default padding, negative values, NaN, infinity, and values greater than 128 are ignored. Style(func(*el.TextEl)) adjusts the background, text/border color, rounded corners, spacing, etc. after the default style of each frame; nil returns to default. The callback does not retain element references, and the Agent name always uses the original shortcut. The optional border is 1dp, rounded corners 4dp, horizontal padding 6dp, vertical padding 3dp. Keep a single line in narrow containers and truncate.
Formatting is provided by core.ShortcutLabel(s, goos string) string, which is shared by kit and window shortcut keys. The syntax is the same as core.ParseShortcut; mod is displayed as Command on macOS and Ctrl on Windows/Linux. Unparsable copy is displayed as is, making it easier to use custom key names. This function only formats and does not query action bindings.
Show action-bound keys
Shortcut keys can be bound to named actions, see Elements and Views · Action and Key Table. kit.KbdFor("editor.save") displays the first key currently bound to this action; after the user changes the key (core.Bind, core.LoadKeymap), the next frame is automatically updated, and nothing is displayed when there is no binding.
kit.KbdFor("editor.save").Render(cx) // keycap
kit.Menu().ActionItem("Save", "editor.save", save) // The same key appears on the right side of the menu
The Agent role is text, and the name is the original shortcut passed in; the symbol corresponding to the platform is displayed on the screen, and no additional duplicate snapshot nodes are generated.
Verification entry: go run ./examples/components -section kbd, add -theme dark to check the dark color. Examples cover 12/16/24sp, Plain, platform symbols, Chinese and English numbers, and narrow containers.
Show elements in context
kit.KbdFor("format").At("code") resolves the binding by the KeyContext path of the element with ID code, using the same rules it uses for handling keystrokes itself (including the conditional expression for core.BindIn). For example, the toolbar button prompts should show the actual shortcut keys in the editor. Display nothing when the element does not exist, is hidden, or is disabled.