实现目标
在获取左侧项目栏文件夹焦点情况下 添加新建文件夹的快捷键 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 | |
| An editor has focus, either the text or a widget. |
| The text in an editor has focus (cursor is blinking). |
| Any editor has focus (regular editor, debug REPL, etc.). |
| Any text input area has focus (editors or text boxes). |
| Text is selected in the editor. |
| Multiple regions of text are selected (multiple cursors). |
| The editor is read only. |
| True when the editor’s associated language ID matches. Example: |
| The active editor is a difference editor. |
| True when the focus is inside an embedded editor. |
Operating system contexts | |
| True when the OS is Linux |
| True when the OS is macOS |
| True when the OS is Windows |
| True when accessing the editor from the Web |
List contexts | |
| A list has focus. |
| A list supports multi select. |
| A list has selection or focus. |
| A list has a selection of 2 elements. |
| A list has a selection of multiple elements. |
Mode contexts | |
| The editor is in snippet mode. |
| The Quick Open dropdown has focus. |
Resource contexts | |
| True when the resource Uri scheme matches. Example: |
| True when the Explorer or editor filename matches. Example: |
| True when the Explorer or editor filename extension matches. Example: |
| True when the Explorer or editor’s resource absolute folder path matches. Example: |
| True when the Explorer or editor’s resource absolute path matches. Example: |
| True when the Explorer or editor title language ID matches. Example: |
| True when the Explorer or editor file is a file system resource that can be handled from a file system provider |
| True when an Explorer or editor file is set |
| The full Uri of the Explorer or editor file |
Explorer contexts | |
| True if Explorer view is visible. |
| True if Explorer view has keyboard focus. |
| True if File Explorer section has keyboard focus. |
| True if OPEN EDITORS section has keyboard focus. |
| True if a folder is selected in the Explorer. |
Editor widget contexts | |
| Editor Find widget is visible. |
| Suggestion widget (IntelliSense) is visible. |
| Multiple suggestions are displayed. |
| Rename input text box is visible. |
| Peek References peek window is open. |
| The Peek References peek window editor has focus. |
| Keep peek editors open (controlled by |
| Quick Fix widget is visible. |
| Parameter hints are visible (controlled by |
| Multiple parameter hints are displayed. |
Debugger contexts | |
| An appropriate debugger extension is available |
| A debug session is running. |
| Active debugger state. Possible values are |
| True when debug type matches. Example: |
| Focus is in the Debug Console REPL. |
Integrated terminal contexts | |
| An integrated terminal has focus. |
| An integrated terminal is opened. |
Timeline view contexts | |
| True if the Timeline view is following the active editor. |
Timeline view item contexts | |
| True when the timeline item’s context value matches. Example: |
Extension contexts | |
| True when the extension’s ID matches. Example: |
| True when the extension is installed. Example: |
| True if the extension has configuration. |
Global UI contexts | |
| Notification has keyboard focus. |
| Notification Center is visible at the bottom right of VS Code. |
| Notification toast is visible at the bottom right of VS Code. |
| Search view is open. |
| Side Bar is displayed. |
| Side Bar has focus. |
| Panel has focus. |
| Window is in Zen Mode. |
| Editor is in centered layout mode. |
| Can be |
| Count of workspace folders. |
| Search view Replace text box is open. |
| True when view identifier matches. Example: |
| True when viewItem context matches. Example: |
| True when window is in fullscreen. |
| The identifier of the currently focused view. |
| True if it is possible to navigate back. |
| True if it is possible to navigate forward. |
| True if it is possible to navigate to the last edit location. |
Global Editor UI contexts | |
| At least one diff (compare) editor is visible. |
| A diff (compare) editor is active. |
| True if one editor is open. |
| Number of editors in a group. |
| True if the active editor group has no editors. |
| A number starting from |
| Will be |
| True when multiple editor groups are present. |
| The identifier of the active editor in a group. |
| True when the active editor in a group is dirty. |
| True when the active editor in a group is not in preview mode. |
| True when the active editor in a group is pinned. |
| True when focus is inside a search editor. |
Configuration settings contexts | |
| True when the setting |
- 鉴于我们要添加访问左侧 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表达式就可以了