skip to Main Content

I am trying to set Jupyter Notebook a keyboard shortcut to Insert New Code Cell below. I was trying to determine when the "when" expression is true. The when expression for "Notebook: Insert Code Cell Below" is notebookCellListFocused && !inputFocus. To see when these "when clause contexts" are true, I followed the instructions here: when clause contexts.

I went to Help > Toggle Developer Tools. Then I used the command "Developer: Inspect Context Keys". My cursor turned into a cross and started highlighting portions of the VSCode UI in red.

I clicked on a notebook cell in my Jupyter notebook, and then observed the output context in the Developer Tools window. The context showed that inputFocus is False. But my chosen keyboard shortcut to insert Code Cell Below was not working. When I changed the when expression for my keyboard shortcut to inputFocus && notebookCellListFocused, the keyboard shortcut did work. It seems that inputFocus is actually true when I am editing the cell, which also tracks with common sense.

So my question is, why did the Inspect Context Keys command return a context where the inputFocus context is False? How do I get it to return the context that I want?

2

Answers


  1. It’s possible that the "Inspect Context Keys" command in VSCode might not perfectly capture the context you expect due to the timing of evaluation or other factors. If you want to ensure your keyboard shortcut works when editing a cell, use the context expression inputFocus && notebookCellListFocused, as this better matches your desired behavior. Trust your observation and intuition about when inputFocus should be true while editing a cell. The "Inspect Context Keys" tool might have limitations in certain scenarios, and you’ve already found a practical solution by adjusting the context condition to match your needs.

    Login or Signup to reply.
  2. There are multiple keyboard shortcuts bound to ctrl+enter by default, so which one takes priority depends further on context. Here are a few of those related to Jupyter Notebooks:

    { "key": "ctrl+enter",            "command": "editor.action.insertLineAfter",
                                         "when": "editorTextFocus && !editorReadonly" },
    
    { "key": "ctrl+enter",            "command": "notebook.cell.insertCodeCellBelow",
                                         "when": "notebookCellListFocused && !inputFocus" },
    
    { "key": "ctrl+enter",            "command": "jupyter.runcurrentcell",
                                         "when": "editorTextFocus && isWorkspaceTrusted && jupyter.hascodecells && !editorHasSelection && !jupyter.havenativecells && !notebookEditorFocused" },
    
    { "key": "ctrl+enter",            "command": "notebook.cell.executeAndFocusContainer",
                                         "when": "notebookCellListFocused || editorTextFocus && inputFocus && notebookEditorFocused" },
    
    { "key": "ctrl+enter",            "command": "jupyter.runByLineStop",
                                         "when": "notebookCellResource in 'jupyter.notebookeditor.runByLineCells'" },
    

    For example, for me, notebook.cell.executeAndFocusContainer takes precedence over notebook.cell.insertCodeCellBelow when the cell input area is focused. So I think part of what you are observing may be related to this.

    You can actually debug what VS Code does with keyboard inputs by using the Developer: Toggle Keyboard Shortcuts Troubleshooting command in the command palette. I find it helpful for scenarios like this.

    I also noticed differences in context keys depending on which part of a notebook cell is clicked:

    • With this selection, and the cell input focused, I got "editorTextFocus": true, "inputFocus": true, "notebookCellListFocused": true, "notebookEditorFocused": true,
      enter image description here

      This is where ctrl+enter triggers notebook.cell.executeAndFocusContainer for me.

    • With this selection, and the cell input focused, I got "inputFocus": false, "notebookCellListFocused": true, "notebookEditorFocused": true,, and the editorTextFocus property was not present
      enter image description here

      When I tab focus to the play button on the left (need to toggle Tab Moves Focus to tab out of the cell input), ctrl+enter still triggers notebook.cell.executeAndFocusContainer for me here, but unlike in the above case, here, ctrl+shift+enter actually triggers notebook.cell.insertCodeCellBelow instead of editor.action.insertLineBefore.

    The context showed that inputFocus is False. But my chosen keyboard shortcut to insert Code Cell Below was not working. When I changed the when expression for my keyboard shortcut to inputFocus && notebookCellListFocused, the keyboard shortcut did work.

    This sounds very perplexing to me and I suspect that in the case where it wasn’t working, some other keyboard shortcut was taking precedence. I’d suggest trying the keyboard troubleshooting command I showed you.

    So my question is, why did the Inspect Context Keys command return a context where the inputFocus context is False?

    Maybe that’s just how it’s supposed to work. You didn’t show what exactly in the cell you clicked to inspect, so I can’t investigate any further.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search