在 Visual Studio 代码扩展中捕获击键 [英] Capturing keystrokes in visual studio code extension

查看:24
本文介绍了在 Visual Studio 代码扩展中捕获击键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在 Visual Studio 代码扩展中捕获击键.我需要知道添加或删除的新文本以及文件中更改的位置.

I would like to be able to capture keystrokes in a visual studio code extension. I need to know the new text that was either added or removed and the position of the change in the file.

我已经注册了一个监听器:

I have registered a listener:

vscode.window.onDidChangeTextEditorSelection(handleChange)

并且正在获取每一个插入符号移动的更新,但我很难从传入的事件中添加/删除文本和位置.目前,我正在处理程序中执行此操作:

and am getting updates on every single caret move but I am having a hard time getting added/removed text and positions from the event that is passed in. Currently, I am doing this in the handler:

function handleChange(event) {
    console.log("Change in the text editor");
    for(var i = 0;i < event.selections.length;i++)
    {
        var selection = event.selections[i];
        console.log("Start- Line: (" + selection.start.line + ") Col: (" + selection.start.character + ") End- Line: (" + selection.end.line + ") Col: (" + selection.end.character + ")");
    }
    console.log(event);
}

文档提到了一个叫做 TextDocumentContentChangeEvent 的东西,它看起来正是我需要的,但我不知道如何注册一个处理程序来接收这些.

The documentation mentions something called a TextDocumentContentChangeEvent which seems like exactly what I need but I don't know how to register a handler to receive these.

推荐答案

我在我原来的问题中发现了这个问题.我正在监听错误的事件.要获取具有返回 TextDocumentContentChangeEvents 的名为 contentChanges 的属性的 TextDocumentChangeEvents,请使用以下代码:

I discovered the problem in my original question. I was listening for the wrong event. To get TextDocumentChangeEvents which have a property called contentChanges which returns TextDocumentContentChangeEvents, use this code:

vscode.workspace.onDidChangeTextDocument(handleChange)

并传入一个函数,该函数将在每次更改文件时使用 TextDocumentChangeEvent 调用.

and pass in a function which will be called with a TextDocumentChangeEvent on every change to a file.

这篇关于在 Visual Studio 代码扩展中捕获击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆