I would like to set up emacs so that it plays a typewriter sounds when typing text into the buffer, as well as a carriage return sound when hitting enter (similar to the Q10 editor on windows). Does anyone have any suggestions for how I might go about this? Is there a hook that I could use?
I currently use aquamacs and emacs 22, but am not averse to upgrading.
EDIT: In case anyone is interested, the vim version of this question was asked here: How can I make VIM play typewriter sound when I write a letter?
First you must establish some way to play sound:
(defun play-typewriter-sound ()
(let ((data-directory "~/Dowloads/Sounds"))
(play-sound `(sound :file "key1.wav"))))
...doesn't work on Mac OSX Emacs for example since it's not compiled with sound support. There are workarounds though, see for example http://www.emacswiki.org/emacs/ErcSound
Then, you can use advice on any Emacsen
(defadvice self-insert-command (after play-a-sound activate)
(play-typewriter-sound))
You could also advise newline-and-indent
.
On Emacs24 you now have post-self-insert-hook
(add-hook 'post-self-insert-hook 'play-typewriter-sound)
If you don't like defadvice
you can use post-command-hook
and check the name of this-command
there:
(add-hook 'post-command-hook #'play-typewriter-sound-maybe)
(defun play-typewriter-sound-maybe ()
(if (eq this-command 'self-insert-command)
(play-typewriter-sound)))
If someone need this using afplay
here whats I use
(defun play (audio-name)
(interactive)
(let* (buf (get-buffer-create "playnoise"))
(start-process-shell-command
"play" buf (concat (format "afplay /Users/foo/audios/%s" audio-name) ".mp3"))))
(play "wrong")
This is quite an old question now, but in case somebody stumbles into this, there's selectric-mode for emacs in MELPA now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With