Two-and-a-half answers:
- Artur Malabarba's init.el code. The blog post has useful comments.
- 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.