简体   繁体   English

Visual Studio扩展的动态热键

[英]Dynamic hotkeys for Visual Studio extension

Is there any way I can dynamically add global hotkeys from within a Visual Studio extension / package? 有什么方法可以从Visual Studio扩展/程序包中动态添加全局热键?

Declaring the hotkeys in the *.vsct file of my package is no option because I do not know which hotkeys should be available at runtime - there is kind of a plugin-system within my extension that can add/remove hotkeys dynamically. 在包的* .vsct文件中声明热键是没有选择的,因为我不知道在运行时应该使用哪些热键-我的扩展程序中有一种插件系统可以动态添加/删除热键。

Adding a message filter does only work when .NET runs the message loop - which is not the case in VS. 添加消息过滤器仅在.NET运行消息循环时才起作用-VS中不是这种情况。

Thanks for your support! 谢谢你的支持!

The best way to add hot keys is to go ahead and define your commands without any key bindings. 添加热键的最佳方法是继续并定义您的命令,而无需任何键绑定。 Essentially make sure that the command MyExtension.MyOperation is available but bound to no keys. 本质上,请确保命令MyExtension.MyOperation可用但未绑定任何键。

At run time you can grab the DTE.Command object for your extension command. 在运行时,您可以获取扩展命令的DTE.Command对象。 It's available through the Commands property on the DTE object. 通过DTE对象上的Commands属性可以使用它。 From there you can change the key the particular command is bound to by setting the Binding property. 在此可以通过设置Binding属性来更改特定命令绑定到的键。

For example if you wanted to change the binding to CTRL+o I would do the following 例如,如果您想将绑定更改为CTRL + o,我将执行以下操作

command.Binding = "Global::ctrl+o";

The syntax for the key binding is actually fairly well documented on MSDN. 实际上,MSDN上对键绑定的语法进行了很好的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM