| 1590 | | @node Translating Input |
|---|
| 1591 | | @subsection Translating Input Events |
|---|
| 1592 | | @cindex translating input events |
|---|
| 1593 | | |
|---|
| 1594 | | This section describes features for translating input events into |
|---|
| 1595 | | other input events before they become part of key sequences. These |
|---|
| 1596 | | features apply to each event in the order they are described here: each |
|---|
| 1597 | | event is first modified according to @code{extra-keyboard-modifiers}, |
|---|
| 1598 | | then translated through @code{keyboard-translate-table} (if applicable), |
|---|
| 1599 | | and finally decoded with the specified keyboard coding system. If it is |
|---|
| 1600 | | being read as part of a key sequence, it is then added to the sequence |
|---|
| 1601 | | being read; then subsequences containing it are checked first with |
|---|
| 1602 | | @code{function-key-map} and then with @code{key-translation-map}. |
|---|
| 1603 | | |
|---|
| 1604 | | @c Emacs 19 feature |
|---|
| 1605 | | @defvar extra-keyboard-modifiers |
|---|
| 1606 | | This variable lets Lisp programs ``press'' the modifier keys on the |
|---|
| 1607 | | keyboard. The value is a character. Only the modifiers of the |
|---|
| 1608 | | character matter. Each time the user types a keyboard key, it is |
|---|
| 1609 | | altered as if those modifier keys were held down. For instance, if |
|---|
| 1610 | | you bind @code{extra-keyboard-modifiers} to @code{?\C-\M-a}, then all |
|---|
| 1611 | | keyboard input characters typed during the scope of the binding will |
|---|
| 1612 | | have the control and meta modifiers applied to them. The character |
|---|
| 1613 | | @code{?\C-@@}, equivalent to the integer 0, does not count as a control |
|---|
| 1614 | | character for this purpose, but as a character with no modifiers. |
|---|
| 1615 | | Thus, setting @code{extra-keyboard-modifiers} to zero cancels any |
|---|
| 1616 | | modification. |
|---|
| 1617 | | |
|---|
| 1618 | | When using a window system, the program can ``press'' any of the |
|---|
| 1619 | | modifier keys in this way. Otherwise, only the @key{CTL} and @key{META} |
|---|
| 1620 | | keys can be virtually pressed. |
|---|
| 1621 | | |
|---|
| 1622 | | Note that this variable applies only to events that really come from |
|---|
| 1623 | | the keyboard, and has no effect on mouse events or any other events. |
|---|
| 1624 | | @end defvar |
|---|
| 1625 | | |
|---|
| 1626 | | @defvar keyboard-translate-table |
|---|
| 1627 | | This variable is the translate table for keyboard characters. It lets |
|---|
| 1628 | | you reshuffle the keys on the keyboard without changing any command |
|---|
| 1629 | | bindings. Its value is normally a char-table, or else @code{nil}. |
|---|
| 1630 | | (It can also be a string or vector, but this is considered obsolete.) |
|---|
| 1631 | | |
|---|
| 1632 | | If @code{keyboard-translate-table} is a char-table |
|---|
| 1633 | | (@pxref{Char-Tables}), then each character read from the keyboard is |
|---|
| 1634 | | looked up in this char-table. If the value found there is |
|---|
| 1635 | | non-@code{nil}, then it is used instead of the actual input character. |
|---|
| 1636 | | |
|---|
| 1637 | | Note that this translation is the first thing that happens to a |
|---|
| 1638 | | character after it is read from the terminal. Record-keeping features |
|---|
| 1639 | | such as @code{recent-keys} and dribble files record the characters after |
|---|
| 1640 | | translation. |
|---|
| 1641 | | |
|---|
| 1642 | | Note also that this translation is done before the characters are |
|---|
| 1643 | | supplied to input methods (@pxref{Input Methods}). Use |
|---|
| 1644 | | @code{translation-table-for-input} (@pxref{Translation of Characters}), |
|---|
| 1645 | | if you want to translate characters after input methods operate. |
|---|
| 1646 | | @end defvar |
|---|
| 1647 | | |
|---|
| 1648 | | @defun keyboard-translate from to |
|---|
| 1649 | | This function modifies @code{keyboard-translate-table} to translate |
|---|
| 1650 | | character code @var{from} into character code @var{to}. It creates |
|---|
| 1651 | | the keyboard translate table if necessary. |
|---|
| 1652 | | @end defun |
|---|
| 1653 | | |
|---|
| 1654 | | Here's an example of using the @code{keyboard-translate-table} to |
|---|
| 1655 | | make @kbd{C-x}, @kbd{C-c} and @kbd{C-v} perform the cut, copy and paste |
|---|
| 1656 | | operations: |
|---|
| 1657 | | |
|---|
| 1658 | | @example |
|---|
| 1659 | | (keyboard-translate ?\C-x 'control-x) |
|---|
| 1660 | | (keyboard-translate ?\C-c 'control-c) |
|---|
| 1661 | | (keyboard-translate ?\C-v 'control-v) |
|---|
| 1662 | | (global-set-key [control-x] 'kill-region) |
|---|
| 1663 | | (global-set-key [control-c] 'kill-ring-save) |
|---|
| 1664 | | (global-set-key [control-v] 'yank) |
|---|
| 1665 | | @end example |
|---|
| 1666 | | |
|---|
| 1667 | | @noindent |
|---|
| 1668 | | On a graphical terminal that supports extended @acronym{ASCII} input, |
|---|
| 1669 | | you can still get the standard Emacs meanings of one of those |
|---|
| 1670 | | characters by typing it with the shift key. That makes it a different |
|---|
| 1671 | | character as far as keyboard translation is concerned, but it has the |
|---|
| 1672 | | same usual meaning. |
|---|
| 1673 | | |
|---|
| 1674 | | The remaining translation features translate subsequences of key |
|---|
| 1675 | | sequences being read. They are implemented in @code{read-key-sequence} |
|---|
| 1676 | | and have no effect on input read with @code{read-event}. |
|---|
| 1677 | | |
|---|
| 1678 | | @defvar function-key-map |
|---|
| 1679 | | This variable holds a keymap that describes the character sequences sent |
|---|
| 1680 | | by function keys on an ordinary character terminal. This keymap has the |
|---|
| 1681 | | same structure as other keymaps, but is used differently: it specifies |
|---|
| 1682 | | translations to make while reading key sequences, rather than bindings |
|---|
| 1683 | | for key sequences. |
|---|
| 1684 | | |
|---|
| 1685 | | If @code{function-key-map} ``binds'' a key sequence @var{k} to a vector |
|---|
| 1686 | | @var{v}, then when @var{k} appears as a subsequence @emph{anywhere} in a |
|---|
| 1687 | | key sequence, it is replaced with the events in @var{v}. |
|---|
| 1688 | | |
|---|
| 1689 | | For example, VT100 terminals send @kbd{@key{ESC} O P} when the |
|---|
| 1690 | | keypad @key{PF1} key is pressed. Therefore, we want Emacs to translate |
|---|
| 1691 | | that sequence of events into the single event @code{pf1}. We accomplish |
|---|
| 1692 | | this by ``binding'' @kbd{@key{ESC} O P} to @code{[pf1]} in |
|---|
| 1693 | | @code{function-key-map}, when using a VT100. |
|---|
| 1694 | | |
|---|
| 1695 | | Thus, typing @kbd{C-c @key{PF1}} sends the character sequence @kbd{C-c |
|---|
| 1696 | | @key{ESC} O P}; later the function @code{read-key-sequence} translates |
|---|
| 1697 | | this back into @kbd{C-c @key{PF1}}, which it returns as the vector |
|---|
| 1698 | | @code{[?\C-c pf1]}. |
|---|
| 1699 | | |
|---|
| 1700 | | Entries in @code{function-key-map} are ignored if they conflict with |
|---|
| 1701 | | bindings made in the minor mode, local, or global keymaps. The intent |
|---|
| 1702 | | is that the character sequences that function keys send should not have |
|---|
| 1703 | | command bindings in their own right---but if they do, the ordinary |
|---|
| 1704 | | bindings take priority. |
|---|
| 1705 | | |
|---|
| 1706 | | The value of @code{function-key-map} is usually set up automatically |
|---|
| 1707 | | according to the terminal's Terminfo or Termcap entry, but sometimes |
|---|
| 1708 | | those need help from terminal-specific Lisp files. Emacs comes with |
|---|
| 1709 | | terminal-specific files for many common terminals; their main purpose is |
|---|
| 1710 | | to make entries in @code{function-key-map} beyond those that can be |
|---|
| 1711 | | deduced from Termcap and Terminfo. @xref{Terminal-Specific}. |
|---|
| 1712 | | @end defvar |
|---|
| 1713 | | |
|---|
| 1714 | | @defvar key-translation-map |
|---|
| 1715 | | This variable is another keymap used just like @code{function-key-map} |
|---|
| 1716 | | to translate input events into other events. It differs from |
|---|
| 1717 | | @code{function-key-map} in two ways: |
|---|
| 1718 | | |
|---|
| 1719 | | @itemize @bullet |
|---|
| 1720 | | @item |
|---|
| 1721 | | @code{key-translation-map} goes to work after @code{function-key-map} is |
|---|
| 1722 | | finished; it receives the results of translation by |
|---|
| 1723 | | @code{function-key-map}. |
|---|
| 1724 | | |
|---|
| 1725 | | @item |
|---|
| 1726 | | Non-prefix bindings in @code{key-translation-map} override actual key |
|---|
| 1727 | | bindings. For example, if @kbd{C-x f} has a non-prefix binding in |
|---|
| 1728 | | @code{key-translation-map}, that translation takes effect even though |
|---|
| 1729 | | @kbd{C-x f} also has a key binding in the global map. |
|---|
| 1730 | | @end itemize |
|---|
| 1731 | | |
|---|
| 1732 | | Note however that actual key bindings can have an effect on |
|---|
| 1733 | | @code{key-translation-map}, even though they are overridden by it. |
|---|
| 1734 | | Indeed, actual key bindings override @code{function-key-map} and thus |
|---|
| 1735 | | may alter the key sequence that @code{key-translation-map} receives. |
|---|
| 1736 | | Clearly, it is better to avoid this type of situation. |
|---|
| 1737 | | |
|---|
| 1738 | | The intent of @code{key-translation-map} is for users to map one |
|---|
| 1739 | | character set to another, including ordinary characters normally bound |
|---|
| 1740 | | to @code{self-insert-command}. |
|---|
| 1741 | | @end defvar |
|---|
| 1742 | | |
|---|
| 1743 | | @cindex key translation function |
|---|
| 1744 | | You can use @code{function-key-map} or @code{key-translation-map} for |
|---|
| 1745 | | more than simple aliases, by using a function, instead of a key |
|---|
| 1746 | | sequence, as the ``translation'' of a key. Then this function is called |
|---|
| 1747 | | to compute the translation of that key. |
|---|
| 1748 | | |
|---|
| 1749 | | The key translation function receives one argument, which is the prompt |
|---|
| 1750 | | that was specified in @code{read-key-sequence}---or @code{nil} if the |
|---|
| 1751 | | key sequence is being read by the editor command loop. In most cases |
|---|
| 1752 | | you can ignore the prompt value. |
|---|
| 1753 | | |
|---|
| 1754 | | If the function reads input itself, it can have the effect of altering |
|---|
| 1755 | | the event that follows. For example, here's how to define @kbd{C-c h} |
|---|
| 1756 | | to turn the character that follows into a Hyper character: |
|---|
| 1757 | | |
|---|
| 1758 | | @example |
|---|
| 1759 | | @group |
|---|
| 1760 | | (defun hyperify (prompt) |
|---|
| 1761 | | (let ((e (read-event))) |
|---|
| 1762 | | (vector (if (numberp e) |
|---|
| 1763 | | (logior (lsh 1 24) e) |
|---|
| 1764 | | (if (memq 'hyper (event-modifiers e)) |
|---|
| 1765 | | e |
|---|
| 1766 | | (add-event-modifier "H-" e)))))) |
|---|
| 1767 | | |
|---|
| 1768 | | (defun add-event-modifier (string e) |
|---|
| 1769 | | (let ((symbol (if (symbolp e) e (car e)))) |
|---|
| 1770 | | (setq symbol (intern (concat string |
|---|
| 1771 | | (symbol-name symbol)))) |
|---|
| 1772 | | @end group |
|---|
| 1773 | | @group |
|---|
| 1774 | | (if (symbolp e) |
|---|
| 1775 | | symbol |
|---|
| 1776 | | (cons symbol (cdr e))))) |
|---|
| 1777 | | |
|---|
| 1778 | | (define-key function-key-map "\C-ch" 'hyperify) |
|---|
| 1779 | | @end group |
|---|
| 1780 | | @end example |
|---|
| 1781 | | |
|---|
| 1782 | | Finally, if you have enabled keyboard character set decoding using |
|---|
| 1783 | | @code{set-keyboard-coding-system}, decoding is done after the |
|---|
| 1784 | | translations listed above. @xref{Terminal I/O Encoding}. In future |
|---|
| 1785 | | Emacs versions, character set decoding may be done before the other |
|---|
| 1786 | | translations. |
|---|
| 1787 | | |
|---|