Custom Keyboard Shortcuts.ipynb
146 lines
| 3.7 KiB
| text/plain
|
TextLexer
Brian E. Granger
|
r17481 | { | |
Min RK
|
r18669 | "cells": [ | |
Brian E. Granger
|
r17496 | { | |
Min RK
|
r18669 | "cell_type": "markdown", | |
"metadata": {}, | |||
"source": [ | |||
"# Keyboard Shortcut Customization" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
"Starting with IPython 2.0 keyboard shortcuts in command and edit mode are fully customizable. These customizations are made using the IPython JavaScript API. Here is an example that makes the `r` key available for running a cell:" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": 7, | |||
"metadata": { | |||
"collapsed": false | |||
}, | |||
"outputs": [ | |||
Brian E. Granger
|
r17496 | { | |
Min RK
|
r18669 | "data": { | |
"application/javascript": [ | |||
"\n", | |||
"IPython.keyboard_manager.command_shortcuts.add_shortcut('r', {\n", | |||
" help : 'run cell',\n", | |||
" help_index : 'zz',\n", | |||
" handler : function (event) {\n", | |||
" IPython.notebook.execute_cell();\n", | |||
" return false;\n", | |||
" }}\n", | |||
");" | |||
], | |||
"text/plain": [ | |||
"<IPython.core.display.Javascript at 0x10e8d1890>" | |||
] | |||
}, | |||
Brian E. Granger
|
r17496 | "metadata": {}, | |
Min RK
|
r18669 | "output_type": "display_data" | |
} | |||
], | |||
"source": [ | |||
"%%javascript\n", | |||
"\n", | |||
"IPython.keyboard_manager.command_shortcuts.add_shortcut('r', {\n", | |||
" help : 'run cell',\n", | |||
" help_index : 'zz',\n", | |||
" handler : function (event) {\n", | |||
" IPython.notebook.execute_cell();\n", | |||
" return false;\n", | |||
" }}\n", | |||
");" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
"There are a couple of points to mention about this API:\n", | |||
"\n", | |||
"* The `help_index` field is used to sort the shortcuts in the Keyboard Shortcuts help dialog. It defaults to `zz`.\n", | |||
"* 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.\n", | |||
"* If you don't need a `help` or `help_index` field, you can simply pass a function as the second argument to `add_shortcut`." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": 11, | |||
"metadata": { | |||
"collapsed": false | |||
}, | |||
"outputs": [ | |||
Brian E. Granger
|
r17496 | { | |
Min RK
|
r18669 | "data": { | |
"application/javascript": [ | |||
"\n", | |||
"IPython.keyboard_manager.command_shortcuts.add_shortcut('r', function (event) {\n", | |||
" IPython.notebook.execute_cell();\n", | |||
" return false;\n", | |||
"});" | |||
], | |||
"text/plain": [ | |||
"<IPython.core.display.Javascript at 0x1019baf90>" | |||
] | |||
}, | |||
Brian E. Granger
|
r17496 | "metadata": {}, | |
Min RK
|
r18669 | "output_type": "display_data" | |
} | |||
], | |||
"source": [ | |||
"%%javascript\n", | |||
"\n", | |||
"IPython.keyboard_manager.command_shortcuts.add_shortcut('r', function (event) {\n", | |||
" IPython.notebook.execute_cell();\n", | |||
" return false;\n", | |||
"});" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
"Likewise, to remove a shortcut, use `remove_shortcut`:" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"execution_count": 8, | |||
"metadata": { | |||
"collapsed": false | |||
}, | |||
"outputs": [ | |||
Brian E. Granger
|
r17496 | { | |
Min RK
|
r18669 | "data": { | |
"application/javascript": [ | |||
"\n", | |||
"IPython.keyboard_manager.command_shortcuts.remove_shortcut('r');" | |||
], | |||
"text/plain": [ | |||
"<IPython.core.display.Javascript at 0x10e8d1950>" | |||
] | |||
}, | |||
Brian E. Granger
|
r17496 | "metadata": {}, | |
Min RK
|
r18669 | "output_type": "display_data" | |
Brian E. Granger
|
r17496 | } | |
], | |||
Min RK
|
r18669 | "source": [ | |
"%%javascript\n", | |||
"\n", | |||
"IPython.keyboard_manager.command_shortcuts.remove_shortcut('r');" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
"If you want your keyboard shortcuts to be active for all of your notebooks, put the above API calls into your `<profile>/static/custom/custom.js` file." | |||
] | |||
Brian E. Granger
|
r17496 | } | |
Min RK
|
r18669 | ], | |
"metadata": {}, | |||
"nbformat": 4, | |||
"nbformat_minor": 0 | |||
Brian E. Granger
|
r17481 | } |