22.7.14 Classifying Events

Every event has an event type, which classifies the event for key binding purposes. For a keyboard event, the event type equals the event value; thus, the event type for a character is the character, and the event type for a function key symbol is the symbol itself. For events that are lists, the event type is the symbol in the CAR of the list. Thus, the event type is always a symbol or a character.

Two events of the same type are equivalent where key bindings are concerned; thus, they always run the same command. That does not necessarily mean they do the same things, however, as some commands look at the whole event to decide what to do. For example, some commands use the location of a mouse event to decide where in the buffer to act.

Sometimes broader classifications of events are useful. For example, you might want to ask whether an event involved the META key, regardless of which other key or mouse button was used.

The functions event-modifiers and event-basic-type are provided to get such information conveniently.

Function: event-modifiers event

This function returns a list of the modifiers that event has. The modifiers are symbols; they include shift, control, meta, alt, hyper and super. In addition, the modifiers list of a mouse event symbol always contains one of click, drag, and down. For double or triple events, it also contains double or triple.

The argument event may be an entire event object, or just an event type. If event is a symbol that has never been used in an event that has been read as input in the current Emacs session, then event-modifiers can return nil, even when event actually has modifiers.

Here are some examples:

(event-modifiers ?a)
     ⇒ nil
(event-modifiers ?A)
     ⇒ (shift)
(event-modifiers ?\C-a)
     ⇒ (control)
(event-modifiers ?\C-%)
     ⇒ (control)
(event-modifiers ?\C-\S-a)
     ⇒ (control shift)
(event-modifiers 'f5)
     ⇒ nil
(event-modifiers 's-f5)
     ⇒ (super)
(event-modifiers 'M-S-f5)
     ⇒ (meta shift)
(event-modifiers 'mouse-1)
     ⇒ (click)
(event-modifiers 'down-mouse-1)
     ⇒ (down)

The modifiers list for a click event explicitly contains click, but the event symbol name itself does not contain ‘click’. Similarly, the modifiers list for an ASCII control character, such as ‘C-a’, contains control, even though reading such an event via read-char will return the value 1 with the control modifier bit removed.

Function: event-basic-type event

This function returns the key or mouse button that event describes, with all modifiers removed. The event argument is as in event-modifiers. For example:

(event-basic-type ?a)
     ⇒ 97
(event-basic-type ?A)
     ⇒ 97
(event-basic-type ?\C-a)
     ⇒ 97
(event-basic-type ?\C-\S-a)
     ⇒ 97
(event-basic-type 'f5)
     ⇒ f5
(event-basic-type 's-f5)
     ⇒ f5
(event-basic-type 'M-S-f5)
     ⇒ f5
(event-basic-type 'down-mouse-1)
     ⇒ mouse-1
Function: mouse-movement-p object

This function returns non-nil if object is a mouse movement event. See Motion Events.