KeyEventToString returns a human-readable name for a key event described by the given key type, character, and modifier.
(key KeyType, ch rune, mod ModifierKey)
| 213 | // KeyEventToString returns a human-readable name for a key event described |
| 214 | // by the given key type, character, and modifier. |
| 215 | func KeyEventToString(key KeyType, ch rune, mod ModifierKey) (string, error) { |
| 216 | var s string |
| 217 | if key == 0 { |
| 218 | s = string([]rune{ch}) |
| 219 | } else { |
| 220 | var ok bool |
| 221 | s, ok = keyToString[key] |
| 222 | if !ok { |
| 223 | return "", fmt.Errorf("no such key %d (ch=%c)", key, ch) |
| 224 | } |
| 225 | |
| 226 | // Special case for ArrowUp/Down/Left/Right |
| 227 | switch s { |
| 228 | case "ArrowUp": |
| 229 | s = "^" |
| 230 | case "ArrowDown": |
| 231 | s = "v" |
| 232 | case "ArrowLeft": |
| 233 | s = "<" |
| 234 | case "ArrowRight": |
| 235 | s = ">" |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if m := mod.String(); m != "" { |
| 240 | return m + "-" + s, nil |
| 241 | } |
| 242 | |
| 243 | return s, nil |
| 244 | } |
| 245 | |
| 246 | // ToKey parses a single key name string into its KeyType, modifier, and rune components. |
| 247 | func ToKey(key string) (KeyType, ModifierKey, rune, error) { |
searching dependent graphs…