You could take advantage of the recent-keys
function. It returns a vector of the last 300 events.
Using
(defun get-last-key ()
(interactive)
(let ((vect (recent-keys)))
(aref vect (1- (length vect)))))
And then doing C-x C-e on
(get-last-key)
You will get
5 (#o5, #x5, ?\C-e)
You will "just" have to compare this to the keystroke you want.
Note that as suggested by @stsquad, when you will call this function, your last keystroke probably won't be <home>
(more likely <return>
). So depending on what you want to achieve, re-binding <home>
might be the way to go.
If you want a "readable" key, use the single-key-description
function. To be thorough, you should also use prin1-to-string
:
(defun get-last-key ()
(interactive)
(let* ((vect (recent-keys))
(key (aref vect (1- (length vect)))))
(if (or (integerp key) (symbolp key) (listp key))
(single-key-description key)
(prin1-to-string key nil)))
)
(get-last-key); C-xC-e will give the string "C-e"