vscode 扩展 - 如何记录击键 [英] vscode extension - How to log keystrokes
问题描述
我正在对 vscode 扩展
(针对某些大学项目)进行用户测试,我需要获取在文本编辑器中按下的按键的日志.
I am doing user testing for a vscode extension
(for some university project) and I need to get a log of the keys that are pressed in the text editor.
我已尝试使用 onDidChangeTextDocument
,但这不能捕获不修改文档的击键(例如,Ctrl 或 Alt).我还尝试向活动文本编辑器添加一个事件侦听器,如下所示:
I have tried with onDidChangeTextDocument
, but that doesn't capture keystrokes that don't modify the document (for instance, Ctrl or Alt). I have also tried to add an event listener to the active text editor like so:
vscode.window.activeTextEditor.addEventListener(('keydown', event => {
console.log(`KeyboardEvent: ${event.key || event.keyCode}`)
}))
但它看起来不像 activeTextEditor
允许添加这样的功能.
but it doesn't look like activeTextEditor
allows to add such function.
有谁知道 vscode 是否允许监听按键?
Does anyone know if vscode allows to listen for keystrokes?
推荐答案
VS Code 不会直接向扩展公开按键事件.尝试查看 VS Code 的 api 指南,并考虑是否可以以不同的方式完成您所追求的目标.
VS Code does not expose keypress events to extension directly. Try reviewing VS Code's api guide and think about if you can accomplish what you are after in a different way.
然而,与 VS Code 中的 keydown
最接近的是扩展可以在 VS Code 中注册 type
命令:vscode.commands.registerCommand('type', e => ...)
However, the closest approximation to keydown
in VS Code is that an extension can register for the type
command in VS Code: vscode.commands.registerCommand('type', e => ...)
此命令将触发按键.但是它不直接记录修饰键.type
命令也应该只用于调试或在非常特殊的情况下,因为它有性能问题和 主要限制
This command will fire for keypresses. However it does not log modifier keys directly. The type
command should also only be used for debugging or in very specific cases as it has performance problems and major limitations
这篇关于vscode 扩展 - 如何记录击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!