1

I want to run a function and do different actions based on the next key sequence.

I have this.

(defun ask-for-C-b-or-M-b (key-sequence)
   (interactive "KPress Key")
   (cond
    ((seq-set-equal-p key-sequence
                      [?\C-b]) "C-b pressed")
    ((seq-set-equal-p key-sequence
                      [?\M-b]) "M-b pressed")))

 (call-interactively 'ask-for-C-b-or-M-b)

This works for C-b but not for M-b and I would like to use the kbd function

(kbd "C-b") instead of [?\C-b]
(kbd "M-b") instead of [?\M-b]

I'm trying to get extra keys in a hydra without making a new hydra

(defhydra hydra-test
   nil
   "test hydra"
   ("SPC" ask-for-C-b-or-M-b "Do complicated thing"))
2

I believe the missing link you're looking for is listify-key-sequence, which will "convert a key sequence to a list of events". Luckily, this works for both key sequences and for the values returned by kbd, so the following should return true:

(equal (listify-key-sequence [?\C-b])
       (listify-key-sequence (kbd "C-b")))

So your function would be:

(defun ask-for-C-b-or-M-b (key-sequence)
   (interactive "KPress Key")
   (cond
    ((seq-set-equal-p (listify-key-sequence key-sequence)
                      (listify-key-sequence (kbd "C-b")))
     "C-b pressed")
    ((seq-set-equal-p (listify-key-sequence key-sequence)
                      (listify-key-sequence (kbd "M-b")))
     "M-b pressed")))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.