Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Disable Mouse only inside Emacs

As laptop touchpads seem to be steadily increasing in size, one unfortunate consequence is that it becomes increasingly harder to avoid touching them by accident while you type. Most systems have safeguards in place that disable the touchpad as you’re typing, but they always seem to fall short for me when it comes to Emacs. While in Emacs, my hands are permanently resting on the keyboard (and over the touchpad), so even if I stop typing for several seconds I don’t want the touchpad to reactivate.

There are ways to permanently deactivate the touchpad with a hotkey, but, so far, the solution that best fits my use-style is to disable it only inside Emacs.

(define-minor-mode disable-mouse-mode
  "A minor-mode that disables all mouse keybinds."
  :global t
  :lighter " 🐭"
  :keymap (make-sparse-keymap))

(dolist (type '(mouse down-mouse drag-mouse
                      double-mouse triple-mouse))
  (dolist (prefix '("" C- M- S- M-S- C-M- C-S- C-M-S-))
    ;; Yes, I actually HAD to go up to 7 here.
    (dotimes (n 7)
      (let ((k (format "%s%s-%s" prefix type n)))
        (define-key disable-mouse-mode-map
          (vector (intern k)) #'ignore)))))

All we do here is define a minor-mode that binds all mouse-related keys to ignore. This is a slight improvement over the code on this StackOverflow answer. Of course, let’s not forget to enable this.

(disable-mouse-mode 1)

Two relevant limitations:

  • This doesn’t distinguish between a touchpad an an actual mouse.
  • It is still possible for modes to define specific buttons that interact with the mouse, but that’s not a huge problem because these buttons take a small portion of the screen so it’s fairly difficult to touch them by accident.

Update 29 Jun 2016

By mere coincidence, it looks like Steve Purcell implemented something extremely similar (comment below). Unlike me, he actually had the decency of wrapping the minor-mode in a Melpa package, and I know some might prefer that over lengthening their init file.

Tags: keybind, init.el, emacs,

comments powered by Disqus