Next: , Previous: , Up: Keymaps   [Contents][Index]


26.9 Functions for Key Lookup

Here are the functions and variables pertaining to key lookup.

Function: lookup-key keymap key &optional accept-defaults

This function returns the definition of key in keymap. If the string or vector key is not a valid key sequence according to the prefix keys specified in keymap (which means it is “too long” and has extra events at the end), then the value is a number, the number of events at the front of key that compose a complete key.

If accept-defaults is non-nil, then lookup-key considers default bindings as well as bindings for the specific events in key. Otherwise, lookup-key reports only bindings for the specific sequence key, ignoring default bindings except when you explicitly ask about them.

All the other functions described in this chapter that look up keys use lookup-key.

(lookup-key (current-global-map) "\C-x\C-f")
    ⇒ find-file
(lookup-key (current-global-map) "\C-x\C-f12345")
    ⇒ 2

If key begins with the character whose value is contained in meta-prefix-char, that character is implicitly removed and the META modifier added to the key. Thus, the first example below is handled by conversion into the second example.

(lookup-key (current-global-map) "\ef")
    ⇒ forward-word
(lookup-key (current-global-map) "\M-f")
    ⇒ forward-word

Unlike read-key-sequence, this function does not modify the specified events in ways that discard information (see Key Sequence Input). In particular, it does not convert letters to lower case.

Command: undefined

Used in keymaps to undefine keys. If a key sequence is defined to this, invoking this key sequence causes a “key undefined” error, just as if the key sequence had no binding.

Function: key-binding key &optional accept-defaults

This function returns the binding for key in the current keymaps, trying all the active keymaps. The result is nil if key is undefined in the keymaps.

The argument accept-defaults controls checking for default bindings, as in lookup-key (above).

(key-binding "\C-x\C-f")
    ⇒ find-file
(key-binding '(control home))
    ⇒ beginning-of-buffer
(key-binding [escape escape escape])
    ⇒ keyboard-escape-quit
Function: local-key-binding keys &optional accept-defaults

This function returns the binding for keys in the current local keymap, or nil if it is undefined there.

The argument accept-defaults controls checking for default bindings, as in lookup-key (above).

Function: global-key-binding keys &optional accept-defaults

This function returns the binding for command keys in the current global keymap, or nil if it is undefined there.

The argument accept-defaults controls checking for default bindings, as in lookup-key (above).

Function: minor-mode-key-binding key &optional accept-defaults

This function returns a list of all the active minor mode bindings of key. More precisely, it returns an alist of pairs (modename . binding), where modename is the variable that enables the minor mode, and binding is key’s binding in that mode. If key has no minor-mode bindings, the value is nil.

If the first binding is not a prefix command, all subsequent bindings from other minor modes are omitted, since they would be completely shadowed. Similarly, the list omits non-prefix bindings that follow prefix bindings.

The argument accept-defaults controls checking for default bindings, as in lookup-key (above).

Variable: meta-prefix-char

This variable is the meta-prefix character code. It is used when translating a two-character sequence to a meta character so it can be looked up in a keymap. For useful results, the value should be a prefix event (see Prefix Keys). The default value is ?\^[ (integer 27), which is the ASCII character usually produced by the ESC key.

As long as the value of meta-prefix-char remains ?\^[, key lookup translates ESC b into M-b, which is normally defined as the backward-word command. However, if you set meta-prefix-char to ?\^X (i.e. the keystroke C-x) or its equivalent ASCII code 24, then SXEmacs will translate C-x b (whose standard binding is the switch-to-buffer command) into M-b.

meta-prefix-char                    ; The default value.
     ⇒ ?\^[   ; Under SXEmacs.
     ⇒ 27     ; Under XEmacs 19.
(key-binding "\eb")
     ⇒ backward-word
?\C-x                               ; The print representation
                                           ;   of a character.
     ⇒ ?\^X   ; Under SXEmacs.
     ⇒ 24     ; Under XEmacs 19.
(setq meta-prefix-char 24)
     ⇒ 24
(key-binding "\C-xb")
     ⇒ backward-word            ; Now, typing C-x b is
                                    ;   like typing M-b.

(setq meta-prefix-char ?\e)          ; Avoid confusion!
                                     ; Restore the default value!
     ⇒ ?\^[   ; Under SXEmacs.
     ⇒ 27     ; Under XEmacs 19.

Next: , Previous: , Up: Keymaps   [Contents][Index]