实现目标
在获取左侧项目栏文件夹焦点情况下 添加新建文件夹的快捷键 Shift + A
我本来是个atom忠实粉丝, 但是atom使用window10自带输入法在书写汉字时总是会出现首字母缺失的情况, 查了好久没法解决, 直接放弃了atom, 转而使用Vs code书写markdown,
但是使用Vs code时发现在左侧导航栏配置新建文件和文件夹没有快捷方式, 于是就想配置一个, 但是在百度上搜了好长时间, 却总是解决不了KeyBoard Shortcuts上面的when 属性, 最终访问的 Vs code 官方文档 解决的.
下面是官方文档配置快捷键的地址
https://code.visualstudio.com/docs/getstarted/keybindings
相应步骤
-
点击左下角设置图标, 点击其中的选项KeyBoard Shortcuts.
-
输入 “explorer.new” 找到相应命令, 点击 explorer.newFolder 行配置上 “Shift + A”, 快捷键. 但是这里还有个 When 属性, 如果放任不管的话, 软件会默认全局快捷键, 试想一下, 在你编辑的时候突然按了个 “Shift + A”, 结果新建了个文件夹, 那真是太糟糕了, 所以 When 属性一定要解决.
-
在KeyBoard Shortcuts视图里, 有一个打开keybindings.json文件的链接, 点击可以打开该文件,
此时我们可以看到右侧文件的相应配置, 这就是我们刚刚配置的东西. 我们可以参照左边的格式配置 when 属性, 可是我们该怎么选择属性呢.
-
找到符合条件的When属性
现在我们可以打开 Vs code 的官方文档 Key Bindings for Visual Studio Code,这个是国外的网站, 纯英文. 可能打不开, 下面贴上文档里的关于 When 的介绍, 里面也是英文的, 不过作为使用Vs code的开发人员, 这点应该能看懂的, 实在不行也可以google翻译.
Context name True when Editor contexts editorFocus
An editor has focus, either the text or a widget. editorTextFocus
The text in an editor has focus (cursor is blinking). textInputFocus
Any editor has focus (regular editor, debug REPL, etc.). inputFocus
Any text input area has focus (editors or text boxes). editorHasSelection
Text is selected in the editor. editorHasMultipleSelections
Multiple regions of text are selected (multiple cursors). editorReadonly
The editor is read only. editorLangId
True when the editor’s associated language ID matches. Example: "editorLangId == typescript"
.isInDiffEditor
The active editor is a difference editor. isInEmbeddedEditor
True when the focus is inside an embedded editor. Operating system contexts isLinux
True when the OS is Linux isMac
True when the OS is macOS isWindows
True when the OS is Windows isWeb
True when accessing the editor from the Web List contexts listFocus
A list has focus. listSupportsMultiselect
A list supports multi select. listHasSelectionOrFocus
A list has selection or focus. listDoubleSelection
A list has a selection of 2 elements. listMultiSelection
A list has a selection of multiple elements. Mode contexts inSnippetMode
The editor is in snippet mode. inQuickOpen
The Quick Open dropdown has focus. Resource contexts resourceScheme
True when the resource Uri scheme matches. Example: "resourceScheme == file"
resourceFilename
True when the Explorer or editor filename matches. Example: "resourceFilename == gulpfile.js"
resourceExtname
True when the Explorer or editor filename extension matches. Example: "resourceExtname == .js"
resourceDirname
True when the Explorer or editor’s resource absolute folder path matches. Example: "resourceDirname == /users/alice/project/src"
resourcePath
True when the Explorer or editor’s resource absolute path matches. Example: "resourcePath == /users/alice/project/gulpfile.js"
resourceLangId
True when the Explorer or editor title language ID matches. Example: "resourceLangId == markdown"
isFileSystemResource
True when the Explorer or editor file is a file system resource that can be handled from a file system provider resourceSet
True when an Explorer or editor file is set resource
The full Uri of the Explorer or editor file Explorer contexts explorerViewletVisible
True if Explorer view is visible. explorerViewletFocus
True if Explorer view has keyboard focus. filesExplorerFocus
True if File Explorer section has keyboard focus. openEditorsFocus
True if OPEN EDITORS section has keyboard focus. explorerResourceIsFolder
True if a folder is selected in the Explorer. Editor widget contexts findWidgetVisible
Editor Find widget is visible. suggestWidgetVisible
Suggestion widget (IntelliSense) is visible. suggestWidgetMultipleSuggestions
Multiple suggestions are displayed. renameInputVisible
Rename input text box is visible. referenceSearchVisible
Peek References peek window is open. inReferenceSearchEditor
The Peek References peek window editor has focus. config.editor.stablePeek
Keep peek editors open (controlled by editor.stablePeek
setting).quickFixWidgetVisible
Quick Fix widget is visible. parameterHintsVisible
Parameter hints are visible (controlled by editor.parameterHints.enabled
setting).parameterHintsMultipleSignatures
Multiple parameter hints are displayed. Debugger contexts debuggersAvailable
An appropriate debugger extension is available inDebugMode
A debug session is running. debugState
Active debugger state. Possible values are inactive
,initializing
,stopped
,running
.debugType
True when debug type matches. Example: "debugType == 'node'"
.inDebugRepl
Focus is in the Debug Console REPL. Integrated terminal contexts terminalFocus
An integrated terminal has focus. terminalIsOpen
An integrated terminal is opened. Timeline view contexts timelineFollowActiveEditor
True if the Timeline view is following the active editor. Timeline view item contexts timelineItem
True when the timeline item’s context value matches. Example: "timelineItem =~ /git:file:commit\\b/"
.Extension contexts extension
True when the extension’s ID matches. Example: "extension == eamodio.gitlens"
.extensionStatus
True when the extension is installed. Example: "extensionStatus == installed"
.extensionHasConfiguration
True if the extension has configuration. Global UI contexts notificationFocus
Notification has keyboard focus. notificationCenterVisible
Notification Center is visible at the bottom right of VS Code. notificationToastsVisible
Notification toast is visible at the bottom right of VS Code. searchViewletVisible
Search view is open. sideBarVisible
Side Bar is displayed. sideBarFocus
Side Bar has focus. panelFocus
Panel has focus. inZenMode
Window is in Zen Mode. isCenteredLayout
Editor is in centered layout mode. workbenchState
Can be empty
,folder
(1 folder), orworkspace
.workspaceFolderCount
Count of workspace folders. replaceActive
Search view Replace text box is open. view
True when view identifier matches. Example: "view == myViewsExplorerID"
.viewItem
True when viewItem context matches. Example: "viewItem == someContextValue"
.isFullscreen
True when window is in fullscreen. focusedView
The identifier of the currently focused view. canNavigateBack
True if it is possible to navigate back. canNavigateForward
True if it is possible to navigate forward. canNavigateToLastEditLocation
True if it is possible to navigate to the last edit location. Global Editor UI contexts textCompareEditorVisible
At least one diff (compare) editor is visible. textCompareEditorActive
A diff (compare) editor is active. editorIsOpen
True if one editor is open. groupEditorsCount
Number of editors in a group. activeEditorGroupEmpty
True if the active editor group has no editors. activeEditorGroupIndex
A number starting from 1
reflecting the position of an editor group in the editor grid. The group with index1
will be the first in the top-left corner.activeEditorGroupLast
Will be true
for the last editor group in the editor grid.multipleEditorGroups
True when multiple editor groups are present. activeEditor
The identifier of the active editor in a group. activeEditorIsDirty
True when the active editor in a group is dirty. activeEditorIsNotPreview
True when the active editor in a group is not in preview mode. activeEditorIsPinned
True when the active editor in a group is pinned. inSearchEditor
True when focus is inside a search editor. Configuration settings contexts config.editor.minimap.enabled
True when the setting editor.minimap.enabled
istrue
. -
鉴于我们要添加访问左侧 Explorer 时有效的快捷键, 因此可以选择Explorer contexts 中的 explorerResourceIsFolder 属性.,最终 keybindings.json 文件如下
// Place your key bindings in this file to override the defaults [ { "key": "a", "command": "explorer.newFile", "when": "explorerResourceIsFolder" }, { "key": "shift+a", "command": "explorer.newFolder", "when": "explorerResourceIsFolder" } ]
新版的 vscode 已经没有必要直接去修改 keybindings.json 文件,直接在 键盘快捷方式
面板右键更改when表达式就可以了