Skip to content
Randall C. O'Reilly edited this page Jan 25, 2024 · 1 revision

This page has info for Keyboard input, Key mappings, Shortcuts, etc

KeyMaps and Prefs

The std GoGi Prefs has a KeyMap that the user can select from among a set of default or customized KeyMaps -- do Edit Key Maps in Prefs editor to see these maps. They map from a keyboard sequence to a KeyFun which is an "enum" of named functions that are recognized across various contexts (e.g., KeyFunCopy for copy-to-clipboard, etc)

The key.Chord (defined in oswin/key) is a string type with methods for decoding / encoding standard key + modifier sequences.

The Modifiers are:

  • Shift, Control, Alt, Meta (= Command on MacOs)

You can also use "Command" as a modifier in Shortcuts etc that will translate into Meta on Mac, and Control on other platforms -- this is a good way to specify shortcuts that translate well for conventions across platforms.

The KeyMaps are typically used directly in the key.Event function for a given Widget -- the incoming key.Chord is mapped onto its corresponding KeyFun and that KeyFun is then used (in a big switch) to determine what function to perform.

Shortcuts

Arbitrary shortcuts can also be assigned at a window-level, in Action and Menu items in ToolBar or MenuBar widgets.

Here's an Action example, using ShortcutKey to directly access the current KeyMap value for gi.KeyFunCut:

	ac = m.AddAction(gi.ActOpts{Label: "Cut", ShortcutKey: gi.KeyFunCut},
		tv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
			txf := recv.Embed(KiT_TextView).(*TextView)
			txf.Cut()
		})

(using Shortcut: "Command+C" would specify a literal shortcut that is consistent regardless of the KeyMap)

For a ToolBar that is defined via Props, you can set a shortcut directly via the "shortcut" property:

	{"UpdateFiles", ki.Props{
		"shortcut": "Command+U",
		"desc":     "update file browser list of files",
		"icon":     "update",
	}},

If you specify a KeyFun here, it will automatically map that onto a standard gi.KeyFun:

	"shortcut": gi.KeyFunHistPrev,

If you have further custom keymaps, you can also use a function, that will grab a KeyMap default, e.g., in Gide:

	{"SaveActiveView", ki.Props{
		"label": "Save",
		"desc":  "save active text view file to its current filename",
		"icon":  "file-save",
		"shortcut-func": giv.ShortcutFunc(func(gei interface{}, act *gi.Action) key.Chord {
			return key.Chord(gide.ChordForFun(gide.KeyFunBufSave).String())
		}),
	}},
Clone this wiki locally