Home > Software engineering >  VS Code onDidChange method not listening/logging changes
VS Code onDidChange method not listening/logging changes

Time:10-29

I want to listen to keypresses in a VS Code extension I'm writing (in disposable2):

import * as vscode from 'vscode'

export function activate(context: vscode.ExtensionContext) {
    const disposable = vscode.commands.registerCommand('extension.search', async () => {
        // some more code
    })

    const disposable2 = vscode.window.onDidChangeActiveTextEditor(handleChange => {
        console.log(JSON.stringify(handleChange))
        vscode.window.showInformationMessage(JSON.stringify(handleChange))
    })

    context.subscriptions.push(disposable)
    context.subscriptions.push(disposable2)
}

But when I run the extension and press keys in the active editor, nothing is logged or shown.

Why could this be, and how to fix it?

Note: Sorry, but I don't know how to execute this code on the web (since it's a VS Code extension).

CodePudding user response:

onDidChangeActiveTextEditor will fire when you change the file you are editing, such as when switching tabs, as per the documentation:

An Event which fires when the active editor has changed. Note that the event also fires when the active editor changes to undefined.

I believe you are looking for onDidChangeTextDocument.

  • Related