File: Custom%20Keyboard%20Shortcuts.rst

package info (click to toggle)
jupyter-notebook 4.2.3-4~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 7,804 kB
  • sloc: python: 8,698; makefile: 240; sh: 74
file content (71 lines) | stat: -rw-r--r-- 2,539 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

`View the original notebook on nbviewer <http://nbviewer.jupyter.org/github/jupyter/notebook/blob/master/docs/source/examples/Notebook/Custom%20Keyboard%20Shortcuts.ipynb>`__

Keyboard Shortcut Customization
===============================

Starting with IPython 2.0 keyboard shortcuts in command and edit mode
are fully customizable. These customizations are made using the Jupyter
JavaScript API. Here is an example that makes the ``r`` key available
for running a cell:

.. code:: python

    %%javascript
    
    Jupyter.keyboard_manager.command_shortcuts.add_shortcut('r', {
        help : 'run cell',
        help_index : 'zz',
        handler : function (event) {
            IPython.notebook.execute_cell();
            return false;
        }}
    );

"By default the keypress ``r``, while in command mode, changes the type
of the selected cell to ``raw``. This shortcut is overridden by the code
in the previous cell, and thus the action no longer be available via the
keypress ``r``."

There are a couple of points to mention about this API:

-  The ``help_index`` field is used to sort the shortcuts in the
   Keyboard Shortcuts help dialog. It defaults to ``zz``.
-  When a handler returns ``false`` it indicates that the event should
   stop propagating and the default action should not be performed. For
   further details about the ``event`` object or event handling, see the
   jQuery docs.
-  If you don't need a ``help`` or ``help_index`` field, you can simply
   pass a function as the second argument to ``add_shortcut``.

.. code:: python

    %%javascript
    
    Jupyter.keyboard_manager.command_shortcuts.add_shortcut('r', function (event) {
        IPython.notebook.execute_cell();
        return false;
    });

Likewise, to remove a shortcut, use ``remove_shortcut``:

.. code:: python

    %%javascript
    
    Jupyter.keyboard_manager.command_shortcuts.remove_shortcut('r');

If you want your keyboard shortcuts to be active for all of your
notebooks, put the above API calls into your ``custom.js`` file.

Of course we provide name for majority of existing action so that you do
not have to re-write everything, here is for example how to bind ``r``
back to it's initial behavior:

.. code:: python

    %%javascript
    
    Jupyter.keyboard_manager.command_shortcuts.add_shortcut('r', 'jupyter-notebook:change-cell-to-raw');

`View the original notebook on nbviewer <http://nbviewer.jupyter.org/github/jupyter/notebook/blob/master/docs/source/examples/Notebook/Custom%20Keyboard%20Shortcuts.ipynb>`__