Show More
@@ -0,0 +1,251 b'' | |||
|
1 | //---------------------------------------------------------------------------- | |
|
2 | // Copyright (C) 2011 The IPython Development Team | |
|
3 | // | |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
|
5 | // the file COPYING, distributed as part of this software. | |
|
6 | //---------------------------------------------------------------------------- | |
|
7 | ||
|
8 | //============================================================================ | |
|
9 | // Keyboard management | |
|
10 | //============================================================================ | |
|
11 | ||
|
12 | IPython.namespace('IPython.keyboard'); | |
|
13 | ||
|
14 | IPython.keyboard = (function (IPython) { | |
|
15 | "use strict"; | |
|
16 | ||
|
17 | // Setup global keycodes and inverse keycodes. | |
|
18 | ||
|
19 | // See http://unixpapa.com/js/key.html for a complete description. The short of | |
|
20 | // it is that there are different keycode sets. Firefox uses the "Mozilla keycodes" | |
|
21 | // and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same | |
|
22 | // but have minor differences. | |
|
23 | ||
|
24 | // These apply to Firefox, (Webkit and IE) | |
|
25 | var _keycodes = { | |
|
26 | 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73, | |
|
27 | 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82, | |
|
28 | 's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90, | |
|
29 | '1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54, | |
|
30 | '7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48, | |
|
31 | '[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191, | |
|
32 | '\\ |': 220, '\' "': 222, | |
|
33 | 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100, | |
|
34 | 'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105, | |
|
35 | 'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111, | |
|
36 | 'f1': 112, 'f2': 113, 'f3': 114, 'f4': 115, 'f5': 116, 'f6': 117, 'f7': 118, | |
|
37 | 'f8': 119, 'f9': 120, 'f11': 122, 'f12': 123, 'f13': 124, 'f14': 125, 'f15': 126, | |
|
38 | 'backspace': 8, 'tab': 9, 'enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18, | |
|
39 | 'meta': 91, 'capslock': 20, 'esc': 27, 'space': 32, 'pageup': 33, 'pagedown': 34, | |
|
40 | 'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40, | |
|
41 | 'insert': 45, 'delete': 46, 'numlock': 144, | |
|
42 | }; | |
|
43 | ||
|
44 | // These apply to Firefox and Opera | |
|
45 | var _mozilla_keycodes = { | |
|
46 | '; :': 59, '= +': 61, '- _': 173, 'meta': 224 | |
|
47 | } | |
|
48 | ||
|
49 | // This apply to Webkit and IE | |
|
50 | var _ie_keycodes = { | |
|
51 | '; :': 186, '= +': 187, '- _': 189, | |
|
52 | } | |
|
53 | ||
|
54 | var browser = IPython.utils.browser[0]; | |
|
55 | var platform = IPython.utils.platform; | |
|
56 | ||
|
57 | if (browser === 'Firefox' || browser === 'Opera') { | |
|
58 | $.extend(_keycodes, _mozilla_keycodes); | |
|
59 | } else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') { | |
|
60 | $.extend(_keycodes, _ie_keycodes); | |
|
61 | } | |
|
62 | ||
|
63 | var keycodes = {}; | |
|
64 | var inv_keycodes = {}; | |
|
65 | for (var name in _keycodes) { | |
|
66 | var names = name.split(' '); | |
|
67 | if (names.length === 1) { | |
|
68 | var n = names[0] | |
|
69 | keycodes[n] = _keycodes[n] | |
|
70 | inv_keycodes[_keycodes[n]] = n | |
|
71 | } else { | |
|
72 | var primary = names[0]; | |
|
73 | var secondary = names[1]; | |
|
74 | keycodes[primary] = _keycodes[name] | |
|
75 | keycodes[secondary] = _keycodes[name] | |
|
76 | inv_keycodes[_keycodes[name]] = primary | |
|
77 | } | |
|
78 | } | |
|
79 | ||
|
80 | var normalize_key = function (key) { | |
|
81 | return inv_keycodes[keycodes[key]]; | |
|
82 | } | |
|
83 | ||
|
84 | var normalize_shortcut = function (shortcut) { | |
|
85 | // Put a shortcut into normalized form: | |
|
86 | // 1. Make lowercase | |
|
87 | // 2. Replace cmd by meta | |
|
88 | // 3. Sort '+' separated modifiers into the order alt+ctrl+meta+shift | |
|
89 | // 4. Normalize keys | |
|
90 | shortcut = shortcut.toLowerCase().replace('cmd', 'meta'); | |
|
91 | var values = shortcut.split("+"); | |
|
92 | if (values.length === 1) { | |
|
93 | return normalize_key(values[0]) | |
|
94 | } else { | |
|
95 | var modifiers = values.slice(0,-1); | |
|
96 | var key = normalize_key(values[values.length-1]); | |
|
97 | modifiers.sort(); | |
|
98 | return modifiers.join('+') + '+' + key; | |
|
99 | } | |
|
100 | } | |
|
101 | ||
|
102 | var shortcut_to_event = function (shortcut, type) { | |
|
103 | // Convert a shortcut (shift+r) to a jQuery Event object | |
|
104 | type = type || 'keydown'; | |
|
105 | shortcut = normalize_shortcut(shortcut); | |
|
106 | var values = shortcut.split("+"); | |
|
107 | var modifiers = values.slice(0,-1); | |
|
108 | var key = values[values.length-1]; | |
|
109 | var opts = {which: keycodes[key]}; | |
|
110 | if (modifiers.indexOf('alt') !== -1) {opts.altKey = true;} | |
|
111 | if (modifiers.indexOf('ctrl') !== -1) {opts.ctrlKey = true;} | |
|
112 | if (modifiers.indexOf('meta') !== -1) {opts.metaKey = true;} | |
|
113 | if (modifiers.indexOf('shift') !== -1) {opts.shiftKey = true;} | |
|
114 | return $.Event(type, opts); | |
|
115 | } | |
|
116 | ||
|
117 | var event_to_shortcut = function (event) { | |
|
118 | // Convert a jQuery Event object to a shortcut (shift+r) | |
|
119 | var shortcut = ''; | |
|
120 | var key = inv_keycodes[event.which] | |
|
121 | if (event.altKey && key !== 'alt') {shortcut += 'alt+';} | |
|
122 | if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl+';} | |
|
123 | if (event.metaKey && key !== 'meta') {shortcut += 'meta+';} | |
|
124 | if (event.shiftKey && key !== 'shift') {shortcut += 'shift+';} | |
|
125 | shortcut += key; | |
|
126 | return shortcut | |
|
127 | } | |
|
128 | ||
|
129 | var trigger_keydown = function (shortcut, element) { | |
|
130 | // Trigger shortcut keydown on an element | |
|
131 | element = element || document; | |
|
132 | element = $(element); | |
|
133 | var event = shortcut_to_event(shortcut, 'keydown'); | |
|
134 | element.trigger(event); | |
|
135 | } | |
|
136 | ||
|
137 | ||
|
138 | // Shortcut manager class | |
|
139 | ||
|
140 | var ShortcutManager = function (delay) { | |
|
141 | this._shortcuts = {} | |
|
142 | this._counts = {} | |
|
143 | this._timers = {} | |
|
144 | this.delay = delay || 800; // delay in milliseconds | |
|
145 | } | |
|
146 | ||
|
147 | ShortcutManager.prototype.help = function () { | |
|
148 | var help = []; | |
|
149 | for (var shortcut in this._shortcuts) { | |
|
150 | var help_string = this._shortcuts[shortcut]['help']; | |
|
151 | var help_index = this._shortcuts[shortcut]['help_index']; | |
|
152 | if (help_string) { | |
|
153 | if (platform === 'MacOS') { | |
|
154 | shortcut = shortcut.replace('meta', 'cmd'); | |
|
155 | } | |
|
156 | help.push({ | |
|
157 | shortcut: shortcut, | |
|
158 | help: help_string, | |
|
159 | help_index: help_index} | |
|
160 | ); | |
|
161 | } | |
|
162 | } | |
|
163 | help.sort(function (a, b) { | |
|
164 | if (a.help_index > b.help_index) | |
|
165 | return 1; | |
|
166 | if (a.help_index < b.help_index) | |
|
167 | return -1; | |
|
168 | return 0; | |
|
169 | }); | |
|
170 | return help; | |
|
171 | } | |
|
172 | ||
|
173 | ShortcutManager.prototype.clear_shortcuts = function () { | |
|
174 | this._shortcuts = {}; | |
|
175 | } | |
|
176 | ||
|
177 | ShortcutManager.prototype.add_shortcut = function (shortcut, data) { | |
|
178 | if (typeof(data) === 'function') { | |
|
179 | data = {help: '', help_index: '', handler: data} | |
|
180 | } | |
|
181 | data.help_index = data.help_index || ''; | |
|
182 | data.help = data.help || ''; | |
|
183 | data.count = data.count || 1; | |
|
184 | if (data.help_index === '') { | |
|
185 | data.help_index = 'zz'; | |
|
186 | } | |
|
187 | shortcut = normalize_shortcut(shortcut); | |
|
188 | this._counts[shortcut] = 0; | |
|
189 | this._shortcuts[shortcut] = data; | |
|
190 | } | |
|
191 | ||
|
192 | ShortcutManager.prototype.add_shortcuts = function (data) { | |
|
193 | for (var shortcut in data) { | |
|
194 | this.add_shortcut(shortcut, data[shortcut]); | |
|
195 | } | |
|
196 | } | |
|
197 | ||
|
198 | ShortcutManager.prototype.remove_shortcut = function (shortcut) { | |
|
199 | shortcut = normalize_shortcut(shortcut); | |
|
200 | delete this._counts[shortcut]; | |
|
201 | delete this._shortcuts[shortcut]; | |
|
202 | } | |
|
203 | ||
|
204 | ShortcutManager.prototype.count_handler = function (shortcut, event, data) { | |
|
205 | var that = this; | |
|
206 | var c = this._counts; | |
|
207 | var t = this._timers; | |
|
208 | var timer = null; | |
|
209 | if (c[shortcut] === data.count-1) { | |
|
210 | c[shortcut] = 0; | |
|
211 | var timer = t[shortcut]; | |
|
212 | if (timer) {clearTimeout(timer); delete t[shortcut];} | |
|
213 | return data.handler(event); | |
|
214 | } else { | |
|
215 | c[shortcut] = c[shortcut] + 1; | |
|
216 | timer = setTimeout(function () { | |
|
217 | c[shortcut] = 0; | |
|
218 | }, that.delay); | |
|
219 | t[shortcut] = timer; | |
|
220 | } | |
|
221 | return false; | |
|
222 | } | |
|
223 | ||
|
224 | ShortcutManager.prototype.call_handler = function (event) { | |
|
225 | var shortcut = event_to_shortcut(event); | |
|
226 | var data = this._shortcuts[shortcut]; | |
|
227 | if (data) { | |
|
228 | var handler = data['handler']; | |
|
229 | if (handler) { | |
|
230 | if (data.count === 1) { | |
|
231 | return handler(event); | |
|
232 | } else if (data.count > 1) { | |
|
233 | return this.count_handler(shortcut, event, data); | |
|
234 | } | |
|
235 | } | |
|
236 | } | |
|
237 | return true; | |
|
238 | } | |
|
239 | ||
|
240 | return { | |
|
241 | keycodes : keycodes, | |
|
242 | inv_keycodes : inv_keycodes, | |
|
243 | ShortcutManager : ShortcutManager, | |
|
244 | normalize_key : normalize_key, | |
|
245 | normalize_shortcut : normalize_shortcut, | |
|
246 | shortcut_to_event : shortcut_to_event, | |
|
247 | event_to_shortcut : event_to_shortcut, | |
|
248 | trigger_keydown : trigger_keydown | |
|
249 | } | |
|
250 | ||
|
251 | }(IPython)); |
@@ -0,0 +1,49 b'' | |||
|
1 | ||
|
2 | ||
|
3 | var normalized_shortcuts = [ | |
|
4 | 'ctrl+shift+m', | |
|
5 | 'alt+meta+p', | |
|
6 | ]; | |
|
7 | ||
|
8 | var to_normalize = [ | |
|
9 | ['shift+%', 'shift+5'], | |
|
10 | ['ShiFT+MeTa+CtRl+AlT+m', 'alt+ctrl+meta+shift+m'], | |
|
11 | ]; | |
|
12 | ||
|
13 | var unshifted = "` 1 2 3 4 5 6 7 8 9 0 - = q w e r t y u i o p [ ] \\ a s d f g h j k l ; ' z x c v b n m , . /"; | |
|
14 | // shifted = '~ ! @ # $ % ^ & * ( ) _ + Q W E R T Y U I O P { } | A S D F G H J K L : " Z X C V B N M < > ?'; | |
|
15 | ||
|
16 | casper.notebook_test(function () { | |
|
17 | ||
|
18 | this.then(function () { | |
|
19 | this.each(unshifted.split(' '), function (self, item) { | |
|
20 | var result = this.evaluate(function (sc) { | |
|
21 | var e = IPython.keyboard.shortcut_to_event(sc); | |
|
22 | var sc2 = IPython.keyboard.event_to_shortcut(e); | |
|
23 | return sc2; | |
|
24 | }, item); | |
|
25 | this.test.assertEquals(result, item, 'Shortcut to event roundtrip: '+item); | |
|
26 | }); | |
|
27 | }); | |
|
28 | ||
|
29 | this.then(function () { | |
|
30 | this.each(to_normalize, function (self, item) { | |
|
31 | var result = this.evaluate(function (pair) { | |
|
32 | return IPython.keyboard.normalize_shortcut(pair[0]); | |
|
33 | }, item); | |
|
34 | this.test.assertEquals(result, item[1], 'Normalize shortcut: '+item[0]); | |
|
35 | }); | |
|
36 | }) | |
|
37 | ||
|
38 | this.then(function () { | |
|
39 | this.each(normalized_shortcuts, function (self, item) { | |
|
40 | var result = this.evaluate(function (sc) { | |
|
41 | var e = IPython.keyboard.shortcut_to_event(sc); | |
|
42 | var sc2 = IPython.keyboard.event_to_shortcut(e); | |
|
43 | return sc2; | |
|
44 | }, item); | |
|
45 | this.test.assertEquals(result, item, 'Shortcut to event roundtrip: '+item); | |
|
46 | }); | |
|
47 | }); | |
|
48 | ||
|
49 | }); No newline at end of file |
@@ -350,66 +350,6 b' IPython.utils = (function (IPython) {' | |||
|
350 | 350 | "$1<a target=\"_blank\" href=\"$2$3\">$2$3</a>"); |
|
351 | 351 | } |
|
352 | 352 | |
|
353 | // some keycodes that seem to be platform/browser independent | |
|
354 | var keycodes = { | |
|
355 | BACKSPACE: 8, | |
|
356 | TAB : 9, | |
|
357 | ENTER : 13, | |
|
358 | SHIFT : 16, | |
|
359 | CTRL : 17, | |
|
360 | CONTROL : 17, | |
|
361 | ALT : 18, | |
|
362 | CAPS_LOCK: 20, | |
|
363 | ESC : 27, | |
|
364 | SPACE : 32, | |
|
365 | PGUP : 33, | |
|
366 | PGDOWN : 34, | |
|
367 | END : 35, | |
|
368 | HOME : 36, | |
|
369 | LEFT_ARROW: 37, | |
|
370 | LEFTARROW: 37, | |
|
371 | LEFT : 37, | |
|
372 | UP_ARROW : 38, | |
|
373 | UPARROW : 38, | |
|
374 | UP : 38, | |
|
375 | RIGHT_ARROW:39, | |
|
376 | RIGHTARROW:39, | |
|
377 | RIGHT : 39, | |
|
378 | DOWN_ARROW: 40, | |
|
379 | DOWNARROW: 40, | |
|
380 | DOWN : 40, | |
|
381 | I : 73, | |
|
382 | M : 77, | |
|
383 | // all three of these keys may be COMMAND on OS X: | |
|
384 | LEFT_SUPER : 91, | |
|
385 | RIGHT_SUPER : 92, | |
|
386 | COMMAND : 93, | |
|
387 | }; | |
|
388 | ||
|
389 | // trigger a key press event | |
|
390 | var press = function (key) { | |
|
391 | var key_press = $.Event('keydown', {which: key}); | |
|
392 | $(document).trigger(key_press); | |
|
393 | } | |
|
394 | ||
|
395 | var press_up = function() { press(keycodes.UP); }; | |
|
396 | var press_down = function() { press(keycodes.DOWN); }; | |
|
397 | ||
|
398 | var press_ctrl_enter = function() { | |
|
399 | $(document).trigger($.Event('keydown', {which: keycodes.ENTER, ctrlKey: true})); | |
|
400 | }; | |
|
401 | ||
|
402 | var press_shift_enter = function() { | |
|
403 | $(document).trigger($.Event('keydown', {which: keycodes.ENTER, shiftKey: true})); | |
|
404 | }; | |
|
405 | ||
|
406 | // trigger the ctrl-m shortcut followed by one of our keys | |
|
407 | var press_ghetto = function(key) { | |
|
408 | $(document).trigger($.Event('keydown', {which: keycodes.M, ctrlKey: true})); | |
|
409 | press(key); | |
|
410 | }; | |
|
411 | ||
|
412 | ||
|
413 | 353 | var points_to_pixels = function (points) { |
|
414 | 354 | // A reasonably good way of converting between points and pixels. |
|
415 | 355 | var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>'); |
@@ -431,7 +371,6 b' IPython.utils = (function (IPython) {' | |||
|
431 | 371 | }; |
|
432 | 372 | }; |
|
433 | 373 | |
|
434 | ||
|
435 | 374 | var url_path_join = function () { |
|
436 | 375 | // join a sequence of url components with '/' |
|
437 | 376 | var url = ''; |
@@ -554,13 +493,6 b' IPython.utils = (function (IPython) {' | |||
|
554 | 493 | regex_split : regex_split, |
|
555 | 494 | uuid : uuid, |
|
556 | 495 | fixConsole : fixConsole, |
|
557 | keycodes : keycodes, | |
|
558 | press : press, | |
|
559 | press_up : press_up, | |
|
560 | press_down : press_down, | |
|
561 | press_ctrl_enter : press_ctrl_enter, | |
|
562 | press_shift_enter : press_shift_enter, | |
|
563 | press_ghetto : press_ghetto, | |
|
564 | 496 | fixCarriageReturn : fixCarriageReturn, |
|
565 | 497 | autoLinkUrls : autoLinkUrls, |
|
566 | 498 | points_to_pixels : points_to_pixels, |
@@ -14,7 +14,6 b'' | |||
|
14 | 14 | |
|
15 | 15 | var CellToolbar = IPython.CellToolbar; |
|
16 | 16 | var raw_cell_preset = []; |
|
17 | var utils = IPython.utils; | |
|
18 | 17 | |
|
19 | 18 | var select_type = CellToolbar.utils.select_ui_generator([ |
|
20 | 19 | ["None", "-"], |
@@ -58,7 +57,7 b'' | |||
|
58 | 57 | var that = $(this); |
|
59 | 58 | // Upon ENTER, click the OK button. |
|
60 | 59 | that.find('input[type="text"]').keydown(function (event, ui) { |
|
61 |
if (event.which === |
|
|
60 | if (event.which === IPython.keyboard.keycodes.enter) { | |
|
62 | 61 | that.find('.btn-primary').first().click(); |
|
63 | 62 | return false; |
|
64 | 63 | } |
@@ -45,7 +45,7 b' var IPython = (function (IPython) {' | |||
|
45 | 45 | "use strict"; |
|
46 | 46 | |
|
47 | 47 | var utils = IPython.utils; |
|
48 |
var key |
|
|
48 | var keycodes = IPython.keyboard.keycodes; | |
|
49 | 49 | |
|
50 | 50 | /** |
|
51 | 51 | * A Cell conceived to write code. |
@@ -195,16 +195,16 b' var IPython = (function (IPython) {' | |||
|
195 | 195 | // whatever key is pressed, first, cancel the tooltip request before |
|
196 | 196 | // they are sent, and remove tooltip if any, except for tab again |
|
197 | 197 | var tooltip_closed = null; |
|
198 |
if (event.type === 'keydown' && event.which != key. |
|
|
198 | if (event.type === 'keydown' && event.which != keycodes.tab ) { | |
|
199 | 199 | tooltip_closed = IPython.tooltip.remove_and_cancel_tooltip(); |
|
200 | 200 | } |
|
201 | 201 | |
|
202 | 202 | var cur = editor.getCursor(); |
|
203 |
if (event.keyCode === key. |
|
|
203 | if (event.keyCode === keycodes.enter){ | |
|
204 | 204 | this.auto_highlight(); |
|
205 | 205 | } |
|
206 | 206 | |
|
207 |
if (event.keyCode === key. |
|
|
207 | if (event.keyCode === keycodes.enter && (event.shiftKey || event.ctrlKey || event.altKey)) { | |
|
208 | 208 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
209 | 209 | return true; |
|
210 | 210 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { |
@@ -214,7 +214,7 b' var IPython = (function (IPython) {' | |||
|
214 | 214 | // The second argument says to hide the tooltip if the docstring |
|
215 | 215 | // is actually empty |
|
216 | 216 | IPython.tooltip.pending(that, true); |
|
217 |
} else if (event.which === key. |
|
|
217 | } else if (event.which === keycodes.up && event.type === 'keydown') { | |
|
218 | 218 | // If we are not at the top, let CM handle the up arrow and |
|
219 | 219 | // prevent the global keydown handler from handling it. |
|
220 | 220 | if (!that.at_top()) { |
@@ -223,7 +223,7 b' var IPython = (function (IPython) {' | |||
|
223 | 223 | } else { |
|
224 | 224 | return true; |
|
225 | 225 | } |
|
226 |
} else if (event.which === key. |
|
|
226 | } else if (event.which === keycodes.esc && event.type === 'keydown') { | |
|
227 | 227 | // First see if the tooltip is active and if so cancel it. |
|
228 | 228 | if (tooltip_closed) { |
|
229 | 229 | // The call to remove_and_cancel_tooltip above in L177 doesn't pass |
@@ -249,7 +249,7 b' var IPython = (function (IPython) {' | |||
|
249 | 249 | // Don't let CM handle the event, defer to global handling. |
|
250 | 250 | return true; |
|
251 | 251 | } |
|
252 |
} else if (event.which === key. |
|
|
252 | } else if (event.which === keycodes.down && event.type === 'keydown') { | |
|
253 | 253 | // If we are not at the bottom, let CM handle the down arrow and |
|
254 | 254 | // prevent the global keydown handler from handling it. |
|
255 | 255 | if (!that.at_bottom()) { |
@@ -258,7 +258,7 b' var IPython = (function (IPython) {' | |||
|
258 | 258 | } else { |
|
259 | 259 | return true; |
|
260 | 260 | } |
|
261 |
} else if (event.keyCode === key. |
|
|
261 | } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) { | |
|
262 | 262 | if (editor.somethingSelected()){ |
|
263 | 263 | var anchor = editor.getCursor("anchor"); |
|
264 | 264 | var head = editor.getCursor("head"); |
@@ -269,7 +269,7 b' var IPython = (function (IPython) {' | |||
|
269 | 269 | IPython.tooltip.request(that); |
|
270 | 270 | event.stop(); |
|
271 | 271 | return true; |
|
272 |
} else if (event.keyCode === key. |
|
|
272 | } else if (event.keyCode === keycodes.tab && event.type == 'keydown') { | |
|
273 | 273 | // Tab completion. |
|
274 | 274 | IPython.tooltip.remove_and_cancel_tooltip(); |
|
275 | 275 | if (editor.somethingSelected()) { |
@@ -6,7 +6,7 b' var IPython = (function (IPython) {' | |||
|
6 | 6 | "use strict"; |
|
7 | 7 | |
|
8 | 8 | // easier key mapping |
|
9 |
var key = IPython. |
|
|
9 | var keycodes = IPython.keyboard.keycodes; | |
|
10 | 10 | |
|
11 | 11 | function prepend_n_prc(str, n) { |
|
12 | 12 | for( var i =0 ; i< n ; i++){ |
@@ -307,29 +307,22 b' var IPython = (function (IPython) {' | |||
|
307 | 307 | Completer.prototype.keydown = function (event) { |
|
308 | 308 | var code = event.keyCode; |
|
309 | 309 | var that = this; |
|
310 | var special_key = false; | |
|
311 | ||
|
312 | // detect special keys like SHIFT,PGUP,... | |
|
313 | for( var _key in key ) { | |
|
314 | if (code == key[_key] ) { | |
|
315 | special_key = true; | |
|
316 | } | |
|
317 | } | |
|
318 | 310 | |
|
319 | 311 | // Enter |
|
320 |
if (code == key. |
|
|
312 | if (code == keycodes.enter) { | |
|
321 | 313 | CodeMirror.e_stop(event); |
|
322 | 314 | this.pick(); |
|
323 | 315 | } |
|
324 | 316 | // Escape or backspace |
|
325 |
else if (code == key. |
|
|
317 | else if (code == keycodes.esc) { | |
|
326 | 318 | CodeMirror.e_stop(event); |
|
327 | 319 | this.close(); |
|
328 | 320 | this.editor.focus(); |
|
329 | } else if (code == key.BACKSPACE) { | |
|
321 | ||
|
322 | } else if (code == keycodes.backspace) { | |
|
330 | 323 | this.close(); |
|
331 | 324 | this.editor.focus(); |
|
332 |
} else if (code == key. |
|
|
325 | } else if (code == keycodes.tab) { | |
|
333 | 326 | //all the fastforwarding operation, |
|
334 | 327 | //Check that shared start is not null which can append with prefixed completion |
|
335 | 328 | // like %pylab , pylab have no shred start, and ff will result in py<tab><tab> |
@@ -345,7 +338,7 b' var IPython = (function (IPython) {' | |||
|
345 | 338 | setTimeout(function () { |
|
346 | 339 | that.carry_on_completion(); |
|
347 | 340 | }, 50); |
|
348 |
} else if (code == key. |
|
|
341 | } else if (code == keycodes.up || code == keycodes.down) { | |
|
349 | 342 | // need to do that to be able to move the arrow |
|
350 | 343 | // when on the first or last line ofo a code cell |
|
351 | 344 | event.stopPropagation(); |
@@ -365,8 +358,8 b' var IPython = (function (IPython) {' | |||
|
365 | 358 | // don't handle keypress if it's not a character (arrows on FF) |
|
366 | 359 | // or ENTER/TAB |
|
367 | 360 | if (event.charCode === 0 || |
|
368 |
code == key. |
|
|
369 |
code == key. |
|
|
361 | code == keycodes.enter || | |
|
362 | code == keycodes.tab | |
|
370 | 363 | ) return; |
|
371 | 364 | |
|
372 | 365 | var cur = this.editor.getCursor(); |
@@ -12,69 +12,8 b'' | |||
|
12 | 12 | var IPython = (function (IPython) { |
|
13 | 13 | "use strict"; |
|
14 | 14 | |
|
15 | // Setup global keycodes and inverse keycodes. | |
|
16 | ||
|
17 | // See http://unixpapa.com/js/key.html for a complete description. The short of | |
|
18 | // it is that there are different keycode sets. Firefox uses the "Mozilla keycodes" | |
|
19 | // and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same | |
|
20 | // but have minor differences. | |
|
21 | ||
|
22 | // These apply to Firefox, (Webkit and IE) | |
|
23 | var _keycodes = { | |
|
24 | 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73, | |
|
25 | 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82, | |
|
26 | 's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90, | |
|
27 | '1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54, | |
|
28 | '7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48, | |
|
29 | '[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191, | |
|
30 | '\\ |': 220, '\' "': 222, | |
|
31 | 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100, | |
|
32 | 'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105, | |
|
33 | 'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111, | |
|
34 | 'f1': 112, 'f2': 113, 'f3': 114, 'f4': 115, 'f5': 116, 'f6': 117, 'f7': 118, | |
|
35 | 'f8': 119, 'f9': 120, 'f11': 122, 'f12': 123, 'f13': 124, 'f14': 125, 'f15': 126, | |
|
36 | 'backspace': 8, 'tab': 9, 'enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18, | |
|
37 | 'meta': 91, 'capslock': 20, 'esc': 27, 'space': 32, 'pageup': 33, 'pagedown': 34, | |
|
38 | 'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40, | |
|
39 | 'insert': 45, 'delete': 46, 'numlock': 144, | |
|
40 | }; | |
|
41 | ||
|
42 | // These apply to Firefox and Opera | |
|
43 | var _mozilla_keycodes = { | |
|
44 | '; :': 59, '= +': 61, '- _': 173, 'meta': 224 | |
|
45 | }; | |
|
46 | ||
|
47 | // This apply to Webkit and IE | |
|
48 | var _ie_keycodes = { | |
|
49 | '; :': 186, '= +': 187, '- _': 189, | |
|
50 | }; | |
|
51 | ||
|
52 | 15 | var browser = IPython.utils.browser[0]; |
|
53 | 16 | var platform = IPython.utils.platform; |
|
54 | ||
|
55 | if (browser === 'Firefox' || browser === 'Opera') { | |
|
56 | $.extend(_keycodes, _mozilla_keycodes); | |
|
57 | } else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') { | |
|
58 | $.extend(_keycodes, _ie_keycodes); | |
|
59 | } | |
|
60 | ||
|
61 | var keycodes = {}; | |
|
62 | var inv_keycodes = {}; | |
|
63 | for (var name in _keycodes) { | |
|
64 | var names = name.split(' '); | |
|
65 | if (names.length === 1) { | |
|
66 | var n = names[0]; | |
|
67 | keycodes[n] = _keycodes[n]; | |
|
68 | inv_keycodes[_keycodes[n]] = n; | |
|
69 | } else { | |
|
70 | var primary = names[0]; | |
|
71 | var secondary = names[1]; | |
|
72 | keycodes[primary] = _keycodes[name]; | |
|
73 | keycodes[secondary] = _keycodes[name]; | |
|
74 | inv_keycodes[_keycodes[name]] = primary; | |
|
75 | } | |
|
76 | } | |
|
77 | ||
|
78 | 17 | |
|
79 | 18 | // Default keyboard shortcuts |
|
80 | 19 | |
@@ -545,141 +484,11 b' var IPython = (function (IPython) {' | |||
|
545 | 484 | }; |
|
546 | 485 | |
|
547 | 486 | |
|
548 | // Shortcut manager class | |
|
549 | ||
|
550 | var ShortcutManager = function (delay) { | |
|
551 | this._shortcuts = {}; | |
|
552 | this._counts = {}; | |
|
553 | this._timers = {}; | |
|
554 | this.delay = delay || 800; // delay in milliseconds | |
|
555 | }; | |
|
556 | ||
|
557 | ShortcutManager.prototype.help = function () { | |
|
558 | var help = []; | |
|
559 | for (var shortcut in this._shortcuts) { | |
|
560 | var help_string = this._shortcuts[shortcut].help; | |
|
561 | var help_index = this._shortcuts[shortcut].help_index; | |
|
562 | if (help_string) { | |
|
563 | if (platform === 'MacOS') { | |
|
564 | shortcut = shortcut.replace('meta', 'cmd'); | |
|
565 | } | |
|
566 | help.push({ | |
|
567 | shortcut: shortcut, | |
|
568 | help: help_string, | |
|
569 | help_index: help_index} | |
|
570 | ); | |
|
571 | } | |
|
572 | } | |
|
573 | help.sort(function (a, b) { | |
|
574 | if (a.help_index > b.help_index) | |
|
575 | return 1; | |
|
576 | if (a.help_index < b.help_index) | |
|
577 | return -1; | |
|
578 | return 0; | |
|
579 | }); | |
|
580 | return help; | |
|
581 | }; | |
|
582 | ||
|
583 | ShortcutManager.prototype.normalize_key = function (key) { | |
|
584 | return inv_keycodes[keycodes[key]]; | |
|
585 | }; | |
|
586 | ||
|
587 | ShortcutManager.prototype.normalize_shortcut = function (shortcut) { | |
|
588 | // Sort a sequence of + separated modifiers into the order alt+ctrl+meta+shift | |
|
589 | shortcut = shortcut.replace('cmd', 'meta').toLowerCase(); | |
|
590 | var values = shortcut.split("+"); | |
|
591 | if (values.length === 1) { | |
|
592 | return this.normalize_key(values[0]); | |
|
593 | } else { | |
|
594 | var modifiers = values.slice(0,-1); | |
|
595 | var key = this.normalize_key(values[values.length-1]); | |
|
596 | modifiers.sort(); | |
|
597 | return modifiers.join('+') + '+' + key; | |
|
598 | } | |
|
599 | }; | |
|
600 | ||
|
601 | ShortcutManager.prototype.event_to_shortcut = function (event) { | |
|
602 | // Convert a jQuery keyboard event to a strong based keyboard shortcut | |
|
603 | var shortcut = ''; | |
|
604 | var key = inv_keycodes[event.which]; | |
|
605 | if (event.altKey && key !== 'alt') {shortcut += 'alt+';} | |
|
606 | if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl+';} | |
|
607 | if (event.metaKey && key !== 'meta') {shortcut += 'meta+';} | |
|
608 | if (event.shiftKey && key !== 'shift') {shortcut += 'shift+';} | |
|
609 | shortcut += key; | |
|
610 | return shortcut; | |
|
611 | }; | |
|
612 | ||
|
613 | ShortcutManager.prototype.clear_shortcuts = function () { | |
|
614 | this._shortcuts = {}; | |
|
615 | }; | |
|
616 | ||
|
617 | ShortcutManager.prototype.add_shortcut = function (shortcut, data) { | |
|
618 | if (typeof(data) === 'function') { | |
|
619 | data = {help: '', help_index: '', handler: data}; | |
|
620 | } | |
|
621 | data.help_index = data.help_index || ''; | |
|
622 | data.help = data.help || ''; | |
|
623 | data.count = data.count || 1; | |
|
624 | if (data.help_index === '') { | |
|
625 | data.help_index = 'zz'; | |
|
626 | } | |
|
627 | shortcut = this.normalize_shortcut(shortcut); | |
|
628 | this._counts[shortcut] = 0; | |
|
629 | this._shortcuts[shortcut] = data; | |
|
630 | }; | |
|
631 | ||
|
632 | ShortcutManager.prototype.add_shortcuts = function (data) { | |
|
633 | for (var shortcut in data) { | |
|
634 | this.add_shortcut(shortcut, data[shortcut]); | |
|
635 | } | |
|
636 | }; | |
|
637 | ||
|
638 | ShortcutManager.prototype.remove_shortcut = function (shortcut) { | |
|
639 | shortcut = this.normalize_shortcut(shortcut); | |
|
640 | delete this._counts[shortcut]; | |
|
641 | delete this._shortcuts[shortcut]; | |
|
642 | }; | |
|
643 | ||
|
644 | ShortcutManager.prototype.count_handler = function (shortcut, event, data) { | |
|
645 | var that = this; | |
|
646 | var c = this._counts; | |
|
647 | var t = this._timers; | |
|
648 | var timer = null; | |
|
649 | if (c[shortcut] === data.count-1) { | |
|
650 | c[shortcut] = 0; | |
|
651 | timer = t[shortcut]; | |
|
652 | if (timer) {clearTimeout(timer); delete t[shortcut];} | |
|
653 | return data.handler(event); | |
|
654 | } else { | |
|
655 | c[shortcut] = c[shortcut] + 1; | |
|
656 | timer = setTimeout(function () { | |
|
657 | c[shortcut] = 0; | |
|
658 | }, that.delay); | |
|
659 | t[shortcut] = timer; | |
|
660 | } | |
|
661 | return false; | |
|
662 | }; | |
|
663 | ||
|
664 | ShortcutManager.prototype.call_handler = function (event) { | |
|
665 | var shortcut = this.event_to_shortcut(event); | |
|
666 | var data = this._shortcuts[shortcut]; | |
|
667 | if (data) { | |
|
668 | var handler = data.handler; | |
|
669 | if (handler) { | |
|
670 | if (data.count === 1) { | |
|
671 | return handler(event); | |
|
672 | } else if (data.count > 1) { | |
|
673 | return this.count_handler(shortcut, event, data); | |
|
674 | } | |
|
675 | } | |
|
676 | } | |
|
677 | return true; | |
|
678 | }; | |
|
487 | // Main keyboard manager for the notebook | |
|
679 | 488 | |
|
489 | var ShortcutManager = IPython.keyboard.ShortcutManager; | |
|
490 | var keycodes = IPython.keyboard.keycodes; | |
|
680 | 491 | |
|
681 | // Main keyboard manager for the notebook | |
|
682 | ||
|
683 | 492 | var KeyboardManager = function () { |
|
684 | 493 | this.mode = 'command'; |
|
685 | 494 | this.enabled = true; |
@@ -779,12 +588,9 b' var IPython = (function (IPython) {' | |||
|
779 | 588 | }; |
|
780 | 589 | |
|
781 | 590 | |
|
782 | IPython.keycodes = keycodes; | |
|
783 | IPython.inv_keycodes = inv_keycodes; | |
|
784 | 591 | IPython.default_common_shortcuts = default_common_shortcuts; |
|
785 | 592 | IPython.default_edit_shortcuts = default_edit_shortcuts; |
|
786 | 593 | IPython.default_command_shortcuts = default_command_shortcuts; |
|
787 | IPython.ShortcutManager = ShortcutManager; | |
|
788 | 594 | IPython.KeyboardManager = KeyboardManager; |
|
789 | 595 | |
|
790 | 596 | return IPython; |
@@ -1917,7 +1917,7 b' var IPython = (function (IPython) {' | |||
|
1917 | 1917 | var that = $(this); |
|
1918 | 1918 | // Upon ENTER, click the OK button. |
|
1919 | 1919 | that.find('input[type="text"]').keydown(function (event, ui) { |
|
1920 |
if (event.which === |
|
|
1920 | if (event.which === IPython.keyboard.keycodes.enter) { | |
|
1921 | 1921 | that.find('.btn-primary').first().click(); |
|
1922 | 1922 | } |
|
1923 | 1923 | }); |
@@ -669,7 +669,7 b' var IPython = (function (IPython) {' | |||
|
669 | 669 | .keydown(function (event, ui) { |
|
670 | 670 | // make sure we submit on enter, |
|
671 | 671 | // and don't re-execute the *cell* on shift-enter |
|
672 |
if (event.which === |
|
|
672 | if (event.which === IPython.keyboard.keycodes.enter) { | |
|
673 | 673 | that._submit_raw_input(); |
|
674 | 674 | return false; |
|
675 | 675 | } |
@@ -103,7 +103,7 b' var IPython = (function (IPython) {' | |||
|
103 | 103 | var that = $(this); |
|
104 | 104 | // Upon ENTER, click the OK button. |
|
105 | 105 | that.find('input[type="text"]').keydown(function (event, ui) { |
|
106 |
if (event.which === |
|
|
106 | if (event.which === IPython.keyboard.keycodes.enter) { | |
|
107 | 107 | that.find('.btn-primary').first().click(); |
|
108 | 108 | return false; |
|
109 | 109 | } |
@@ -20,7 +20,7 b' var IPython = (function (IPython) {' | |||
|
20 | 20 | "use strict"; |
|
21 | 21 | |
|
22 | 22 | // TextCell base class |
|
23 |
var key = IPython. |
|
|
23 | var keycodes = IPython.keyboard.keycodes; | |
|
24 | 24 | |
|
25 | 25 | /** |
|
26 | 26 | * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text' |
@@ -140,7 +140,7 b' var IPython = (function (IPython) {' | |||
|
140 | 140 | if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey || event.altKey)) { |
|
141 | 141 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
142 | 142 | return true; |
|
143 |
} else if (event.which === key. |
|
|
143 | } else if (event.which === keycodes.up && event.type === 'keydown') { | |
|
144 | 144 | // If we are not at the top, let CM handle the up arrow and |
|
145 | 145 | // prevent the global keydown handler from handling it. |
|
146 | 146 | if (!that.at_top()) { |
@@ -148,8 +148,8 b' var IPython = (function (IPython) {' | |||
|
148 | 148 | return false; |
|
149 | 149 | } else { |
|
150 | 150 | return true; |
|
151 | } | |
|
152 |
} else if (event.which === key. |
|
|
151 | }; | |
|
152 | } else if (event.which === keycodes.down && event.type === 'keydown') { | |
|
153 | 153 | // If we are not at the bottom, let CM handle the down arrow and |
|
154 | 154 | // prevent the global keydown handler from handling it. |
|
155 | 155 | if (!that.at_bottom()) { |
@@ -157,8 +157,8 b' var IPython = (function (IPython) {' | |||
|
157 | 157 | return false; |
|
158 | 158 | } else { |
|
159 | 159 | return true; |
|
160 | } | |
|
161 |
} else if (event.which === key. |
|
|
160 | }; | |
|
161 | } else if (event.which === keycodes.esc && event.type === 'keydown') { | |
|
162 | 162 | if (that.code_mirror.options.keyMap === "vim-insert") { |
|
163 | 163 | // vim keyMap is active and in insert mode. In this case we leave vim |
|
164 | 164 | // insert mode, but remain in notebook edit mode. |
@@ -317,6 +317,7 b' class="notebook_app"' | |||
|
317 | 317 | |
|
318 | 318 | <script src="{{ static_url("base/js/events.js") }}" type="text/javascript" charset="utf-8"></script> |
|
319 | 319 | <script src="{{ static_url("base/js/utils.js") }}" type="text/javascript" charset="utf-8"></script> |
|
320 | <script src="{{ static_url("base/js/keyboard.js") }}" type="text/javascript" charset="utf-8"></script> | |
|
320 | 321 | <script src="{{ static_url("base/js/dialog.js") }}" type="text/javascript" charset="utf-8"></script> |
|
321 | 322 | <script src="{{ static_url("services/kernels/js/kernel.js") }}" type="text/javascript" charset="utf-8"></script> |
|
322 | 323 | <script src="{{ static_url("services/kernels/js/comm.js") }}" type="text/javascript" charset="utf-8"></script> |
@@ -5,14 +5,14 b' casper.notebook_test(function () {' | |||
|
5 | 5 | var result = this.evaluate(function() { |
|
6 | 6 | IPython.notebook.command_mode(); |
|
7 | 7 | pos0 = IPython.notebook.get_selected_index(); |
|
8 | IPython.utils.press(IPython.keycodes.b) | |
|
8 | IPython.keyboard.trigger_keydown('b'); | |
|
9 | 9 | pos1 = IPython.notebook.get_selected_index(); |
|
10 | IPython.utils.press(IPython.keycodes.b) | |
|
10 | IPython.keyboard.trigger_keydown('b'); | |
|
11 | 11 | pos2 = IPython.notebook.get_selected_index(); |
|
12 | 12 | // Simulate the "up arrow" and "down arrow" keys. |
|
13 |
IPython. |
|
|
13 | IPython.keyboard.trigger_keydown('up'); | |
|
14 | 14 | pos3 = IPython.notebook.get_selected_index(); |
|
15 |
IPython. |
|
|
15 | IPython.keyboard.trigger_keydown('down'); | |
|
16 | 16 | pos4 = IPython.notebook.get_selected_index(); |
|
17 | 17 | return pos0 == 0 && |
|
18 | 18 | pos1 == 1 && |
@@ -13,8 +13,8 b' casper.notebook_test(function () {' | |||
|
13 | 13 | |
|
14 | 14 | // Simulate the "up arrow" and "down arrow" keys. |
|
15 | 15 | // |
|
16 |
IPython. |
|
|
17 |
IPython. |
|
|
16 | IPython.keyboard.trigger_keydown('up'); | |
|
17 | IPython.keyboard.trigger_keydown('down'); | |
|
18 | 18 | return true; |
|
19 | 19 | }); |
|
20 | 20 | this.test.assertTrue(result, 'Up/down arrow okay in empty notebook.'); |
@@ -22,7 +22,7 b' casper.notebook_test(function () {' | |||
|
22 | 22 | var cell = IPython.notebook.get_cell(0); |
|
23 | 23 | cell.set_text('a=11; print(a)'); |
|
24 | 24 | cell.clear_output(); |
|
25 |
IPython. |
|
|
25 | IPython.keyboard.trigger_keydown('shift+enter'); | |
|
26 | 26 | }); |
|
27 | 27 | |
|
28 | 28 | this.wait_for_output(0); |
@@ -41,7 +41,7 b' casper.notebook_test(function () {' | |||
|
41 | 41 | var cell = IPython.notebook.get_cell(0); |
|
42 | 42 | cell.set_text('a=12; print(a)'); |
|
43 | 43 | cell.clear_output(); |
|
44 |
IPython. |
|
|
44 | IPython.keyboard.trigger_keydown('ctrl+enter'); | |
|
45 | 45 | }); |
|
46 | 46 | |
|
47 | 47 | this.wait_for_output(0); |
@@ -32,7 +32,7 b' casper.notebook_test(function () {' | |||
|
32 | 32 | |
|
33 | 33 | // interrupt using Ctrl-M I keyboard shortcut |
|
34 | 34 | this.thenEvaluate( function() { |
|
35 | IPython.utils.press_ghetto(IPython.utils.keycodes.I) | |
|
35 | IPython.keyboard.trigger_keydown('i'); | |
|
36 | 36 | }); |
|
37 | 37 | |
|
38 | 38 | this.wait_for_output(0); |
@@ -9,7 +9,7 b' casper.notebook_test(function() {' | |||
|
9 | 9 | var cell_one = IPython.notebook.get_selected_cell(); |
|
10 | 10 | cell_one.set_text('a = 5'); |
|
11 | 11 | |
|
12 | IPython.utils.press(IPython.keycodes.b) | |
|
12 | IPython.keyboard.trigger_keydown('b'); | |
|
13 | 13 | var cell_two = IPython.notebook.get_selected_cell(); |
|
14 | 14 | cell_two.set_text('print(a)'); |
|
15 | 15 | }; |
General Comments 0
You need to be logged in to leave comments.
Login now