Subject: bug#4478: 23.1; key bindings for mouse wheel - unclear

From: Stefan Monnier <[email-masked]>
Date: Fri, 18 Sep 2009 09:34:14 -0400

> 1. Doc bug: The doc is not clear about how to bind the mouse wheel
> (rotations).

Indeed.  The way to do it is currently messy and ugly.
 
> I've been using bindings such as this:
 
> (define-key map [wheel-down]   'aaa)
> (define-key map [wheel-down]   'bbb)
> (define-key map [C-wheel-down] 'ccc)
> (define-key map [C-wheel-down] 'ddd)

IIUC this is "the right way".  And it works under w32 and ns.
Under X11, sadly, mouse-wheel events are usually represented as mouse
clicks for buttons 4 and 5, and there's no way for Emacs to know whether
these correspond to mouse clicks (on mice with more than 3 buttons) or
mouse-wheel events.  Worse yet, Emacs doesn't offer an easy way to map
all the variants of `mouse-4' and `mouse-5' (with and without each of
the possible modifiers) to wheel-up and wheel-down.

The code appended below does manage to do such a remap, but as you can
see it's ugly, and worse yet: there can only be one such thing because
it uses the "default" binding.  [ And it probably won't work with
xterm-mouse-mode. ]
 
> (define-key map (vector mouse-wheel-down-event) 'aaa)
> (define-key map (vector mouse-wheel-up-event)   'bbb)
> (define-key map
>   (vector (list 'control mouse-wheel-down-event)) 'ccc)
> (define-key map
>   (vector (list 'control mouse-wheel-up-event)) 'ddd)
 
> I haven't tested on GNU/Linux, but I'm assuming this is what to use
> for portable code. Is there a shorter or better way to say this and
> still be portable? 

I think this is about as good as it gets for now, yes.
 
> 1. If there is not, there should be (a shorter way to do this).

The [wheel-down] and friends shown above is what there should be.

        Stefan

(define-key function-key-map [t]
	 '(menu-item "" nil
	   :filter sm-rewrite-mwheel))

(defun sm-rewrite-mwheel (ignore)
  (let* ((events (this-single-command-raw-keys))
         (event (unless (zerop (length events))
                  (aref events (1- (length events))))))
    (message "sm-rewrite-mwheel: key=%s" event)
    (case (event-basic-type event)
      (mouse-4
       (let* ((mods (delete 'click (event-modifiers event)))
              (new-key (event-convert-list (append mods '(wheel-up))))
              (new-event (cons new-key (cdr event))))
         (vector new-event)))
      (mouse-5
       (let* ((mods (delete 'click (event-modifiers event)))
              (new-key (event-convert-list (append mods '(wheel-down))))
              (new-event (cons new-key (cdr event))))
         (vector new-event))))))