Emacs Init: Mouse Button Syntax

By Xah Lee. Date: . Last updated: .

Find Syntax for Mouse

  1. Alt+x describe-key
  2. press the button or scroll the wheel.
  3. Emacs displays the syntax in Messages Buffer

🛑 WARNING: The syntax for mouse button and wheel are different for each {Microsoft Windows, Linux, Mac}, also depends on what mouse, trackpad, etc you are using.

Mouse Syntax for Normal Mouse, on Linux

left button"<mouse-1>"
middle button"<mouse-2>"
right button"<mouse-3>"
wheel roll up"<mouse-4>"
wheel roll down"<mouse-5>"
back button"<mouse-8>"
forward button"<mouse-9>"

Mouse Syntax for Normal Mouse, on Microsoft Windows and macOS

left button"<mouse-1>"
middle button"<mouse-2>"
right button"<mouse-3>"
back button"<mouse-4>"
forward button"<mouse-5>"
wheel roll up"<wheel-up>"
wheel roll down"<wheel-down>"

Sample Code for Binding Mouse

;; sample setting for mouse button and wheel
;; Emacs 29 syntax
;; For emacs 28, use global-set-key
(cond
 ((eq system-type 'windows-nt)
  (keymap-global-set "<mouse-4>" 'describe-char))
 ((eq system-type 'darwin) ; Mac
  (keymap-global-set "<mouse-4>" 'describe-char))
 ((eq system-type 'gnu/linux)
  (keymap-global-set "<mouse-8>" 'describe-char)))

(keymap-global-set "M-<wheel-up>" 'previous-buffer)
(keymap-global-set "M-<wheel-down>" 'next-buffer)

Emacs Init, Mouse