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


27.4.2.2 Changing Key Bindings Programmatically

You can use the functions global-set-key and define-key to rebind keys under program control.

(global-set-key keys cmd)

Defines keys globally to run cmd.

(define-key keymap keys def)

Defines keys to run def in the keymap keymap.

keymap is a keymap object.

keys is the sequence of keystrokes to bind.

def is anything that can be a key’s definition:

For backward compatibility, SXEmacs allows you to specify key sequences as strings. However, the preferred method is to use the representations of key sequences as vectors of keystrokes. See Keystrokes, for more information about the rules for constructing key sequences.

Emacs allows you to abbreviate representations for key sequences in most places where there is no ambiguity. Here are some rules for abbreviation:

Here are some examples of programmatically binding keys:

;;;  Bind my-command to f1
(global-set-key 'f1 'my-command)

;;;  Bind my-command to Shift-f1
(global-set-key '(shift f1) 'my-command)

;;; Bind my-command to C-c Shift-f1
(global-set-key '[(control c) (shift f1)] 'my-command)

;;; Bind my-command to the middle mouse button.
(global-set-key 'button2 'my-command)

;;; Bind my-command to META CTL Right Mouse Button
;;; in the keymap that is in force when you are running dired.
(define-key dired-mode-map '(meta control button3) 'my-command)


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