##// END OF EJS Templates
update notebook quickhelp on new shortcuts
Paul Ivanov -
Show More
@@ -1,251 +1,257
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2011 The IPython Development Team
2 // Copyright (C) 2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // Keyboard management
9 // Keyboard management
10 //============================================================================
10 //============================================================================
11
11
12 IPython.namespace('IPython.keyboard');
12 IPython.namespace('IPython.keyboard');
13
13
14 IPython.keyboard = (function (IPython) {
14 IPython.keyboard = (function (IPython) {
15 "use strict";
15 "use strict";
16
16
17 // Setup global keycodes and inverse keycodes.
17 // Setup global keycodes and inverse keycodes.
18
18
19 // See http://unixpapa.com/js/key.html for a complete description. The short of
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"
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
21 // and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same
22 // but have minor differences.
22 // but have minor differences.
23
23
24 // These apply to Firefox, (Webkit and IE)
24 // These apply to Firefox, (Webkit and IE)
25 var _keycodes = {
25 var _keycodes = {
26 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73,
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,
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,
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,
29 '1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54,
30 '7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48,
30 '7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48,
31 '[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191,
31 '[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191,
32 '\\ |': 220, '\' "': 222,
32 '\\ |': 220, '\' "': 222,
33 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100,
33 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100,
34 'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105,
34 'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105,
35 'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111,
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,
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,
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,
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,
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,
40 'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40,
41 'insert': 45, 'delete': 46, 'numlock': 144,
41 'insert': 45, 'delete': 46, 'numlock': 144,
42 };
42 };
43
43
44 // These apply to Firefox and Opera
44 // These apply to Firefox and Opera
45 var _mozilla_keycodes = {
45 var _mozilla_keycodes = {
46 '; :': 59, '= +': 61, '- _': 173, 'meta': 224
46 '; :': 59, '= +': 61, '- _': 173, 'meta': 224
47 }
47 }
48
48
49 // This apply to Webkit and IE
49 // This apply to Webkit and IE
50 var _ie_keycodes = {
50 var _ie_keycodes = {
51 '; :': 186, '= +': 187, '- _': 189,
51 '; :': 186, '= +': 187, '- _': 189,
52 }
52 }
53
53
54 var browser = IPython.utils.browser[0];
54 var browser = IPython.utils.browser[0];
55 var platform = IPython.utils.platform;
55 var platform = IPython.utils.platform;
56
56
57 if (browser === 'Firefox' || browser === 'Opera') {
57 if (browser === 'Firefox' || browser === 'Opera') {
58 $.extend(_keycodes, _mozilla_keycodes);
58 $.extend(_keycodes, _mozilla_keycodes);
59 } else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') {
59 } else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') {
60 $.extend(_keycodes, _ie_keycodes);
60 $.extend(_keycodes, _ie_keycodes);
61 }
61 }
62
62
63 var keycodes = {};
63 var keycodes = {};
64 var inv_keycodes = {};
64 var inv_keycodes = {};
65 for (var name in _keycodes) {
65 for (var name in _keycodes) {
66 var names = name.split(' ');
66 var names = name.split(' ');
67 if (names.length === 1) {
67 if (names.length === 1) {
68 var n = names[0]
68 var n = names[0]
69 keycodes[n] = _keycodes[n]
69 keycodes[n] = _keycodes[n]
70 inv_keycodes[_keycodes[n]] = n
70 inv_keycodes[_keycodes[n]] = n
71 } else {
71 } else {
72 var primary = names[0];
72 var primary = names[0];
73 var secondary = names[1];
73 var secondary = names[1];
74 keycodes[primary] = _keycodes[name]
74 keycodes[primary] = _keycodes[name]
75 keycodes[secondary] = _keycodes[name]
75 keycodes[secondary] = _keycodes[name]
76 inv_keycodes[_keycodes[name]] = primary
76 inv_keycodes[_keycodes[name]] = primary
77 }
77 }
78 }
78 }
79
79
80 var normalize_key = function (key) {
80 var normalize_key = function (key) {
81 return inv_keycodes[keycodes[key]];
81 return inv_keycodes[keycodes[key]];
82 }
82 }
83
83
84 var normalize_shortcut = function (shortcut) {
84 var normalize_shortcut = function (shortcut) {
85 // Put a shortcut into normalized form:
85 // Put a shortcut into normalized form:
86 // 1. Make lowercase
86 // 1. Make lowercase
87 // 2. Replace cmd by meta
87 // 2. Replace cmd by meta
88 // 3. Sort '+' separated modifiers into the order alt+ctrl+meta+shift
88 // 3. Sort '+' separated modifiers into the order alt+ctrl+meta+shift
89 // 4. Normalize keys
89 // 4. Normalize keys
90 shortcut = shortcut.toLowerCase().replace('cmd', 'meta');
90 shortcut = shortcut.toLowerCase().replace('cmd', 'meta');
91 var values = shortcut.split("+");
91 var values = shortcut.split("+");
92 if (values.length === 1) {
92 if (values.length === 1) {
93 return normalize_key(values[0])
93 return normalize_key(values[0])
94 } else {
94 } else {
95 var modifiers = values.slice(0,-1);
95 var modifiers = values.slice(0,-1);
96 var key = normalize_key(values[values.length-1]);
96 var key = normalize_key(values[values.length-1]);
97 modifiers.sort();
97 modifiers.sort();
98 return modifiers.join('+') + '+' + key;
98 return modifiers.join('+') + '+' + key;
99 }
99 }
100 }
100 }
101
101
102 var shortcut_to_event = function (shortcut, type) {
102 var shortcut_to_event = function (shortcut, type) {
103 // Convert a shortcut (shift+r) to a jQuery Event object
103 // Convert a shortcut (shift+r) to a jQuery Event object
104 type = type || 'keydown';
104 type = type || 'keydown';
105 shortcut = normalize_shortcut(shortcut);
105 shortcut = normalize_shortcut(shortcut);
106 var values = shortcut.split("+");
106 var values = shortcut.split("+");
107 var modifiers = values.slice(0,-1);
107 var modifiers = values.slice(0,-1);
108 var key = values[values.length-1];
108 var key = values[values.length-1];
109 var opts = {which: keycodes[key]};
109 var opts = {which: keycodes[key]};
110 if (modifiers.indexOf('alt') !== -1) {opts.altKey = true;}
110 if (modifiers.indexOf('alt') !== -1) {opts.altKey = true;}
111 if (modifiers.indexOf('ctrl') !== -1) {opts.ctrlKey = true;}
111 if (modifiers.indexOf('ctrl') !== -1) {opts.ctrlKey = true;}
112 if (modifiers.indexOf('meta') !== -1) {opts.metaKey = true;}
112 if (modifiers.indexOf('meta') !== -1) {opts.metaKey = true;}
113 if (modifiers.indexOf('shift') !== -1) {opts.shiftKey = true;}
113 if (modifiers.indexOf('shift') !== -1) {opts.shiftKey = true;}
114 return $.Event(type, opts);
114 return $.Event(type, opts);
115 }
115 }
116
116
117 var event_to_shortcut = function (event) {
117 var event_to_shortcut = function (event) {
118 // Convert a jQuery Event object to a shortcut (shift+r)
118 // Convert a jQuery Event object to a shortcut (shift+r)
119 var shortcut = '';
119 var shortcut = '';
120 var key = inv_keycodes[event.which]
120 var key = inv_keycodes[event.which]
121 if (event.altKey && key !== 'alt') {shortcut += 'alt+';}
121 if (event.altKey && key !== 'alt') {shortcut += 'alt+';}
122 if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl+';}
122 if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl+';}
123 if (event.metaKey && key !== 'meta') {shortcut += 'meta+';}
123 if (event.metaKey && key !== 'meta') {shortcut += 'meta+';}
124 if (event.shiftKey && key !== 'shift') {shortcut += 'shift+';}
124 if (event.shiftKey && key !== 'shift') {shortcut += 'shift+';}
125 shortcut += key;
125 shortcut += key;
126 return shortcut
126 return shortcut
127 }
127 }
128
128
129 var trigger_keydown = function (shortcut, element) {
129 var trigger_keydown = function (shortcut, element) {
130 // Trigger shortcut keydown on an element
130 // Trigger shortcut keydown on an element
131 element = element || document;
131 element = element || document;
132 element = $(element);
132 element = $(element);
133 var event = shortcut_to_event(shortcut, 'keydown');
133 var event = shortcut_to_event(shortcut, 'keydown');
134 element.trigger(event);
134 element.trigger(event);
135 }
135 }
136
136
137
137
138 // Shortcut manager class
138 // Shortcut manager class
139
139
140 var ShortcutManager = function (delay) {
140 var ShortcutManager = function (delay) {
141 this._shortcuts = {}
141 this._shortcuts = {}
142 this._counts = {}
142 this._counts = {}
143 this._timers = {}
143 this._timers = {}
144 this.delay = delay || 800; // delay in milliseconds
144 this.delay = delay || 800; // delay in milliseconds
145 }
145 }
146
146
147 ShortcutManager.prototype.help = function () {
147 ShortcutManager.prototype.help = function () {
148 var help = [];
148 var help = [];
149 for (var shortcut in this._shortcuts) {
149 for (var shortcut in this._shortcuts) {
150 var help_string = this._shortcuts[shortcut]['help'];
150 var help_string = this._shortcuts[shortcut]['help'];
151 var help_index = this._shortcuts[shortcut]['help_index'];
151 var help_index = this._shortcuts[shortcut]['help_index'];
152 if (help_string) {
152 if (help_string) {
153 if (platform === 'MacOS') {
153 if (platform === 'MacOS') {
154 shortcut = shortcut.replace('meta', 'cmd');
154 shortcut = shortcut.replace('meta', 'cmd');
155 }
155 }
156 help.push({
156 help.push({
157 shortcut: shortcut,
157 shortcut: shortcut,
158 help: help_string,
158 help: help_string,
159 help_index: help_index}
159 help_index: help_index}
160 );
160 );
161 }
161 }
162 }
162 }
163 help.sort(function (a, b) {
163 help.sort(function (a, b) {
164 if (a.help_index > b.help_index)
164 if (a.help_index > b.help_index)
165 return 1;
165 return 1;
166 if (a.help_index < b.help_index)
166 if (a.help_index < b.help_index)
167 return -1;
167 return -1;
168 return 0;
168 return 0;
169 });
169 });
170 return help;
170 return help;
171 }
171 }
172
172
173 ShortcutManager.prototype.clear_shortcuts = function () {
173 ShortcutManager.prototype.clear_shortcuts = function () {
174 this._shortcuts = {};
174 this._shortcuts = {};
175 }
175 }
176
176
177 ShortcutManager.prototype.add_shortcut = function (shortcut, data) {
177 ShortcutManager.prototype.add_shortcut = function (shortcut, data, suppress_help_update) {
178 if (typeof(data) === 'function') {
178 if (typeof(data) === 'function') {
179 data = {help: '', help_index: '', handler: data}
179 data = {help: '', help_index: '', handler: data}
180 }
180 }
181 data.help_index = data.help_index || '';
181 data.help_index = data.help_index || '';
182 data.help = data.help || '';
182 data.help = data.help || '';
183 data.count = data.count || 1;
183 data.count = data.count || 1;
184 if (data.help_index === '') {
184 if (data.help_index === '') {
185 data.help_index = 'zz';
185 data.help_index = 'zz';
186 }
186 }
187 shortcut = normalize_shortcut(shortcut);
187 shortcut = normalize_shortcut(shortcut);
188 this._counts[shortcut] = 0;
188 this._counts[shortcut] = 0;
189 this._shortcuts[shortcut] = data;
189 this._shortcuts[shortcut] = data;
190 if (!suppress_help_update) {
191 // update the keyboard shortcuts notebook help
192 IPython.quick_help = new IPython.QuickHelp();
193 }
190 }
194 }
191
195
192 ShortcutManager.prototype.add_shortcuts = function (data) {
196 ShortcutManager.prototype.add_shortcuts = function (data) {
193 for (var shortcut in data) {
197 for (var shortcut in data) {
194 this.add_shortcut(shortcut, data[shortcut]);
198 this.add_shortcut(shortcut, data[shortcut], True);
195 }
199 }
200 // update the keyboard shortcuts notebook help
201 IPython.quick_help = new IPython.QuickHelp();
196 }
202 }
197
203
198 ShortcutManager.prototype.remove_shortcut = function (shortcut) {
204 ShortcutManager.prototype.remove_shortcut = function (shortcut) {
199 shortcut = normalize_shortcut(shortcut);
205 shortcut = normalize_shortcut(shortcut);
200 delete this._counts[shortcut];
206 delete this._counts[shortcut];
201 delete this._shortcuts[shortcut];
207 delete this._shortcuts[shortcut];
202 }
208 }
203
209
204 ShortcutManager.prototype.count_handler = function (shortcut, event, data) {
210 ShortcutManager.prototype.count_handler = function (shortcut, event, data) {
205 var that = this;
211 var that = this;
206 var c = this._counts;
212 var c = this._counts;
207 var t = this._timers;
213 var t = this._timers;
208 var timer = null;
214 var timer = null;
209 if (c[shortcut] === data.count-1) {
215 if (c[shortcut] === data.count-1) {
210 c[shortcut] = 0;
216 c[shortcut] = 0;
211 var timer = t[shortcut];
217 var timer = t[shortcut];
212 if (timer) {clearTimeout(timer); delete t[shortcut];}
218 if (timer) {clearTimeout(timer); delete t[shortcut];}
213 return data.handler(event);
219 return data.handler(event);
214 } else {
220 } else {
215 c[shortcut] = c[shortcut] + 1;
221 c[shortcut] = c[shortcut] + 1;
216 timer = setTimeout(function () {
222 timer = setTimeout(function () {
217 c[shortcut] = 0;
223 c[shortcut] = 0;
218 }, that.delay);
224 }, that.delay);
219 t[shortcut] = timer;
225 t[shortcut] = timer;
220 }
226 }
221 return false;
227 return false;
222 }
228 }
223
229
224 ShortcutManager.prototype.call_handler = function (event) {
230 ShortcutManager.prototype.call_handler = function (event) {
225 var shortcut = event_to_shortcut(event);
231 var shortcut = event_to_shortcut(event);
226 var data = this._shortcuts[shortcut];
232 var data = this._shortcuts[shortcut];
227 if (data) {
233 if (data) {
228 var handler = data['handler'];
234 var handler = data['handler'];
229 if (handler) {
235 if (handler) {
230 if (data.count === 1) {
236 if (data.count === 1) {
231 return handler(event);
237 return handler(event);
232 } else if (data.count > 1) {
238 } else if (data.count > 1) {
233 return this.count_handler(shortcut, event, data);
239 return this.count_handler(shortcut, event, data);
234 }
240 }
235 }
241 }
236 }
242 }
237 return true;
243 return true;
238 }
244 }
239
245
240 return {
246 return {
241 keycodes : keycodes,
247 keycodes : keycodes,
242 inv_keycodes : inv_keycodes,
248 inv_keycodes : inv_keycodes,
243 ShortcutManager : ShortcutManager,
249 ShortcutManager : ShortcutManager,
244 normalize_key : normalize_key,
250 normalize_key : normalize_key,
245 normalize_shortcut : normalize_shortcut,
251 normalize_shortcut : normalize_shortcut,
246 shortcut_to_event : shortcut_to_event,
252 shortcut_to_event : shortcut_to_event,
247 event_to_shortcut : event_to_shortcut,
253 event_to_shortcut : event_to_shortcut,
248 trigger_keydown : trigger_keydown
254 trigger_keydown : trigger_keydown
249 }
255 }
250
256
251 }(IPython));
257 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now