Go to the first, previous, next, last section, table of contents.

8. The keyboard

8.1. What is the difference in in key sequences between XEmacs and GNU Emacs?

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)
is a convenience shorthand for the sequence [(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.
[(meta a)]
is The Right Thing. It corresponds in a one-to-one way with the internal representation of key-sequences in keymaps.
[Meta-a]
is typical FSF Emacs 19 brain damage. As is usual, an existing, functional design is ignored (XEmacs) and an incompatible and technically worse kludge is used.
"\ea"
is compatible with Emacs 18, but suffers from ASCII Seven-Bit Brain Damage. I also find it harder to read. Use this if you're trying to write code which works in every Emacs, but be aware that you can not express all Possible key-sequences (control-9, f1, etc.) using this.

8.2. How can I make XEmacs recognize the Alt key of my HP workstation as a Meta key?

Put the following line into a file and load it with xmodmap(1) before starting XEmacs:

remove Mod1 = Mode_switch

8.3. How can I stop the down-arrow-key from adding empty lines at the bottom of my buffers?

Add the following line to your `.emacs' file:

(setq next-line-add-newlines nil)

8.4. I wonder if there is an interactive function that can generate "fake" keyboard events. This way, I could simply map them inside XEmacs.

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)))

8.5. I am trying to bind C-. to scroll up by one line and C-; to scroll down by one line...

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.)

8.6. I cannot manage to globally bind my Delete key to something other than the default. How does one do this?

(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.

8.7. How can I bind complex functions (or macros) to keys?

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).

8.8. Can the cursor keys scroll the screen a line at a time, rather than the default half page jump? I tend it to find it disorienting.

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)

8.9. How to map "Help" key alone on Sun type4 keyboard?

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:'.

8.10. How can you type in special characters in XEmacs?

One way is to use the package `x-compose'. Then you can use sequences like Compose " a to get d (a-umlaut), etc.

8.11. Why does (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.


Go to the first, previous, next, last section, table of contents.