I want an Emacs Lisp function to have two different behaviors depending on whether the control key is pressed or not.

Is there a way to query the state of the control key? Thanks.

There's no facility to check the current state of a modifier key, but you can check whether the last key used to run the command was used with "control" or not:

(memq 'control (event-modifiers last-command-event))

Note that TAB and RET are actually the same as C-i and C-m, so they are considered as having a "control" modifier.

  • Thanks Stefan. Unfortunately, the last-command-event is a selection from a menu, and that does not seem to include the state of the modifier keys. The action function of the selected menu-item is the code that I want to have modifiable behavior. – DawgBone Oct 20 '14 at 20:17
  • 2
    The "Emacs way" would be to let C-u (or any other universal argument) modify the behavior. You can pick this up using either interactive or current-prefix-argument. – Lindydancer Oct 21 '14 at 7:00

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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