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
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.
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:
For example, for me,
notebook.cell.executeAndFocusContainer
takes precedence overnotebook.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,
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 theeditorTextFocus
property was not presentWhen 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 triggersnotebook.cell.executeAndFocusContainer
for me here, but unlike in the above case, here, ctrl+shift+enter actually triggersnotebook.cell.insertCodeCellBelow
instead ofeditor.action.insertLineBefore
.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.
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.