[gnu.emacs.bug] can a command know its invoking key sequence?

mic@THEORY.LCS.MIT.EDU (11/16/89)

This is a help request.

If a command is bound to several different key sequences, is there a way
for it to know which way it was called?  The closest thing I've found is to
parse over the last hundred keystrokes reported by (recent-keys), saving
the last valid key sequence (there are pathological examples showing that
this method cannot always work correctly).  I am hoping for a more
efficient and correct solution.

---- Mic Grigni (on vacation for a day after STOC submissions)

PS: I know of course that commands shouldn't generally depend on their
    calling keystroke, but I think I have a semi-valid use here.  I want a 
    function that will auto-detect and load definitions for various 
    different kinds of keyboards, where emacs doesn't know what kind of
    keyboard you have until you hit a function key (TERM doesn't tell you).

kjones@talos.uucp (Kyle Jones) (11/16/89)

mic@THEORY.LCS.MIT.EDU writes:
 > If a command is bound to several different key sequences, is there a way
 > for it to know which way it was called?

Try using (this-command-keys).

C-h f this-command-keys yields

   this-command-keys:
   Return string of the keystrokes that invoked this command.

Note that this string includes the keys used to provide the prefix
argument, if any.

merlyn@iwarp.intel.com (Randal Schwartz) (11/17/89)

In article <8911151740.AA16000@hummingbird.LCS.MIT.EDU>, mic@THEORY writes:
| This is a help request.
| 
| If a command is bound to several different key sequences, is there a way
| for it to know which way it was called?  The closest thing I've found is to
| parse over the last hundred keystrokes reported by (recent-keys), saving
| the last valid key sequence (there are pathological examples showing that
| this method cannot always work correctly).  I am hoping for a more
| efficient and correct solution.

(describe-function 'this-command-keys) ==>
| this-command-keys:
| Return string of the keystrokes that invoked this command.

Here's some code I wrote last summer that uses it:

;;; original by merlyn -- LastEditDate="Tue May 16 12:45:11 1989"

(defun rebind-this-key (key command)
  "Set KEY to invoke COMMAND, preferring a local binding if present.
Interactively, KEY is the key that invoked this routine, while COMMAND
is prompted for, making this function useful to bind to unused or
unallocated keys."
  (interactive
   (let ((tck (this-command-keys)))
     (list
      tck
      (read-command (format "%s is currently undefined; define as: "
			    (key-description tck))))))
  (let ((map (current-local-map)))
    (if map
	(let ((lk (lookup-key map key)))
	  (if (or (null lk) (numberp lk))
	      (setq map (current-global-map))))
      (setq map (current-global-map)))
    (define-key map key command)))
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/