8

I have Emacs.app version 24.5 (9.0) on a MacBook with Mac OS X 10.11.3 (15D21). How can I generate mouse-2 from my MacBook's trackpad? I've googled and found references to mac-emulate-three-button-mouse (this question) (not found in such Emacs version) and similar tricks that do not work.

It is possible to generate such an event, or there is some other way, like binding globally something like s-<mouse-1> to <mouse-2>, etc.?

Renzo
  • 251
  • 4
  • 8

5 Answers5

7

After some more googling and reading of the manual, I have found a solution that works for me, by adding to the file ~/.emacs.d/init.el the following line:

(define-key key-translation-map (kbd "<s-mouse-1>") (kbd "<mouse-2>"))

In this way, I can generate mouse-2 events by pressing Command when clicking with the trackpad.

key-translation-map is a translation keymap (see the manual) that “... uses translation keymaps to translate certain event sequences into others.”

sds
  • 5,928
  • 20
  • 39
Renzo
  • 251
  • 4
  • 8
6

You could also tell flyspell to use mouse-3 which seems more appropriate for a context menu:

(eval-after-load "flyspell"
    '(progn
       (define-key flyspell-mouse-map [down-mouse-3] #'flyspell-correct-word)
       (define-key flyspell-mouse-map [mouse-3] #'undefined)))

Answer stolen from here

Bryan Ash
  • 160
  • 2
  • 6
2

Copied from my answer at SuperUser:

This isn't so much of a solution as a workaround, but I'm currently trying the (open source) MiddleClick app which adds middle-click functionality all across the system by triggering a middle-click when you do a three-finger-tap.

The only problems are that you can press middle-click accidentally by touching three fingers on the trackpad every now and then, which has been somewhat annoying. There are also obviously the system trackpad bindings (e.g., App Expose or Mission Control) that can conflict with the three-finger-tap if you don't make the corresponding scrolling motion pronounced enough. You could probably fix these conflicts by tweaking the sources, as it appears that there are no settings in the running app itself.

You can install MiddleClick via homebrew as well:

brew cask install middleclick

EDIT:

It turns out that the three-finger-tap behavior of MiddleClick is rather annoying and unusable, for me at least. Also, trying to modify the sources for better behavior proved to be more trouble than it's worth, as MiddleClick makes use of MultitouchSupport.framework, which is private and requires reverse-engineering to make sense of its functions' parameters. Unfortunately, I don't have time for that right now, esp. over something so basic. However, I did find BetterTouchTool which implements the same basic functionality but provides much more customizability. It's not free, but it's only $3 and moreover it works fine with the Magic Trackpad 2, which can't be said for the similar app that I also tried, MagicPrefs.

BetterTouchTool can also be installed via homebrew:

brew cask info bettertouchtool
GDP2
  • 1,340
  • 9
  • 25
1

This works for me

(defun middle-click (click)
  (interactive "e")
  (let ((event (event-start click))
        (prefix-arg current-prefix-arg))
    (call-interactively (key-binding (vector (list 'mouse-2 event)) t))))

(global-set-key (kbd "<s-mouse-1>") 'middle-click)

From https://lists.gnu.org/archive/html/help-gnu-emacs/2003-07/msg00259.html

killdash9
  • 179
  • 8
1

I adapted bryan-ash's answer to use the add-hook function instead of eval-after-load to customize my magic mouse bindings for the flyspell.

  1. Add the elisp code below to your .emacs file to map the magic mouse down-mouse-3 and mouse-3 clicks for flyspell.

    ;;
    (add-hook 'flyspell-mode-hook 'my-flyspell-mode-hook)
    ;;
    (defun my-flyspell-mode-hook ()
      ;; Do things when flyspell enters of leaves flyspell mode
      ;; Added manually
      ;;
      ;; Magic Mouse Fixes
      (if flyspell-mode (progn
           (define-key flyspell-mouse-map [down-mouse-3] #'flyspell-correct-word)
           (define-key flyspell-mouse-map [mouse-3] #'undefined))
        nil)
      ;; End my-flyspell-mode-hook
      )
    

This answer was validated using:

emacs-version: GNU Emacs 26.1 (build 1, x86_64-apple-darwin14.5.0, NS appkit-1348.17 Version 10.10.5 (Build 14F2511)) of 2018-05-30

Melioratus
  • 4,504
  • 1
  • 25
  • 43