50.3.10 Rebinding Mouse Buttons

Emacs uses Lisp symbols to designate mouse buttons, too. The ordinary mouse events in Emacs are click events; these happen when you press a button and release it without moving the mouse. You can also get drag events, when you move the mouse while holding the button down. Drag events happen when you finally let go of the button.

The symbols for basic click events are mouse-1 for the leftmost button, mouse-2 for the next, and so on. Here is how you can redefine the second mouse button to split the current window:

(keymap-global-set "<mouse-2>" 'split-window-below)

The symbols for drag events are similar, but have the prefix ‘drag-’ before the word ‘mouse’. For example, dragging the first button generates a drag-mouse-1 event.

You can also define bindings for events that occur when a mouse button is pressed down. These events start with ‘down-’ instead of ‘drag-’. Such events are generated only if they have key bindings. When you get a button-down event, a corresponding click or drag event will always follow.

If you wish, you can distinguish single, double, and triple clicks. A double click means clicking a mouse button twice in approximately the same place. The first click generates an ordinary click event. The second click, if it comes soon enough, generates a double-click event instead. The event type for a double-click event starts with ‘double-’: for example, double-mouse-3.

This means that you can give a special meaning to the second click at the same place, but it must act on the assumption that the ordinary single click definition has run when the first click was received.

This constrains what you can do with double clicks, but user interface designers say that this constraint ought to be followed in any case. A double click should do something similar to the single click, only more so. The command for the double-click event should perform the extra work for the double click.

If a double-click event has no binding, it changes to the corresponding single-click event. Thus, if you don’t define a particular double click specially, it executes the single-click command twice.

Emacs also supports triple-click events whose names start with ‘triple-’. Emacs does not distinguish quadruple clicks as event types; clicks beyond the third generate additional triple-click events. However, the full number of clicks is recorded in the event list, so if you know Emacs Lisp you can distinguish if you really want to (see Click Events in The Emacs Lisp Reference Manual). We don’t recommend distinct meanings for more than three clicks, but sometimes it is useful for subsequent clicks to cycle through the same set of three meanings, so that four clicks are equivalent to one click, five are equivalent to two, and six are equivalent to three.

Emacs also records multiple presses in drag and button-down events. For example, when you press a button twice, then move the mouse while holding the button, Emacs gets a ‘double-drag-’ event. And at the moment when you press it down for the second time, Emacs gets a ‘double-down-’ event (which is ignored, like all button-down events, if it has no binding).

The variable double-click-time specifies how much time can elapse between clicks and still allow them to be grouped as a multiple click. Its value is in units of milliseconds. If the value is nil, double clicks are not detected at all. If the value is t, then there is no time limit. The default is 500.

The variable double-click-fuzz specifies how much the mouse can move between clicks and still allow them to be grouped as a multiple click. Its value is in units of pixels on windowed displays and in units of 1/8 of a character cell on text-mode terminals; the default is 3.

The symbols for mouse events also indicate the status of the modifier keys, with the usual prefixes ‘C-’, ‘M-’, ‘H-’, ‘s-’, ‘A-’, and ‘S-’. These always precede ‘double-’ or ‘triple-’, which always precede ‘drag-’ or ‘down-’.

A frame includes areas that don’t show text from the buffer, such as the mode line and the scroll bar. You can tell whether a mouse button comes from a special area of the screen by means of dummy prefix keys. For example, if you click the mouse in the mode line, you get the prefix key mode-line before the ordinary mouse-button symbol. Thus, here is how to define the command for clicking the first button in a mode line to run scroll-up-command:

(keymap-global-set "<mode-line> <mouse-1>" 'scroll-up-command)

Here is the complete list of these dummy prefix keys and their meanings:

mode-line

The mouse was in the mode line of a window.

vertical-line

The mouse was in the vertical line separating side-by-side windows. (If you use scroll bars, they appear in place of these vertical lines.)

vertical-scroll-bar

The mouse was in a vertical scroll bar. (This is the only kind of scroll bar Emacs currently supports.)

menu-bar

The mouse was in the menu bar.

tab-bar

The mouse was in a tab bar.

tab-line

The mouse was in a tab line.

header-line

The mouse was in a header line.

You can put more than one mouse button in a key sequence, but it isn’t usual to do so.