5

I can't believe there is no way in Emacs to easily and reliably ignore the mouse but my google fu seems not good enough to find how to do it.

By easily and reliably I mean to disable the device, not unbind the mouse events (which is not reliable as any modes can rebind them).

syl20bnr
  • 2,095
  • 11
  • 21
  • 1
    Anything you'll be able to do easily (i.e. from lisp), a package would be able to undo it, so I think the question isn't 100% clear. Anyway, my suggestion would be to rebind the events in some "high priority" map. The `bind-key` package has a `bind-key*` function which tries hard to do this. – YoungFrog Jul 26 '15 at 08:35
  • 2
    Did you try `emacs -nw` yet? – wasamasa Jul 26 '15 at 08:44
  • 2
    I don't quite get the reasoning to be honest, if you'd want to do it easily and reliably, do it on the OS level instead of expecting that Emacs comes up with the feature. – wasamasa Jul 26 '15 at 08:58
  • 3
    The idea is to disable easily and reliably the mouse for Emacs in GUI mode like in the terminal, being forced to do it at the whole OS just for Emacs would be silly. This is a request I got for Spacemacs and I find it perfectly valid. – syl20bnr Jul 26 '15 at 13:58
  • Can you give some examples of the modes which rebind mouse events? – Name Jul 26 '15 at 15:00
  • Not really, but disabling the mouse by rebinding the mouse events is not reliable since any code can rebind them again. This is all the point of the question: disabling the mouse reliably, that is _really_ disabling it (like in the terminal). – syl20bnr Jul 27 '15 at 01:27
  • Can [this question](http://emacs.stackexchange.com/q/16973/3972) lead you to ideas? If you come up with an answer, please post it here too. – Mirzhan Irkegulov Dec 26 '15 at 18:08
  • I don't follow. What problem are you having that isn't avoided by not touching the mouse? I disable the mouse when using Emacs by keeping my hands on the keyboard ;) – Tyler May 27 '16 at 19:45

3 Answers3

3

Two-and-a-half answers:

  1. Artur Malabarba's init.el code. The blog post has useful comments.
  2. Victor Deryagin's init.el code from which Malabarba derived his.

My third "answer" probably only applies to touchpads, but since most folks who have the need to turn off "the mouse" are actually using laptops and are accidentally touching their touchpad, I'll throw it in. (Along with an invitation to anyone who can extend the following to handle the mouse, in which case this post will actually provide 3 answers :-) Though this answer also relies on init.el code, it is more OS-oriented and X-oriented (and probably Linux-only: OS + X != OSX), another reason why I'm listing it last.

This approach calls synclient or xinput depending on which works for you. First, some details:

synclient is the newer/better way to control touchpads, so you should probably prefer it if you have it or can install it. synclient defines properties which you can set at runtime. The relevant property here is TouchpadOff which you can check (from a shell) with

synclient -l | fgrep -e TouchpadOff

1==off (think TouchpadOff==true), 0==on. Unfortunately on my 10-year-old laptop, I get

$ synclient -l | fgrep -e TouchpadOff
    TouchpadOff             = 2

which apparently means "it don't work," because empirically I can do

synclient TouchpadOff=1

and nothing happens--my touchpad continues to work. Fortunately I (and perhaps you) can use the {older, less flexible, more likely to break on kernel/configuration change} xinput. First, check the ID of your touchpad: in my case, it's

$ xinput --list | fgrep -ie touchpad
⎜   ↳ SynPS/2 Synaptics TouchPad                    id=11   [slave  pointer  (2)]

(Note you can also do something very similar for your mouse, if you've got one, and control it via the obvious extension of the following procedure. However I'm not discussing that here because synclient does not--IIUC--provide similar functionality.)

I can then do

xinput --disable 11

which actually WFM, and likewise

xinput --enable 11

So how to wrap all this for init.el? I defer to any elisp experts who can improve the following--I am certainly lacking in elisp-fu--but here goes:

(defconst XINPUT-TOUCHPAD-ID "11") ; if using xinput, SET TO VALUE APPROPRIATE FOR YOUR DEVICE!
(defconst XINPUT-DISABLE-TOUCHPAD (concat "xinput --disable " XINPUT-TOUCHPAD-ID))
(defconst XINPUT-ENABLE-TOUCHPAD  (concat "xinput --enable " XINPUT-TOUCHPAD-ID))
(defconst SYNCLIENT-DISABLE-TOUCHPAD "synclient TouchpadOff=1")
(defconst SYNCLIENT-ENABLE-TOUCHPAD  "synclient TouchpadOff=0")

;;; TEST YOUR DEVICE before you choose to use `synclient` (preferred) or `xinput`
(defconst DISABLE-TOUCHPAD XINPUT-DISABLE-TOUCHPAD)
(defconst ENABLE-TOUCHPAD  XINPUT-ENABLE-TOUCHPAD)

(defun touchpad-off (&optional frame)
  (interactive)
  (shell-command DISABLE-TOUCHPAD))

(defun touchpad-on (&optional frame)
  (interactive)
  (shell-command ENABLE-TOUCHPAD))

(add-hook 'focus-in-hook #'touchpad-off)
(add-hook 'focus-out-hook #'touchpad-on)
(add-hook 'delete-frame-functions #'touchpad-on)

;;; and don't forget to enable the touchpad when you exit Emacs:
(add-hook 'kill-emacs-hook #'touchpad-on)

Once enabled from your init.el, the above code should turn your touchpad off when any GUI Emacs (errr ... dunno if this works for XEmacs) frame gains focus, and back on when an Emacs frame loses focus.

TomRoche
  • 592
  • 3
  • 20
2

Complementary to the answer that TomRoche provided, you might want to enable mouse-avoidance-mode which makes sure that the mouse pointer doesn't get in your way, in boring or more entertaining ways. Have fun!

1

I just published a package called disable-mouse which does this: it provides a global minor mode which swallows and ignores all mouse events.

sanityinc
  • 2,871
  • 13
  • 17