Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio code 在VS代码扩展中,当用户剪切/复制/粘贴时,如何通知我?_Visual Studio Code_Vscode Extensions - Fatal编程技术网

Visual studio code 在VS代码扩展中,当用户剪切/复制/粘贴时,如何通知我?

Visual studio code 在VS代码扩展中,当用户剪切/复制/粘贴时,如何通知我?,visual-studio-code,vscode-extensions,Visual Studio Code,Vscode Extensions,对于我的扩展,我需要知道何时发生剪切/复制/粘贴,并能够获取与这些操作相关联的文本。如果我知道文本何时发生,我可能会从编辑器中获取文本 我找不到这些操作的侦听器。我想我可以查找ctrl-x、ctrl-c和ctrl-v键盘输入,但有些用户可能使用编辑菜单而不使用键盘 当这些操作通过键盘或“编辑”菜单进行时,是否有办法得到通知?没有直接访问剪贴板的api,但某些扩展会覆盖默认的复制和粘贴快捷方式,以自定义复制粘贴行为。以下是两个例子: 但是,正如您所注意到的,当使用上下文菜单进行复制时,这种

对于我的扩展,我需要知道何时发生剪切/复制/粘贴,并能够获取与这些操作相关联的文本。如果我知道文本何时发生,我可能会从编辑器中获取文本

我找不到这些操作的侦听器。我想我可以查找ctrl-x、ctrl-c和ctrl-v键盘输入,但有些用户可能使用编辑菜单而不使用键盘


当这些操作通过键盘或“编辑”菜单进行时,是否有办法得到通知?

没有直接访问剪贴板的api,但某些扩展会覆盖默认的复制和粘贴快捷方式,以自定义复制粘贴行为。以下是两个例子:

但是,正如您所注意到的,当使用上下文菜单进行复制时,这种方法将不起作用。为了支持这一点,您可以尝试拦截
editor.action.clipboardCopyAction
命令。查看Vim扩展如何截取
type
命令以获取此示例:

此处为原始询问者

我提出了一个解决方案,涉及覆盖编辑器中的默认剪切/复制/粘贴操作。以下是extension.js中“copy”的代码(我使用的是js而不是ts):


这绝对不是最好的解决方案。。。然而,它似乎确实起了作用。有什么意见/建议吗?是否存在重复注册和注销会导致的任何问题?

是的,这对我很有用。您知道overrideCommand()的任何文档吗?我可能仍然需要使用:vscode.commands.executeCommand(“default:editor.action.clipboardPaste”)执行默认行为‌​行动“)?
//override the editor.action.clipboardCopyAction with our own
var clipboardCopyDisposable = vscode.commands.registerTextEditorCommand('editor.action.clipboardCopyAction', overriddenClipboardCopyAction); 

context.subscriptions.push(clipboardCopyDisposable);

/*
 * Function that overrides the default copy behavior. We get the selection and use it, dispose of this registered
 * command (returning to the default editor.action.clipboardCopyAction), invoke the default one, and then re-register it after the default completes
 */
function overriddenClipboardCopyAction(textEditor, edit, params) {

    //debug
    console.log("---COPY TEST---");

    //use the selected text that is being copied here
    getCurrentSelectionEvents(); //not shown for brevity

    //dispose of the overridden editor.action.clipboardCopyAction- back to default copy behavior
    clipboardCopyDisposable.dispose();

    //execute the default editor.action.clipboardCopyAction to copy
    vscode.commands.executeCommand("editor.action.clipboardCopyAction").then(function(){

        console.log("After Copy");

        //add the overridden editor.action.clipboardCopyAction back
        clipboardCopyDisposable = vscode.commands.registerTextEditorCommand('editor.action.clipboardCopyAction', overriddenClipboardCopyAction);

        context.subscriptions.push(clipboardCopyDisposable);
    }); 
}