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

How do I use function keys under X Windows?

This depends on whether you are running Emacs inside a terminal emulator window, or whether you are allowing Emacs to create its own X window. You can tell which you are doing by noticing whether Emacs creates a new window when you start it.

If you are running Emacs inside a terminal emulator window, then it behaves exactly as it does on any other tty. In this case, for function keys to be useful, they must generate character sequences that are sent to the programs running inside the window as input. The `xterm' program has two different sets of character sequences that it generates when function keys are pressed, depending on the sunFunctionKeys X resource and the -sf and +sf command line options. (To find out what these key sequences are, see section How do I tell what characters my function or arrow keys emit?.) In addition, with xterm, you can override what key sequence a specific function key (or any other key) will generate with the `translations' resource. This, for example:

  XTerm.VT100.Translations: #override \
    <KeyPress>F1: string(0x1b) string("[xyzzy")

makes the function key F1 generate the character sequence "ESC [xyzzy".

On the other hand, if Emacs is managing its own X window, the following description applies. Emacs receives `KeyPress' events from the X server when a key is pressed while the keyboard focus is in its window. The KeyPress event contains an X "keysym" code, which is simply an arbitrary number corresponding to the name of the keysym, and information on which "modifiers" such as `control' and `shift' are active. For example, the `Tab' keysym is 0xff09. (Generally, a key on the keyboard will generate a keysym whose name is the same as the label on the key, ie. the `Tab' key will normally generate the `Tab' keysym. This can be changed with the xmodmap program.) Emacs recognizes all the keysyms that correspond to standard ASCII characters and internally uses the ASCII character instead.

(WARNING: I am about to describe a gross, disgusting hack to you, have your barf bag ready.)

When Emacs receives the X keysym of one of the arrow keys, it behaves the same as if it had received a letter key with the control modifier down as follows (this is hard-coded):

  Up    becomes C-p
  Down  becomes C-n
  Right becomes C-f
  Left  becomes C-b

The way Emacs treats other keysyms depends on what kind of machine it was compiled on. The type of the display machine is irrelevant! Function keys are mapped internally to escape sequences, while other keys are completely ignored.

  1. If compiled on a Sun, Emacs recognizes these X keysyms that are normally on a Sun keyboard:
      F1 through F9
      L1 through L10 (same as F11 through F20)
      R1 through R15 (same as F21 through F35)
        (The keys labelled R8, R10, R12, and R14 usually are mapped to the
         X keysyms Up, Left, Right, and Down.)
      Break (the `Alternate' key is given this keysym)
    These keys work like Sun function keys. When Emacs recieves the keysym, it will internally use character sequences that look like "ESC [ ### z", where ### is replaced by a number. The character sequences are identical to those generated by Sun's keyboard under SunView. Any function key not listed above generates "ESC [ - 1 z". In order to use these key sequences, they should be bound to commands using the standard key binding methods, just as if Emacs were running on a regular terminal. WARNING: F11 and L1 are the same keysym in X, as are F12 and L2, etc. {Yes, this is stupid. Complain to the X consortium.}
  2. If not compiled on a Sun, the function keys will appear to Emacs in a way remarkably similar to the keys of a DEC LK201 keyboard (used on some VT series terminals). These X keysyms will be recognized:
      F1 through F20
      Help (treated same as F15)
      Menu (treated same as F16, is the LK201 `Do' key)
      Insert (LK201 `Insert Here' key)
      Prior (LK201 `Prev Screen' key *** ONLY IN 18.58 AND LATER ***)
      Next (LK201 `Next Screen' key *** ONLY IN 18.58 AND LATER ***)
    And finally, the LK201 key labelled `Remove' (or `Delete') is often mapped to the Delete keysym which generates the DEL character (C-?) instead of the key sequence given by the LK201 `Remove' key. It may also be mapped to some other keysym, such as `_Remove', in which case you can't use it from within Emacs at all. Each function key will be internally converted to a character sequence that looks like "ESC [ ## ~", where ## is replaced by a number. The character sequences are identical to those generated by a LK201 keyboard. Any function key not listed above generates "ESC [ - 1 ~".

For the complete list of the numbers which are generated by the function keys, look in the file src/x11term.c at the definitions of the function stringFuncVal.

If you are running Emacs on a Sun machine, even if your X display is running on a non-Sun machine (eg., an X terminal), you get the setup described above for Suns. The determining factor is what type of machine Emacs is running (was compiled) on, not what type of machine your X display is on.

If you have function keys not listed above on your keyboard, you can use `xmodmap' to change their keysym assignments to get keys that Emacs will recognize, but that may screw up other programs.

X resources are not used by Emacs to affect the key sequences generated. In particular, there are no X key "translations" for Emacs.

If you have function keys not listed above and you don't want to use xmodmap to change their names, you might want to make a modification to your Emacs. Johan Vromans <jv@mh.nl> has made available a patch for Emacs that adds the x-rebind-key function of Epoch to Emacs 18.58. This allows another layer of key rebinding before Emacs even sees the keys, and in this layer you can rebind all of the keys and modifier combinations as well.

Anonymous FTP:


Johan Vromans explains what this buys for you:

  After implementing this, all keyboard keys can be configured to send
  user definable sequences, eg.,
    (x-rebind-key "KP_F1" 0 "\033OP")
  This will have the keypad key PF1 send the sequence "ESC O P", just like
  an ordinary VT series terminal.

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