Registering click events for emacs lisp

Go To StackoverFlow.com

1

I want to register a click event and do actions based on the position, window ..etc.

This site, http://www.gnu.org/software/emacs/manual/html_node/elisp/Click-Events.html, shows the layout of the event variables, but how do I actually attach a handler to the click events.

So, basically I am looking for a function that behaves like attach-handler would in the below senario.

(attach-handler [mouse-1] 
       `(lambda (e) (foo e))
2012-04-04 20:33
by Doboy


5

You may use the code letter `e' of the interactive declaration to access the event; e.g., the following will make left click insert the event data at the point clicked:

(define-key global-map (kbd "<down-mouse-1>")
  (lambda (event)
    (interactive "e")
    (message "%s" event)
    (let ((posn (elt event 1)))
      (with-selected-window (posn-window posn)
        (goto-char (posn-point posn))
        (insert (format "%s" event))))))
2012-04-04 21:36
by huaiyuan


1

They are all keys. Try C-h c ( or C-h k) and click a mouse button to see what it is currently bound to. Then use M-x global-set-key to set it to whatever you want to. Your function will have to be interactive to bind it to a key.

2012-04-04 20:42
by Miserable Variable
Ok, so how would I get the event information in the following statement?

(global-set-key [mouse-1] \(lambda() (interactive) (print event)) - Doboy 2012-04-04 20:49

Are you getting anything out of the print? You might want to create named function instead of a lambda and edebug it to see if event has any interesting properties. Also look at last-input-event and last-command-event. Oh and there is also mouse-event-p and others - Miserable Variable 2012-04-04 21:00
Thanks last-command-event was what I was looking fo - Doboy 2012-04-04 21:37
Glad it was useful. I also learned something new - Miserable Variable 2012-04-04 21:57
Ads