11

I'm trying to add some functionality to someone else's package, and so I'd like to respect their patterns. Unfortunately, one of these patterns is to not use kbd.

I need to bind a function to C-S-b, but I can't figure out how. I know how to do this with a single modifier (e.g. "\S-b"), but I can't get it to work with multiple modifiers. I know I can just evaluate (kbd "C-S-b") and use its output ([33554434]), but I'd like something easier to read.

Here are a few things I've tried:

(define-key emacs-lisp-mode-map
  "\C-\S-b" 'test-command)
;;; Invalid modifier

(define-key emacs-lisp-mode-map
  [C-S-b] 'test-command)
;;; Does nothing

(define-key emacs-lisp-mode-map
  "\C-B" 'test-command)
;;; Binds C-b
Malabarba
  • 22,878
  • 6
  • 78
  • 163

1 Answers1

15

You are missing a ? and two backslashes in the vector representation:

(global-set-key [?\C-\S-b] 'test-command)

The section on Key Sequences in the Elisp manual says:

Key sequences containing function keys, mouse button events, system events, or non-ASCII characters such as C-= or H-a cannot be represented as strings; they have to be represented as vectors.

In the vector representation, each element of the vector represents an input event, in its Lisp form. For example, the vector [?\C-x ?l] represents the key sequence C-x l.

And under Other Character Modifier Bits it says:

The Lisp syntax for the shift bit is \S-; thus, ?\C-\S-o or ?\C-\S-O represents the shifted-control-o character.

itsjeyd
  • 14,586
  • 3
  • 58
  • 87
  • 1
    I had been trying to understand what the question marks mean. Thanks to your reply, I found these: [Ctl-Char Syntax ?\C-](http://www.gnu.org/software/emacs/manual/html_node/elisp/Ctl_002dChar-Syntax.html) and [Meta-Char Syntax ?\M-](http://www.gnu.org/software/emacs/manual/html_node/elisp/Meta_002dChar-Syntax.html) – Kaushal Modi Nov 13 '14 at 11:48
  • And yes, Malabarba's question is answered in this page on [Other Modifier Bits](http://www.gnu.org/software/emacs/manual/html_node/elisp/Other-Char-Bits.html). – Kaushal Modi Nov 13 '14 at 11:53
  • @kaushalmodi Thanks for the links! They're very useful in gaining a deeper understanding of what's going on. – itsjeyd Nov 13 '14 at 12:16