Skip to content
This repository

added key handler for control-s to notebook, seems to work pretty well #1334

Merged
merged 3 commits into from about 2 years ago

3 participants

fawce Thomas Kluyver Fernando Perez
fawce

Hi,

There was a thread on the dev list about key bindings and hooking ctrl-save to trigger a new save (http://mail.scipy.org/pipermail/ipython-dev/2012-January/008674.html).

What makes this hard is that the browser will only trigger keyboard events for the element that currently has focus. To see what this means, open a google docs spreadsheet. Hit ctrl-s, and you'll see a quick status in the header that your doc is being saved. Now, click on a menu and leave the menu dropped down - ctrl-s now will ask you to (completely pointlessly) save the html of the page. In google's spreadsheet this oddity is almost never an issue, because you almost never notice this discrepancy as you're almost always keyboard focused on the sheet.

In the updated UI, notebook's layout has the same convenient property as google docs, where focus is almost always in the notebook element or a child. The only way I was able to end up without focus in the notebook was to click the menu. As long as a menu is dropped down, the ctrl-s triggers the browser's dialog.

thanks,
fawce

p.s.
This PR is from a branch based on a pull from ellisonbg/master.

Thomas Kluyver takluyver commented on the diff January 27, 2012
IPython/frontend/html/notebook/static/js/notebook.js
@@ -62,7 +62,14 @@ var IPython = (function (IPython) {
62 62
         $(document).keydown(function (event) {
63 63
             // console.log(event);
64 64
             if (that.read_only) return true;
65  
-            if (event.which === 27) {
  65
+            
  66
+            // Save (CTRL+S) or (AppleKey+S) 
  67
+            //metaKey = applekey on mac
  68
+            if ((event.ctrlKey || event.metaKey) && event.keyCode==83) { 
1
Thomas Kluyver Collaborator

As I understand it, this will catch either Ctrl-S or Meta-S (which is Alt-S?) on any platform, which isn't ideal. I don't do much Javascript - is there a neat way to only catch the shortcut relevant to the platform?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Thomas Kluyver
Collaborator

Thanks for tackling this, I think it will be good for usability.

Fernando Perez
Owner

Thanks @fawce!

@takluyver, I tested this on Firefox and Chrome (linux), with an Apple keyboard, and I see no problems. Alt (meta) and the Apple key are not captured by this, only Control-S works. And I assume that @fawce is on OSX, so for him this makes Cmd-S (Apple key) work as well.

It is indeed a big usability win, so I'm merging it now. There's always the chance we can get a bug report from some user on some combination of platform/browser, but the only way to find out is to merge and put it out for public use.

Fernando Perez fperez merged commit 93bb469 into from January 28, 2012
Fernando Perez fperez closed this January 28, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
5  IPython/frontend/html/notebook/static/js/codecell.js
@@ -70,7 +70,7 @@ var IPython = (function (IPython) {
70 70
         if (this.read_only){
71 71
             return false;
72 72
         }
73  
-
  73
+        
74 74
         // note that we are comparing and setting the time to wait at each key press.
75 75
         // a better wqy might be to generate a new function on each time change and
76 76
         // assign it to CodeCell.prototype.request_tooltip_after_time
@@ -82,7 +82,8 @@ var IPython = (function (IPython) {
82 82
         if(event.type === 'keydown' ){
83 83
             that.remove_and_cancel_tooltip();
84 84
         }
85  
-
  85
+        
  86
+         
86 87
         if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) {
87 88
             // Always ignore shift-enter in CodeMirror as we handle it.
88 89
             return true;
9  IPython/frontend/html/notebook/static/js/notebook.js
@@ -62,7 +62,14 @@ var IPython = (function (IPython) {
62 62
         $(document).keydown(function (event) {
63 63
             // console.log(event);
64 64
             if (that.read_only) return true;
65  
-            if (event.which === 27) {
  65
+            
  66
+            // Save (CTRL+S) or (AppleKey+S) 
  67
+            //metaKey = applekey on mac
  68
+            if ((event.ctrlKey || event.metaKey) && event.keyCode==83) { 
  69
+                IPython.save_widget.save_notebook();
  70
+                event.preventDefault();
  71
+                return false;
  72
+            } else if (event.which === 27) {
66 73
                 // Intercept escape at highest level to avoid closing 
67 74
                 // websocket connection with firefox
68 75
                 event.preventDefault();
2  IPython/frontend/html/notebook/static/js/textcell.js
@@ -67,7 +67,7 @@ var IPython = (function (IPython) {
67 67
         // handlers and is used to provide custom key handling. Its return
68 68
         // value is used to determine if CodeMirror should ignore the event:
69 69
         // true = ignore, false = don't ignore.
70  
-
  70
+        
71 71
         if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) {
72 72
             // Always ignore shift-enter in CodeMirror as we handle it.
73 73
             return true;
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.