The real question might be rephrased as "When should one use the quoted list, vector, or escaped string representations of key sequences?" Is there any particular advantage to one representation over another?
>From Richard Mlynarik <mly@adoc.xerox.com>:
[(meta a)]
.
(global-set-key 'a 'foo)
means the same thing as
(global-set-key '[a] 'foo)
. It could be argued that allowing
such a shorthand just leads to sloppiness and bugs, but it's there, and
it isn't likely to go away.
Put the following line into a file and load it with xmodmap(1) before starting XEmacs:
remove Mod1 = Mode_switch
Add the following line to your `.emacs' file:
(setq next-line-add-newlines nil)
This seems to work:
(defun cg--generate-char-event (ch) "Generate an event, as if ch has been typed" (dispatch-event (character-to-event ch))) ;; Backspace and Delete stuff (global-set-key '(backspace) '(lambda () (interactive) (cg--generate-char-event 127))) (global-set-key '(unknown_keysym_0x4) '(lambda () (interactive) (cg--generate-char-event 4)))
Add the following (Thanks to Richard Mlynarik <mly@adoc.xerox.com> and Wayne Newberry <wayne@zen.cac.stratus.com>) to `.emacs':
(defun scroll-up-one-line () (interactive) (scroll-up 1)) (defun scroll-down-one-line () (interactive) (scroll-down 1)) (global-set-key [(control ?.)] 'scroll-up-one-line) ; C-. (global-set-key [(control ?;)] 'scroll-down-one-line) ; C-;
The key point is that you can only bind simple functions to keys; you can not bind a key to a function that you're also passing arguments to. (See <A HREF="#ss8.7">8.7 How can I bind complex functions for a better answer.)
(defun Foo () (interactive) (message "You hit DELETE")) (global-set-key "\C-?" 'Foo)
However, some modes explicitly bind Delete, so you would need to
add a hook that does local-set-key
for them.
As an example, say you want the PASTE key on a Sun keyboard to insert the current Primary X selection at point. You can accomplish this with:
(define-key global-map 'f18 'x-insert-selection)
However, this only works if there is a current X selection (the selection will be highlighted). The functionality I like is for the PASTE key to insert the current X selection if there is one, otherwise insert the contents of the clipboard. To do this you need to pass arguments to x-insert-selection. This is done by wrapping the call in a 'lambda form:
(define-key global-map 'f18 (function (lambda () (interactive) (x-insert-selection t nil))))
This binds the 'f18 key to a "generic" functional object. The interactive spec is required because only interactive functions can be bound to keys. Also take a look at the doc for "function".
For the FAQ example you could use:
(global-set-key [(control ?.)] (function (lambda () (interactive) (scroll-up 1)))) (global-set-key [(control ?;)] (function (lambda () (interactive) (scroll-up -1))))
This is fine if you only need a few functions within the lambda body. If you're doing more it's cleaner to define a separate function as in the original FAQ example (question 11.3).
Try this:
(defun scroll-one-line-up (&optional arg) "Scroll the selected window up (forward in the text) one line (or N lines)." (interactive "p") (scroll-up (or arg 1))) (defun scroll-one-line-down (&optional arg) "Scroll the selected window down (backward in the text) one line (or N)." (interactive "p") (scroll-down (or arg 1))) (global-set-key 'up 'scroll-one-line-up) (global-set-key 'down 'scroll-one-line-down)
The following will also work but will affect more than just the cursor keys (i.e. C-n and C-p):
(setq scroll-step 1)
The following works in GNU Emacs 19:
(global-set-key [help] 'help-command) ;; Help
The following works in XEmacs 19.13 with the addition of shift:
(global-set-key [(shift help)] 'help-command) ;; Help
But it doesn't work alone. This is in the file `PROBLEMS' which should have come with your XEmacs installation:
Emacs ignores the help key when running OLWM.
OLWM grabs the help key, and retransmits it to the appropriate client using XSendEvent. Allowing Emacs to react to synthetic events is a security hole, so this is turned off by default. You can enable it by setting the variable x-allow-sendevents to t. You can also cause fix this by telling OLWM to not grab the help key, with the null binding `OpenWindows.KeyboardCommand.Help:'.
One way is to use the package `x-compose'. Then you can use sequences like Compose " a to get d (a-umlaut), etc.
(define-key global-map [ delete-forward ] 'delete-char)
complain of not being able to bind an unknown key?Try this instead:
(define-key global-map [delete_forward] 'delete-char)
and it will work.
What you are seeing above is a bug due to code that is trying to check for FSF Emacs bogosity like
(define-key global-map [C-M-a] 'delete-char)
which otherwise would cause no errors but would not result in the expected behavior.